2

저는 (1) blobstore에 JPG를 업로드하는 GAE 페이지를 파이썬으로 작성했습니다. 그 부분은 작동합니다. 이제는 (2) I 'm Feeling Lucky Image 변형을 수행 한 다음 (3) BLOBstore에 다른 BLOB로 저장해야합니다. 이상적으로는 동일한 업로드 처리기에서 (1), (2) 및 (3)을하고 싶습니다.UploadHandler 내에서 이미지 변환을 수행하고 Blobstore에 저장

여기 코드를 수행했지만 (1) 및 (2) 만 수행합니다. https://developers.google.com/appengine/docs/python/images/#Python_Transforming_images_from_the_Blobstore

내가 찾을 수있는 가장 가까운 SO를 통해 보면서 한이 있습니다 : Storing Filtered images on the blobstore in GAE

그것은 다음 Blob 저장소에 파일을 업로드 (파일 API를 사용하여) 파일로 변환 저장합니다. 그러나 파일 API를 사용하며 다음에 따라 Files API가 사용되지 않습니다. 내 모델에서 https://developers.google.com/appengine/docs/python/blobstore/#Python_Writing_files_to_the_Blobstore

, 나는 Blob 저장소에있는 이미지에 대한 참조를 저장하는 BlobKeyProperty이

class ImageModel(ndb.Model): 
    imagetoserve = ndb.BlobKeyProperty(indexed=False) 

여기에 지금까지 업로드 처리기 코드입니다 :

from google.appengine.ext import blobstore 
from google.appengine.ext.webapp import blobstore_handlers 
from google.appengine.api.images import get_serving_url 
from google.appengine.api import images 

upload_files = self.get_uploads('imgfile') # 'file' is file upload field in the form 
blob_info = upload_files[0] 

imgtmp = images.Image(blob_info) 
imgtmp.im_feeling_lucky() 
img = ImageModel() 
img.imagetoserve = imgtmp 
img.put() 

내 문제가됩니다 이 줄 :

img.imagetoserve = imgtmp 

모델 i 블로 키 프로퍼티는 있지만 이미지를 제공하고있어 유형 불일치 오류가 발생합니다. 변환 된 imgtmp를 blobstore에 업로드하고, blobkey를 캡처하고, 참조를 내 모델에 저장하는 단계는 어떻게 수행합니까?

답변

1

불행히도 Blobstore API Documentation

을 읽고, 당신은 전통적으로 파일 API를 통해이 작업을 수행 한 것입니다,하지만 그들은 GCS를 사용하여 찬성, 당신은 다음과 같은 작업을 수행 할 수 있음을 비하하고 있기 때문에 (누락 된 부분에 입력 할 수 있습니다) : (this example부터)

import cloudstorage as gcs 
from google.appengine.ext import blobstore 

class ImageModel(ndb.Model): 
    image_filename = ndb.StringProperty(indexed=False) 

    @property 
    def imagetoserve(self): 
     return blobstore.create_gs_key(self.image_filename) 

BUCKET = "bucket_to_store_image\\" 

with gcs.open(BUCKET + blob_info.filename, 'w', content_type='image/png') as f: 
    f.write(imgtmp.execute_transforms()) 

img = ImageModel() 
img.image_filename = BUCKET + blob_info.filename 
img.put()