【问题标题】:Not able to upload files from GAE project to Google cloud Storage using GCS Client library+java无法使用 GCS 客户端库+java 将文件从 GAE 项目上传到 Google 云存储
【发布时间】:2013-07-02 14:14:20
【问题描述】:

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

这里是代码sn-p

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>

谁能帮我解决这个问题。

【问题讨论】:

  • 您是否已将您的应用引擎服务帐户添加到 BUCKETNAME 的权限中?有关如何执行此操作的说明,请参阅 developers.google.com/appengine/docs/java/googlestorage/…
  • 非常感谢,它确实帮助我解决了这个问题。
  • 我还有一个小问题。现在,我可以成功上传图片了,当我使用商店管理器查看云存储时,我可以看到“公开分享”没有被选中。我将 acl 设置为“公开阅读”。你能帮我解决这个问题吗?
  • 您是否设置了存储桶 acl 而不是对象 acl?经理显示完整的 acl 是什么?
  • 我正在尝试仅通过此语句设置 acl "GcsFileOptions options = new GcsFileOptions.Builder().mimeType("text/html").acl("public-read").build() ;"如何为对象设置acl。

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


【解决方案1】:

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

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

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

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

它对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    • 2015-05-30
    相关资源
    最近更新 更多