How to migrate from The Files API to Google Cloud

2019-08-05 18:12发布

问题:

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