How to migrate from The Files API to Google Cloud Storage?

谁说我不能喝 提交于 2019-12-24 12:23:02

问题


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)?


回答1:


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


来源:https://stackoverflow.com/questions/30343330/how-to-migrate-from-the-files-api-to-google-cloud-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!