2017-02-12 84 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。我仍然在尝试使用它时遇到了错误,就像刚才所说的那样,我会更新问题。 –