【问题标题】:Storing images from server in GAE blobstore?将来自服务器的图像存储在 GAE blobstore 中?
【发布时间】:2015-01-27 13:42:04
【问题描述】:

我想将从 api 获得的文件和图像存储在 blobstore 中(或者更确切地说,以便可以从 blobstore api 访问它们)。由于 file-api 已被弃用,我该怎么做?

【问题讨论】:

  • What have you tried so far? - 你有没有试过询问任何 GAE 官方支持渠道?
  • 我也有同样的话题,从许多建议来看,建议使用 GCS 来存储图像而不是 Datastore 或 Blobstore。

标签: python google-app-engine blobstore


【解决方案1】:

一种方法是将图像存储在 Cloud Storage (gcs) 中并通过 blogstore api 访问它们。基本上你调用gcs.open() 并写入文件。然后,当您需要使用 blobstore api 时,您调用 blobkey = blobstore.create_gs_key()。有了它,您可以执行诸如使用图像 api 调用 images.get_serving_url(blobkey, secure_url=False) 之类的操作。

您如何做到这一点取决于您的具体目标是什么。我用来在我上传的画廊中提供图像。为此,我在前端的 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)

或者通过获取前面提到的 blocky 来使用 Blobstore API:

blobkey = blobstore.create_gs_key()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-06
    • 1970-01-01
    相关资源
    最近更新 更多