Unable to upload files from GAE project to Google Cloud. Storage using the GCS + java client library

I am trying to upload an image / file to Google Cloud Storage from a GAE application using the new Gcs client library.

Here is a snippet of code

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 in the logs I get a 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 with this.

+3
source share
1 answer

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

Give permission for your bucket or objects. To enable the application, create new objects in the bucket, you need to do the following:

Log in to the App Engine admin console. Click on the application that you want to allow the use of your cloud storage. Click on the "Application" Settings in the "Administration" section on the left. copy value under service account name. This is the name of the service account of your application, in the format application-id@appspot.gserviceaccount.com. If you are using the Engine Premier account application, the name of the service account 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 application access to the bucket is to use the Google APIs Console to add the service account name as a team member to the project containing the bucket. (The application must have edit permissions if you need to write it in a bucket.) For information about permissions in the cloud storage, see Areas and Permissions. Adding applications in if required.

This works for me.

+6
source

Source: https://habr.com/ru/post/1485876/


All Articles