简体   繁体   中英

Not able to upload files from GAE project to Google cloud Storage using GCS Client library+java

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.

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 application-id@appspot.gserviceaccount.com. If you are using an App Engine Premier Account, the service account name for your application is in the format application-id.example.com@appspot.gserviceaccount.com.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM