1
votes

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
You will be able to use the blobstore but not the files API. At least that's the way I understand it. So you could change to using a class that has a blobstore.BlobReferenceProperty() and save it like that. - Niklas R.
@Niklasinstockholm, the question then is how to save file to blobstore (currently it is done with Files API usage). - LA_
btw, by enabling billing, you aren't going to receive a "huge bill". your app already fits free quota, so it will stay free for free parts, and you'll be charged only for extra services - Igor Artamonov
@IgorArtamonov, partially agree, but with increased users activity, some changes done in the code, I could receive a huge bill once billing is enabled (and I can not receive it now, since Over Quota exception happens and that's it). - LA_
After you enable billing, you can set a maximum daily budget. Set it to $1 and it is close enough to free that you don't have to worry about it. - gaefan

1 Answers

2
votes

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