3

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

这是代码片段

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>

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

4

1 回答 1

6

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

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

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

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

这个对我有用。

于 2013-09-19T01:44:20.170 回答