2015-01-27 6 views
0

blobstore에있는 API에서 가져온 파일과 이미지를 저장하려고합니다 (또는 blobstore API에서 액세스 할 수 있도록). file-api는 더 이상 사용되지 않으므로 어떻게해야합니까?GAE blobstore에 서버의 이미지를 저장 하시겠습니까?

+0

[지금까지 시도한 내용은 무엇입니까?] (http://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) - 시도해 보셨습니까? GAE 공식 지원 채널에 문의 하시겠습니까? –

+0

나는 많은 제안에서 Datastore 또는 Blobstore보다는 이미지를 저장하기 위해 GCS를 사용하는 것이 좋습니다. – hyip

답변

1

하나의 방법은 이미지를 Cloud Storage (gcs)에 저장하고 blogstore API를 통해 액세스하는 것입니다. 기본적으로 gcs.open()으로 전화하여 파일을 작성하십시오. 그런 다음 blobstore API를 사용해야 할 때 blobkey = blobstore.create_gs_key()이라고 부릅니다. 이를 통해 images.get_serving_url(blobkey, secure_url=False)과 같은 이미지 API를 사용할 수 있습니다.

당신이하는 일은 당신이 어떤 특정한 목표에 달려 있는지에 달려 있습니다. 내가 업로드 한 갤러리에서 이미지를 제공하는 데 사용하고 있습니다. 그렇게하기 위해 파일을 보내는 프론트 엔드의 html 폼에 파일 업로드가 있습니다. 나는이 일을하고있는 백엔드 (이건 그냥 넓은 스트로크입니다) :

# inside the webapp2.RequestHandler get method: 
import mimetypes 
file_data = self.request.get("photoUpload", default_value = None) 
filename = self.request.POST["photoUpload"].filename 
folder = "someFolderName" 
content_type = mimetypes.guess_type(self.filename)[0] 

그런 다음 GCS에 파일 데이터를 저장 :

from google.appengine.api import app_identity 
import cloudstorage as gcs 

# gcs_filename must be unique so I'm using bucket/folder/file 
# it would be smart to check uniqueness before proceeding 
gcs_filename = '/%s%s/%s' % (bucket or app_identity.get_default_gcs_bucket_name(), folder, filename) 
gcs.open(gcs_filename, 'w', content_type=content_type or b'binary/octet-stream', options={b'x-goog-acl': b'public-read'}) as f: 
      f.write(file_data) 

이 지금은 같은 호출로 GCS API를 사용하여 액세스 할 수 있습니다 :

gcs.delete(gcs_filename) 

또는 이전 고르지 언급 얻어서 Blob 저장소 API를 사용

blobkey = blobstore.create_gs_key() 
관련 문제