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()
을 그리고 :
감사 –
여전히 양동이에 표시에는 개체가 없습니다. 나는 오브젝트를 생성하기 위해 GCS에서 201을 얻고 PUT 콜을 위해 200을 얻었습니다. 그렇기 때문에 성공하지 못했습니까? 또한 이것은 dev_appserver.py 서버에서 실행 중입니다 –
버킷에 대한 옵션을 추가하기 위해 편집 한 다음 쓰기에'with' 문을 사용합니다. 양식 입력을 확인할 수 있으며 jpg 이미지를 가리키는 적절한 URL입니까? ('content_type = content_type'을 설정하는 것이 가장 좋으며, 내 대답의 두 번째 부분에서와 같이 프로그래밍 방식으로 content_type을 얻습니다.) – GAEfan