0

Google AppEngine에서 실행중인 플라스크 웹 앱이 있습니다. 이 앱에는 내 사용자가 이미지 링크를 제공하는 데 사용할 양식이 있습니다. 링크에서 이미지 데이터를 다운로드 한 다음 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'을 설정하는 것이 가장 좋으며, 내 대답의 두 번째 부분에서와 같이 프로그래밍 방식으로 content_type을 얻습니다.) – GAEfan

관련 문제