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




[image: 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). 

[image: 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.



-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/google-appengine.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-appengine/fe53cf67-c57d-4ca0-a949-ea86c079aeea%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to