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 On Sat, Jul 25, 2015 at 5:44 AM, Leon Prouger <[email protected]> wrote: > 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. > > > -- > 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 [email protected]. > To post to this group, send email to [email protected]. > 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 > <https://groups.google.com/d/msgid/google-appengine/145ce87c-e299-4d7f-8c8c-83f2959b5548%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- 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 [email protected]. To post to this group, send email to [email protected]. 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/CAE46Be9e6BtR-1pyrKvE_gmkY_q1QmUxDc5gd4rniGWSLMSrLQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
