问题描述
我昨天收到Google发来的一条消息,称7月28日将禁用Files API,并建议迁移到Google Cloud Storage。
我使用以下方法使用Files API - 一旦收到电子邮件,我将它的附件保存到blobstore中 - google.appengine.api导入文件bs_file = files.blobstore.create(mime_type = ctype,_blobinfo_uploaded_filename ='截图_'+ image_file_name)
尝试:
with files.open( bs_file,'a')作为f:
f.write(image_file)
files.finalize(bs_file)
blob_key = files.blobstore.get_blob_key(bs_file)
之后,我访问blobstore并将相同的图像附加到我发送的另一封邮件中:
attachments = []
用于message.attachments中的at_blob_key:
blob_reader = blobstore.BlobReader(at_blob_key)
blob_info = blobstore.BlobInfo。 get(at_blob_key)
如果blob_reader和blob_info:
filename = blob_info.filename
attachments.append((filename,blob_reader.read()))
if len(attachments)> 0:
email.attachments =附件
email.send()
现在,我应该使用Google Cloud Storage而不是Blobstore。 Google云端存储不是免费的,所以我必须启用结算功能。目前我的Blobstore存储数据是0.27Gb,这很小,所以看起来我不需要付出太多。但我害怕启用结算功能,因为我的代码的其他部分可能会产生巨额账单(似乎无法为Google Cloud Storage启用结算功能)。
那么,在我的情况下,有没有办法继续使用Blobstore来存储文件?我还有什么可以免费使用的,而不是Google云端存储(关于Google云端硬盘是什么)?
以下示例使用GCS默认存储区来存储屏幕截图。默认存储桶有免费配额。
from google.appengine.api import app_identity
将cloudstorage导入为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文件名应该是唯一的
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 NDB
I've got a message yesterday from Google saying that the Files API will be disabled on July 28th and it is recommended to migrate to Google Cloud Storage.
Currently I use Files API in the following way - once email is received, I save its attachment (images only) to 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)
Later on, I access blobstore and attach the same images to another mail I send:
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()
Now, I am supposed to use Google Cloud Storage instead of Blobstore. Google Cloud Storage is not free, so I have to enable billing. Currently my Blobstore Stored Data is 0.27Gb, which is small, so looks like I will not have to pay a lot. But I am afraid to enable billing, since some other parts of my code could result in a huge bill (and seems there is no way to enable billing just for Google Cloud Storage).
So, is there any way to continue usage of Blobstore for files storage in my case? What else can I use for free instead of Google Cloud Storage (what is about Google Drive)?
The below example uses the GCS default bucket to store your screenshots. The default bucket has free quota.
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 NDB
这篇关于如何从The Files API迁移到Google Cloud Storage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!