昨天我收到一条来自Google的消息,说Files API将在7月28日被禁用,建议迁移到Google Cloud Storage。
目前我使用Files API的方式如下-一旦收到电子邮件,我将其附件(仅限图像)保存到blobstore -
from google.appengine.api import files
bs_file = files.blobstore.create(mime_type=ctype, _blobinfo_uploaded_filename='screenshot_'+image_file_name)
try:
with files.open(bs_file, 'a') as f:
f.write(image_file)
files.finalize(bs_file)
blob_key = files.blobstore.get_blob_key(bs_file)稍后,我访问blobstore并将相同的图像附加到我发送的另一封邮件中:
attachments = []
for at_blob_key in message.attachments:
blob_reader = blobstore.BlobReader(at_blob_key)
blob_info = blobstore.BlobInfo.get(at_blob_key)
if blob_reader and blob_info:
filename = blob_info.filename
attachments.append((filename, blob_reader.read()))
if len(attachments) > 0:
email.attachments = attachments
email.send()现在,我应该使用Google Cloud Storage而不是Blobstore。Google Cloud Storage不是免费的,所以我必须启用计费。目前我的Blobstore存储的数据是0.27 to,这是很小的,所以看起来我不需要支付很多钱。但我不敢启用计费,因为我的代码的其他部分可能会导致巨大的账单(似乎没有办法只为Google Cloud Storage启用计费)。
那么,在我的案例中,有没有办法继续使用Blobstore进行文件存储?除了Google Cloud Storage,我还可以免费使用什么( Google Drive是什么)?
发布于 2015-05-20 20:08:05
以下示例使用GCS默认存储桶来存储您的截图。默认存储桶有免费配额。
from google.appengine.api import app_identity
import cloudstorage as gcs
default_bucket = app_identity.get_default_gcs_bucket_name()
image_file_name = datetime.datetime.utcnow().strftime('%Y%m%d%H%M%S') + '_' + image_file_name # GCS filename should be unique
gcs_filename = '/%s/screenshot_%s' % (default_bucket, image_file_name)
with gcs.open(gcs_filename, 'w', content_type=ctype) as f:
f.write(image_file)
blob_key = blobstore.create_gs_key('/gs' + gcs_filename)
blob_key = blobstore.BlobKey(blob_key) # if should be stored in NDBhttps://stackoverflow.com/questions/30343330
复制相似问题