2013-11-22 18 views
5

ここに、ブロブストアにデータをアップロードするためのPyコードがあります。BlobstoreをGCSに移動する:Google App Engine Python

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) 

は、今私は、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) 

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 

、GAEのように生産上のエラーを投げ、この手順の実行中の任意の一つは、save_data_to_gcs手順で私を修正するのに役立つ可能性がありお願い申し上げます。

ありがとうございました。

+1

GCSを有効化しましたか? Google Cloud Consoleでは、アプリにバケットへのアクセスを許可しましたか?これを行うには、バケットを選択し、[Bucket Permissions]をクリックし、新しい「ユーザー」アクセス許可を追加して、アプリの電子メールアドレス([email protected])を提供し、所有者にします。 –

+0

@ MarkSimpson:ありがとうバディ..それは魅力のように動作します.. 私はもう一つの賛成が必要です、どのようにファイルのレコードのユニークなキーを取得するには? –

+0

OK、私は以下の答えに私のコメントを変えました。それを正しいものとして受け入れてください。新しい質問を別に投稿してください。この問題とは無関係です。ありがとう。 –

答えて

4

GCSの場合、activate your projectが必要です。

その後Google Cloud Consoleであなたのバケツを使用するようにアプリの権限を付与します。

、あなたのバケツを選択し、それを行う「バケツ権限」ボタンをクリックして、アプリのメールアドレスを提供する、新しい「ユーザーの許可を追加するには( [email protected])を作成し、それを所有者にします。

0

ファイルapiが2015年9月に停止されたため、これはもう機能しません。

関連する問題