3
votes

I am trying to upload image/file to google cloud storage from my GAE application using new Gcs Client Library.

Here is the code snippet

GcsService gcsService = GcsServiceFactory.createGcsService(new RetryParams.Builder()
          .initialRetryDelayMillis(10)
          .retryMaxAttempts(10)
          .totalRetryPeriodMillis(15000)
          .build());
GcsFilename filename = new GcsFilename(BUCKETNAME, FILENAME);
GcsFileOptions options = new GcsFileOptions.Builder().mimeType("text/html").acl("public-read").build();
GcsOutputChannel writeChannel = gcsService.createOrReplace(filename,options);           
PrintWriter out = new PrintWriter(Channels.newWriter(writeChannel, "UTF8"));
out.println("The woods are lovely dark and deep.");
out.println("But I have promises to keep.");
out.flush();
writeChannel.waitForOutstandingWrites();
writeChannel.write(ByteBuffer.wrap("And miles to go before I sleep.".getBytes()));
writeChannel.close();

When i look into the logs i am getting 403 error like this

Server replied with 403, check that ACLs are set correctly on the object and bucket:
Request: POST https://storage.googleapis.com/<bucket name>/<object name>
x-goog-resumable: start
x-goog-api-version: 2
Content-Type: text/html
x-goog-acl: public-read

no content

Response: 403 with 152 bytes of content
Content-Type: application/xml; charset=UTF-8
Content-Length: 152
Date: Tue, 02 Jul 2013 14:10:02 GMT
Server: HTTP Upload Server Built on Jun 28 2013 13:27:54 (1372451274)
X-Google-Cache-Control: remote-fetch
Via: HTTP/1.1 GWA
<?xml version='1.0' encoding='UTF-8'?><Error><Code>AccessDenied</Code><Message>Access denied.</Message><Details>images2.solestruck.com</Details></Error>

Can someone help me in fixing this.

1
Have you added your app engine service account to BUCKETNAME's permissions? See developers.google.com/appengine/docs/java/googlestorage/… for instructions on how to do this.Brandon Yarbrough
Thanks alot, it really helped me to fix this issue.user2502452
I have one more small issue. Now, i could able to upload images successfully, when i look into the cloud storage using Store manager, i could see the "Share Publicly" is not checked. I am setting acl as "public-read". Could you plz help me in this.user2502452
Is it possible you've set the bucket acl and not the object acl? What does the manager show the full acl as being?Brandon Yarbrough
I am trying to set acl only through this statement "GcsFileOptions options = new GcsFileOptions.Builder().mimeType("text/html").acl("public-read").build();" how to set acl for the objects.user2502452

1 Answers

6
votes

I have the same problem. Follow instructions below (https://developers.google.com/appengine/docs/java/googlestorage/#Java_Prerequisites)

Give permissions to your bucket or objects. To enable your app to create new objects in a bucket, you need to do the following:

Log into the App Engine Admin Console. Click on the application you want to authorize for your Cloud Storage bucket. Click on Application Settings under the Administration section on the left-hand side. Copy the value under Service Account Name. This is the service account name of your application, in the format [email protected]. If you are using an App Engine Premier Account, the service account name for your application is in the format [email protected].

Grant access permissions using the following methods: The easiest way to grant app access to a bucket is to use the Google APIs Console to add the service account name of the app as a team member to the project that contains the bucket.(The app should have edit permissions if it needs to write to the bucket.) For information about permissions in Cloud Storage, see Scopes and Permissions. Add more apps to the project team if desired.

It works for me.