将Blobstore移动到GCS:Google App Engine Python [英] Moving Blobstore to GCS: Google App Engine Python

查看:121
本文介绍了将Blobstore移动到GCS:Google App Engine Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  from google.appengine .api导入文件
$ b $ def save_data_to_blob_store(data):
#创建文件
file_name = files.blobstore.create(mime_type ='application / octet-stream')

#打开文件并写入文件
用files.open(file_name,'a')作为f:
f.write(data)

#完成文件。在尝试阅读之前先做这件事。
files.finalize(file_name)

#获取文件的blob键
blob_key = files.blobstore.get_blob_key(file_name)
return str(blob_key)

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

  def save_data_to_gcs(请求):
file_name ='/ gs / bucket-name-1 / new_file'#改变桶/对象名称以满足你的需求
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!\\\
')
f.write('This is Google云端存储对象!\ n')
files.finalize(writable_file_name)

在执行此过程时,GAE将生产中的错误引发为:

 请求中的异常:
Traceback(最近一次调用最后):
文件/base/data/home/apps/s~bfsolu/248.371810019093562707/common/zip-packages/django-1.1.zip/django/core/handlers/base.py,第92行,在get_response
response = callback(request,* callback_arg s,** callback_kwargs)
文件/base/data/home/apps/s~bfsolu/248.371810019093562707/myapp/utils.py,第51行,在save_data_to_gcs
acl ='public-read' )
创建
文件/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/files/gs.py,第326行,返回files._create (_GS_FILESYSTEM,文件名=文件名,params = params)
文件/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/files/file.py,第647行, in _create
_make_call('Create',request,response)
文件/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/files/file.py ,第252行,在_make_call
_raise_app_error(e)
文件/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/files/file.py ,第210行,在_raise_app_error
中引发PermissionDeniedError(e)
PermissionDeniedError:ApplicationError:8



任何人都可以帮助合作请在 save_data_to_gcs 过程中纠正我的错误,谢谢。

谢谢。

解决方案

您需要激活您的项目对于GCS。 然后授予您的应用在 Google云端控制台为此,请选择您的存储桶,点击存储桶权限按钮,然后添加新的用户权限,应用程序电子邮件地址(app-id@appspot.gserviceaccount.com)并将其设为所有者。


Here is my existing py code for data uploading to blobstore.

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)

Now i'm trying to deprecate blobstore & moving to GCS. I wrote some code but not working as expected.

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)

While executing this procedure, GAE throwing an error on production as,

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

Any one could help to correct me in save_data_to_gcs procedure, Would appreciate.

Thanks.

解决方案

You will need to activate your project for GCS.

Then grant your app permission to use your bucket in Google Cloud Console:

To do that, select your bucket, click the 'Bucket Permissions' button, and add new 'User' permissions, providing your app email address (app-id@appspot.gserviceaccount.com) and making it an owner.

这篇关于将Blobstore移动到GCS:Google App Engine Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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