【问题标题】:Moving Blobstore to GCS: Google App Engine Python将 Blobstore 迁移到 GCS:Google App Engine Python
【发布时间】:2013-12-06 23:49:52
【问题描述】:

这是我现有的用于将数据上传到 blobstore 的 py 代码。

from google.appengine.api import files

def save_data_to_blob_store(data):
    # Create the file
    file_name = files.blobstore.create(mime_type='application/octet-stream')

    # Open the file and write to it
    with files.open(file_name, 'a') as f:
        f.write(data)

    # Finalize the file. Do this before attempting to read it.
    files.finalize(file_name)

    # Get the file's blob key
    blob_key = files.blobstore.get_blob_key(file_name)
    return str(blob_key)

现在我正在尝试弃用 blobstore 并迁移到 GCS。我写了一些代码,但没有按预期工作。

def save_data_to_gcs(request):
    file_name = '/gs/bucket-name-1/new_file' # change bucket/object names to suit your needs
    writable_file_name = files.gs.create(file_name, mime_type='application/octet-stream',
                                     acl='public-read')
    with files.open(writable_file_name, 'a') as f:
        f.write('Hello World!\n')
        f.write('This is a Google Cloud Storage object!\n')
    files.finalize(writable_file_name)

在执行此过程时,GAE 在生产中抛出错误,

Exception in request:
Traceback (most recent call last):
  File "/base/data/home/apps/s~bfsolu/248.371810019093562707/common/zip-packages/django-1.1.zip/django/core/handlers/base.py", line 92, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/base/data/home/apps/s~bfsolu/248.371810019093562707/myapp/utils.py", line 51, in save_data_to_gcs
    acl='public-read')
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/files/gs.py", line 326, in create
    return files._create(_GS_FILESYSTEM, filename=filename, params=params)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/files/file.py", line 647, in _create
    _make_call('Create', request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/files/file.py", line 252, in _make_call
    _raise_app_error(e)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/files/file.py", line 210, in _raise_app_error
    raise PermissionDeniedError(e)
 PermissionDeniedError: ApplicationError: 8

任何人都可以在 save_data_to_gcs 程序中帮助纠正我,不胜感激。

谢谢。

【问题讨论】:

  • 您是否激活了 GCS(启用计费等)?在 Google Cloud Console 中,您是否授予您的应用访问您的存储桶的权限?为此,请选择您的存储桶,单击“存储桶权限”,然后添加新的“用户”权限,提供您的应用电子邮件地址 (app-id@appspot.gserviceaccount.com) 并使其成为所有者。
  • @MarkSimpson:谢谢哥们。它就像魅力一样。我还需要一个帮助,如何获得文件记录的唯一密钥?
  • 好的,我已经把我的评论变成了下面的答案。请接受它是正确的。单独发布该新问题。它与这个问题无关。谢谢。

标签: python google-app-engine blobstore google-cloud-storage


【解决方案1】:

由于 files api 已于 2015 年 9 月关闭,因此不再起作用。

【讨论】:

    【解决方案2】:

    您需要 activate your project 获取 GCS。

    然后授予您的应用使用Google Cloud Console中的存储桶的权限:

    为此,请选择您的存储桶,点击“存储桶权限”按钮,然后添加新的“用户”权限,提供您的应用电子邮件地址 (app-id@appspot.gserviceaccount.com) 并使其成为所有者。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-04
      • 2010-11-10
      • 1970-01-01
      相关资源
      最近更新 更多