Uploading Files directly to Cloud Storage without going through App Engine Instances

71 views
Skip to first unread message

Abhiram Vedullapalli

unread,
Oct 25, 2018, 8:22:02 AM10/25/18
to Google App Engine
I am using SignedURL to upload file directly to Cloud Storage ( using com.google.cloud.storage Client library)  without going through App Engine instances. The Process I am following is : 

Creating a empty Object and generating a SignedURL for that Object :

Storage storage = null;

try{

FileInputStream credentialsStream = new FileInputStream("JSONFile");

Credentials credentials = GoogleCredentials.fromStream(credentialsStream);

storage
= StorageOptions.newBuilder().setCredentials(credentials).build().getService();

}catch(IOException e) {

  e
.printStackTrace();

}

Acl aclObject = Acl.of(User.ofAllUsers(),Role.OWNER);

List<Acl> aclAccess = new ArrayList<>();

aclAccess.add(aclObject);

//BucketName and User name are Strings.
BlobId blobId = BlobId.of(BUCKET_NAME,USER_NAME+"TeamLogo");

BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setAcl(aclAccess).setContentType("image/jpeg").build();

Blob blob = storage.create(blobInfo);

By now , An Empty Object is created in Cloud Storage




Screenshot (3).png


I am generating a signedURL for this Empty Object such that when user uploads a file , the Content of the file replaces the Empty object . 

HttpMethod httpMethod = HttpMethod.PUT;

   
ServiceAccountSigner signer = ServiceAccountCredentials.newBuilder().setClientId(CLIENT_ID).setClientEmail(CLIENT_EMAIL).setPrivateKey(PRIVATEKEY).setPrivateKeyId(PRIVATE_KEY_ID).build()

    URL url
= blob.signUrl(10,TimeUnit.MINUTES,Storage.SignUrlOption.httpMethod(httpMethod),Storage.SignUrlOption.signWith(signer),Storage.SignUrlOption.withContentType());
   
return url;

My HTML code to handle the Upload of the File

<form action="${signedURL}" method="put" enctype="multipart/form-data">
 
<label>Enter Your User Name</label><br>
<input type="text" name="UserName" ><br><br>
<label>Enter Your Team Name</label><br>
<input type="text" name="TeamName" ><br><br>
<label>Upload Team Logo</label><br>
<input type="file" name="myFile" required="required"><br><br>
<input type="submit" value="Create Team">
<input type="hidden" name="success_action_redirect" value="http://localhost:8080/register">
</form>

After selecting the file and Clicking on Upload, the file I selected is not uploaded to Cloud Storage and it is loading this page ( A white page with a URL). 

Screenshot (2).png

I am not achieving my desired result. What I am missing in my Code ? Java Documentation for Cloud Storage doesn't offer full picture for newbies like me. Someone please help me in this regard.



sami...@google.com

unread,
Oct 26, 2018, 12:48:56 PM10/26/18
to Google App Engine
I am providing you the Github sample for Java to upload Objects to Cloud Storage bucket since you were able to go through the first step of creating an empty object, but just uploading the Object is what you are having issues with. As I can see from the code, you are trying to send requests to the Cloud Storage using the XML API and using application default credential to authenticate, I am providing you this documentation on how to do that. As you are trying to create signed URLs to control access to buckets, I would recommend going through this article which explains how to do it.

I understand that you want to figure out what could be missing in your code, I think StackOverflow Community will be able to provide a better guidance and a probable solution to your inquiries as Google Group is intended for Google Cloud general product discussion purposes only. I would recommend going to Stack Overflow using the proper Cloud Storage tag regarding this issue for further assistance.


Also, if you think that our documentation does not provide enough guidance regarding what you are trying to achieve you can you can report it in Public Issue Tracker. Issue Tracker is a tool used
at Google to track bugs and feature requests during product development, even documentations. So this way we would be able to provide more information in our documents according to your needs.
Reply all
Reply to author
Forward
0 new messages