2017-02-12 26 views
0

我正在製作一個應用程序,用戶可以在其中上傳一些照片,以便其他人可以看到它們。由於其中一些可能有點大,我需要生成較小的圖像以預覽內容。如何使用GCS代替Blobstore在GAE中生成縮略圖?

我已經在GCS上傳圖片,與形式的URL:「https://storage.googleapis.com/ ......」,但是從我可以在Images API docs看到,它採用了blobstore,這我不使用(它被取代)。如何從gcs鏈接提供縮略圖以避免讓用戶加載完整圖像?我真的很感激任何代碼示例。

UPDATE:

我試圖用例如使用與文件名images.Image我的應用程序的image複製的建議,但它給了我一個TransformationError,如果我不嘗試任何轉換的NotImageError:

def get(self): 

    teststr ='/gs/staging.trn-test2.appspot.com/TestContainer/Barcos-2017-02-12-145657.jpg' 
    img = images.Image(filename=teststr) 

    img.resize(width=80, height=100) 
    thumbnail = img.execute_transforms(output_encoding=images.JPEG) 
    self.response.headers['Content-Type'] = 'image/jpeg' 
    self.response.out.write(thumbnail) 

我錯過了什麼?

+0

這些錯誤通常會提示損壞/丟失的圖像。但代碼看起來不錯,文件是可見的,我沒有建議... –

+0

正如你可以在我的文章(圖片鏈接)中看到的圖像是在那裏,它顯示沒有任何問題。這裏再次鏈接:https://storage.googleapis.com/staging.trn-test2.appspot.com/TestContainer/Barcos-2017-02-12-145657.jpg所以我不認爲它的遺失或損壞。它還能是什麼? –

回答

1

通常,您可以使用Blobstore API,但使用GCS作爲基礎存儲而不是Blobstore,請參閱Using the Blobstore API with Google Cloud Storage。恕我直言只是存儲被取代,而不是API本身:

注意:您應該考慮使用Google Cloud Storage而不是Blobstore來存儲blob數據。

尤其對於Image類(從您的鏈接),你可以使用filename可選的構造函數的參數,而不是blob_key一個(觸發上述Blob存儲API + GCS使用引擎蓋下):

filename: String of the the file name of a Google Storage file that 
      contains the image data. Must be in the format 
      `/gs/bucket_name/object_name`. 

從它__init__()功能:

if filename: 
    self._blob_key = blobstore.create_gs_key(filename) 
else: 
    self._blob_key = _extract_blob_key(blob_key) 
+0

我認爲它根本不需要,但我想這很好,我仍然可以使用該API。我仍然在嘗試使用它時遇到了錯誤,就像剛纔所說的那樣,我會更新問題。 –