无法使用GCS客户端库+ java将文件从GAE项目上传到Google云存储

时间:2013-07-02 14:14:20

标签: java google-app-engine google-cloud-storage

我正在尝试使用新的Gcs客户端库从我的GAE应用程序将图像/文件上传到Google云端存储。

以下是代码段

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();

当我查看日志时,我收到403错误,如此

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>

有人可以帮我解决这个问题。

1 个答案:

答案 0 :(得分:6)

我有同样的问题。按照以下说明(https://developers.google.com/appengine/docs/java/googlestorage/#Java_Prerequisites

  

为您的桶或对象授予权限。启用您的应用   在存储桶中创建新对象,您需要执行以下操作:

     

登录App Engine管理控制台。点击您的应用程序   想要授权您的云存储桶。单击应用程序   左侧“管理”部分下的设置。复制   服务帐户名称下的值。这是服务帐户名称   您的应用程序,格式   application-id@appspot.gserviceaccount.com。如果您使用的是应用程序   Engine Premier帐户,您的应用程序的服务帐户名称   是格式   application-id.example.com@appspot.gserviceaccount.com。

     

使用以下方法授予访问权限:最简单的方法   授予应用程序访问存储区的权限是使用Google API控制台   将应用程序的服务帐户名称添加为团队成员   包含存储桶的项目。(该应用应具有编辑权限   如果需要写入存储桶。)有关权限的信息   在云存储中,请参阅范围和权限。添加更多应用程序   如果需要,项目团队。

它对我有用。

相关问题