无法使用GCS客户端库+ java将GAE项目中的文件上传到Google云存储 [英] Not able to upload files from GAE project to Google cloud Storage using GCS Client library+java

查看:159
本文介绍了无法使用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(树林深邃而可爱。);
out.println(但我承诺保持。);
out.flush();
writeChannel.waitForOutstandingWrites();
writeChannel.write(ByteBuffer.wrap(And miles to go before I sleep。。getBytes()));
writeChannel.close();

当我查看日志时,我得到了这样的403错误

 服务器回复403,检查ACL是否在对象和存储桶上正确设置:
请求:POST https://storage.googleapis.com/ <存储桶名称> /<对象名称>
x-goog-resumable:start
x-goog-api-version:2
Content-Type:text / html
x-goog-acl:public-read

没有内容

响应:403与152字节的内容
Content-Type:application / xml; charset = UTF-8
内容长度:152
日期:2013年7月2日星期二14:10:02 GMT
服务器:HTTP上传服务器建立于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>

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

解决方案

我有同样的问题。按照以下说明操作( 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控制台
添加服务帐户作为团队成员的应用程序名称包含到包含该存储桶的
项目中(如果需要写入存储桶,应用程序应该具有编辑权限
。)有关权限
的信息云存储,请参阅范围和权限。如果需要,可以将更多应用程序添加到
项目团队中。


它适用于我。

I am trying to upload image/file to google cloud storage from my GAE application using new Gcs Client Library.

Here is the code snippet

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 into the logs i am getting 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 in fixing this.

解决方案

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

Give permissions to your bucket or objects. To enable your app to create new objects in a bucket, you need to do the following:

Log into the App Engine Admin Console. Click on the application you want to authorize for your Cloud Storage bucket. Click on Application Settings under the Administration section on the left-hand side. Copy the value under Service Account Name. This is the service account name of your application, in the format application-id@appspot.gserviceaccount.com. If you are using an App Engine Premier Account, the service account name 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 app access to a bucket is to use the Google APIs Console to add the service account name of the app as a team member to the project that contains the bucket.(The app should have edit permissions if it needs to write to the bucket.) For information about permissions in Cloud Storage, see Scopes and Permissions. Add more apps to the project team if desired.

It works for me.

这篇关于无法使用GCS客户端库+ java将GAE项目中的文件上传到Google云存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆