[英]Moving Blobstore to GCS: Google App Engine Python
這是我現有的用於將數據上傳到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雲端控制台中使用您的存儲分區的權限
為此,請選擇您的存儲桶,單擊“存儲桶權限”按鈕,然后添加新的“用戶”權限,提供您的應用程序電子郵件地址(app-id@appspot.gserviceaccount.com)並將其設為所有者。
由於文件api在2015年9月關閉,因此不再起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.