无法使用GCS客户端库+ java将文件从GAE项目上传到Google云存储
我正在尝试使用新的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// 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 AccessDenied
Access denied.images2.solestruck.com
有人可以帮我解决这个问题。
我也有同样的问题。 请按照以下说明操作( https://developers.google.com/appengine/docs/java/googlestorage/#Java_Prerequisites )
为您的存储桶或对象授予权限。 要使您的应用程序能够在存储桶中创建新对象,您需要执行以下操作:
登录App Engine管理控制台。 单击要为云存储桶授权的应用程序。 单击左侧“管理”部分下的“应用程序设置”。 复制服务帐户名称下的值。 这是您的应用程序的服务帐户名称,格式为application-id@appspot.gserviceaccount.com。 如果您使用的是App Engine Premier帐户,则应用程序的服务帐户名称格式为application-id.example.com@appspot.gserviceaccount.com。
使用以下方法授予访问权限:授予应用程序访问存储桶的最简单方法是使用Google API控制台将应用程序的服务帐户名称作为团队成员添加到包含存储桶的项目中。(应用程序应该如果需要写入存储桶,则具有编辑权限。)有关云存储中权限的信息,请参阅范围和权限。 如果需要,可以向项目团队添加更多应用。
这个对我有用。