GAE Python + Google云存储授权问题

Tim*_*rks 4 google-app-engine google-cloud-storage

我正在尝试运行Google云端存储+ Google App Engine(Python)"Hello world"应用.

我按照说明激活了Google云端存储,并在我的App Engine应用控制台中创建了一个存储桶.我编写了一个简单的程序来编写测试文件,但是当我运行它时,云存储库会抛出403,显然是因为我的App Engine应用程序的凭据没有显示,无效或者没有这个桶的权利.

这是代码:

BUCKET = '/pcj-info-testing'

class TestGCS(webapp2.RequestHandler):
  def create_file(self, filename):
    self.response.write("Creating\n");
    gcs_file = gcs.open(filename, 'w', content_type = 'text/plain');
    gcs_file.write('Testing, 1, 2, 3\n')
    gcs_file.write('At ' + datetime.now().isoformat())    
    gcs_file.close()

  def get(self):
    self.response.headers['Content-Type'] = 'text/plain'
    filename = BUCKET + "/demo-file" + str(random.randrange(10**10, 10**11-1))
    self.create_file(filename)
    self.response.write('File stat:\n')
    stat = gcs.stat(filename)
    self.response.write(repr(stat))
Run Code Online (Sandbox Code Playgroud)

这是堆栈跟踪和错误的相关部分:

  File "/base/data/home/apps/s~pcj-info/1.373629132682543570/gcstest.py", line 14, in create_file
    gcs_file = gcs.open(filename, 'w', content_type = 'text/plain');
  File "/base/data/home/apps/s~pcj-info/1.373629132682543570/cloudstorage/cloudstorage_api.py", line 74, in open
    return storage_api.StreamingBuffer(api, filename, content_type, options)
  File "/base/data/home/apps/s~pcj-info/1.373629132682543570/cloudstorage/storage_api.py", line 597, in __init__
    errors.check_status(status, [201], path, headers, resp_headers)
  File "/base/data/home/apps/s~pcj-info/1.373629132682543570/cloudstorage/errors.py", line 106, in check_status
    raise ForbiddenError(msg)
ForbiddenError: Expect status [201] from Google Storage. But got status 403.
Path: '/pcj-info-testing/demo-file16955619179'.
Request headers: {'x-goog-resumable': 'start', 'x-goog-api-version': '2', 'content-type': 'text/plain', 'accept-encoding': 'gzip, *'}.
Response headers: {'alternate-protocol': '443:quic', 'content-length': '146', 'via': 'HTTP/1.1 GWA', 'x-google-cache-control': 'remote-fetch', 'vary': 'Origin', 'server': 'HTTP Upload Server Built on Jan 23 2014 15:07:07 (1390518427)', 'date': 'Sat, 08 Feb 2014 16:49:56 GMT', 'content-type': 'application/xml; charset=UTF-8'}.
Extra info: None.
Run Code Online (Sandbox Code Playgroud)

我已经查看了存储桶上的访问权限,它们看起来是正确的(尽管默认情况下它们应该是正确创建的,根据文档) - 在Google API控制台中列为我的ID 与存储桶中的ID 相同权限,拥有所有者权限.

我可以看到Google可能会在其状态代码文档中返回403的原因,但是我发现,在App Engine中发出调用的urlfetch库并不会返回这些错误名称(我假设它们是文本字符串)在错误代码之后立即在HTTP响应中返回,但是我认为库只给你整数结果.所以我对下一步做什么感到茫然.

是否有任何直接的方法来捕获API返回的错误?如果我知道错误是AccountProblem vs. InvalidSecurity或其他什么,我的故障排除会有很大的不同.令人非常沮丧的是,API以一种在支持的云开发平台上使用推荐库的用户无法访问的方式返回错误代码.

如果我给"所有经过身份验证的用户"访问权限,那就可以了.所以看起来我正在认证为某人,但有人不在权限列表中.添加我在测试应用时登录的Gmail帐户无济于事.(该应用需要管理员登录才能点击所有网址.)

jte*_*ace 5

您需要将appengine服务帐户添加为具有写入权限.

您可以在"服务帐户名称"下的appengine控制台的"应用程序设置"下找到服务帐户电子邮件.