0

Google App Engineで動作しているFlask Webアプリケーションがあります。このアプリは、私のユーザーがイメージリンクを提供するために使用するフォームを持っています。リンクから画像データをダウンロードし、Google Cloud Storageバケットにアップロードします。画像データをダウンロードしてからGoogle Cloud Storageにアップロード

私がこれまで見つけたのは、Googleのドキュメントでは、「gcs」としてインストールしてインポートした「cloudstorage」クライアントライブラリを使用するように指示しています。
ここをクリックしてください:https://cloud.google.com/appengine/docs/python/googlecloudstorageclient/read-write-to-cloud-storage

私は、要求によって画像データを正しく処理していないと思います。私はCloud Storageのアップロードコールから200のコードを返しますが、コンソールでそれを探すときにはオブジェクトはありません。私は画像を取得し、それをアップロードしようのはここです:

img_resp = requests.get(image_link, stream=True) 
objectName = '/myBucket/testObject.jpg' 

gcs_file = gcs.open(objectName, 
        'w', 
        content_type='image/jpeg') 
gcs_file.write(img_resp) 
gcs_file.close() 

編集:ここでは は答えの提案を反映するために私の更新されたコードは次のとおりです。

image_url = urlopen(url) 
content_type = image_url.headers['Content-Type'] 
img_bytes = image_url.read() 
image_url.close() 

filename = bucketName + objectName 
options = {'x-goog-acl': 'public-read', 
      'Cache-Control': 'private, max-age=0, no-transform'} 

with gcs.open(filename, 
       'w', 
       content_type=content_type, 
       options=options) as f: 
    f.write(img_bytes) 
    f.close() 

しかし、私はまだ201を取得していますPOST(ファイル作成)コールで応答し、PUT呼び出しで200を返しますが、オブジェクトはコンソールに表示されません。彼らは単にURLを入力して制限する理由は、

from google.appengine.api import images 
import urllib2 

image = urllib2.urlopen(image_url) 

img_resp = image.read() 
image.close() 

objectName = '/myBucket/testObject.jpg' 
options = {'x-goog-acl': 'public-read', 
     'Cache-Control': 'private, max-age=0, no-transform'} 

with gcs.open(objectName, 
       'w', 
       content_type='image/jpeg', 
       options=options) as f: 

    f.write(img_resp) 
    f.close() 

をそして:

答えて

1

これを試してみてください。

if isinstance(image_or_url, basestring): # should be url 
    if not image_or_url.startswith('http'): 
     image_or_url = ''.join([ 'http://', image_or_url]) 
    image = urllib2.urlopen(image_url) 
    content_type = image.headers['Content-Type'] 
    img_resp = image.read() 
    image.close() 
else: 
    img_resp = image_or_url.read() 
    content_type = image_or_url.content_type 

開発サーバーで実行している場合、ファイルはローカルデータストアにアップロードされます。でそれを確認します、私はこれを試してみます

http://localhost:<your admin port number>/datastore?kind=__GsFileInfo__ 

http://localhost:<your admin port number>/datastore?kind=__BlobInfo__ 
+0

感謝を –

+0

まだバケツに現れて何のオブジェクトはありません。私はオブジェクトの呼び出しを作成するためにGCSから201を取得しています。そして、PUT呼び出しのために200を取得していますので、成功したとは限りません。また、これはdev_appserver.pyサーバで実行されています –

+0

バケットのオプションを追加するように編集しました。書き込みに 'with'文を使用しました。フォームの入力を確認できますか?それはjpgイメージを指す正しいURLですか? ( 'content_type = content_type'を設定するのが最善で、私の答えの2番目の部分のようにプログラムでcontent_typeを取得してください) – GAEfan

関連する問題