Help migrating from Files API

76 views
Skip to first unread message

Leon Prouger

unread,
Jul 25, 2015, 8:44:28 AM7/25/15
to Google App Engine
Hey folks, I'm learning the new Cloud Storage and question how to I migrate my app with a least friction.

Now I'm using this sample piece of code to save images:

from google.appengine.api import images, files


def upload(ext, stream):

    file_name = files.blobstore.create(mime_type=mimetypes.types_map[ext])


    with files.open(file_name, 'a') as f:

        f.write(stream.getvalue())


    files.finalize(file_name)

    return images.get_serving_url(files.blobstore.get_blob_key(file_name))


Which I'm thinking to change to the next:


def upload(ext, stream):

    filename = "/images/my_file"

    with gcs.open(filename, 'w') as f:

        f.write(stream.getvalue())


    # Blobstore API requires extra /gs to distinguish against blobstore files.

    blobstore_filename = '/gs' + filename

    # This blob_key works with blobstore APIs that do not expect a

    # corresponding BlobInfo in datastore.

    blob_key = blobstore.create_gs_key(blobstore_filename)

    return images.get_serving_url(blob_key)


Which seems to work.

The problem that in the old version I don't have to worry about filenames, Files API job was to assign me one. Now I need to give a name myself. Any way the new api should do it for me? Or maybe I should just hash the file. 


Alex Martelli

unread,
Jul 26, 2015, 12:43:50 PM7/26/15
to google-a...@googlegroups.com
To generate a unique filename, I recommend the technique suggested at http://stackoverflow.com/questions/22156030/google-cloud-storage-create-file-name-automatically .

However, on an unrelated note, take care: your code seems to be missing the content_type named parameter to the open call, and you'll probably want to add it, as you did in the previous version in the create call.

Alex

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-appengi...@googlegroups.com.
To post to this group, send email to google-a...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-appengine.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-appengine/145ce87c-e299-4d7f-8c8c-83f2959b5548%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Leon Prouger

unread,
Aug 2, 2015, 4:04:33 PM8/2/15
to Google App Engine
Thanks for answering, it should solve my problem. I wonder if it's safer to use uuid1 on GAE, but the collision chance of uuid4 seems small enough for my case.

Also I added content type as you advised 

Carlos Lallana

unread,
Aug 3, 2015, 4:40:03 AM8/3/15
to Google App Engine
As a quick response:

uuid1() may compromise privacy since it creates a UUID containing the computer’s network address. 
uuid4() creates a random UUID.

Refer to this answer from Stackoverflow for further details.
Reply all
Reply to author
Forward
0 new messages