How could erase file from cloudstore with the url from file?

84 views
Skip to first unread message

Juan Antonio Fernández Sánchez

unread,
Apr 16, 2016, 5:22:07 PM4/16/16
to Google App Engine
I'm saving images in cloudstore (blobstore) using 'cloudstorage' library in python.

Well, this images are the image profile from  users and I get the url from blobstore to save this with the rest of data of user.
And now I' m trying develop the way to erase this files using the same url that I have saved in the data block of users, but I dont find a simple way to do this with the 'cloudstorage' library.

I could save the filename of the photos of users in DB togheter to url to access faster, but I prefer know how I can could make this.

Thanks you so much for help.

Code example that I want more o less:

def DeleteFile(self, url):
key = keyFromUrl(url)
gcs.delete(key)

Adam (Cloud Platform Support)

unread,
Apr 16, 2016, 6:56:34 PM4/16/16
to Google App Engine
First off, Google Cloud Storage is not Blobstore. You can create a key for use with Blobstore with blobstore.create_gs_key(), but you should only do this if you really need to use Blobstore API for something (eg. with the Images API). You should not treat Blobstore and GCS as the same thing or intermingle them needlessly.

So adding to that, GCS doesn't understand or care about blob keys, so gcs.delete(key) won't work. Objects that are uploaded to Blobstore don't necessarily exist in GCS, so there isn't a way to get a GCS file name from a blob key*. You need to use the GCS file path directly, eg. gcs.delete('/mybucket/path/to/myobject').

So to avoid this type of problem you should always use 'cloudstorage' and deal with objects in terms of GCS files, using blobstore.create_gs_key() only when you need to use some function that needs the Blobstore API. You can make Blobstore talk to GCS, but not the other way around.

* The filename you get from BlobInfo is the filename from the user's machine when they uploaded the blob, not the filename in GCS.

Juan Antonio Fernández Sánchez

unread,
Apr 18, 2016, 4:46:15 AM4/18/16
to Google App Engine
Hi Adam,

Maybe I'm confusing the options of store data on the Google Infrastructure.
I want store the profile photos to my users in my GAE app, and finding way to do this I found Google Cloud Storage, using the library cloudstorage for python but also saw blobstore and gcloud and I'm a bit confusing.
When I loaded a photo with gcs.open I can see this in the dev server in the section BlobStore Viewer, because of this I thought that blobstore and GCS is more or less the same thing.

When, when I make this:

gcs_file = gcs.open(filename, 'w', ...)       
gcs_file.write(datos)       
gcs_file.close()

After I can see in Blobstore Viewer in dev_server web console a new file, and If I use this code:
       
        blobstore_filename = '/gs' + filename

        key = blobstore.create_gs_key(blobstore_filename)
        return get_serving_url(key)

I can get the url where the image is stored to use to show it wherever i want.

For this reason, I thought that was easy way to obtain from url that I have the way to erase que photo or the file that this url refers.

Thanks you very much for all your help Adam.

Adam (Cloud Platform Support)

unread,
Apr 18, 2016, 5:15:28 PM4/18/16
to Google App Engine
No problem. Yes, the development server stores blobs and GCS files in the same temporary directory on your machine, and both are visible in the Blobstore Viewer from the dev server admin console. This is somewhat confusing, because in the production console there is no Blobstore Viewer, only the Cloud Storage browser, and anything that is only written to the Blobstore is not visible.

I will add that there is exactly one way to get the GCS filename of an uploaded blob, but it can only be done inside the handler that you pass to blobstore.create_upload_url(), using blobstore.parse_file_info().The FileInfo object returned contains a field 'gs_object_name' which is the name of the file written to GCS, or None if it was only written to the Blobstore. There's a full example which demonstrates how this works which has been uploaded by a user on GitHub. 

Juan Antonio Fernández Sánchez

unread,
Apr 25, 2016, 6:32:26 AM4/25/16
to Google App Engine
Thanks for all info, Adam,

I'm using now the Cloud Storage service to upload images files. And  I only save the url in the data block user to show the photo. But now I want can delete the image of user using the url that I have. In dev-server I can do this extracting the key of file from the url but i dont know how do this in the Cloud Storage. Maybe I should save the name of file beside the url, and when I need erase the photo, I use the name of file instead of the url only.
What do you think about?

Adam (Cloud Platform Support)

unread,
Apr 26, 2016, 2:37:21 PM4/26/16
to Google App Engine
You should store the GCS file name and just use cloudstorage.delete('/bucket/object_name'). GCS functions can only work with GCS file names. I'm not what you mean by 'only save the url in the data block user to show the photo' though, so if you could expand on this or provide some code it would be helpful (eg. do you mean the upload URL, or are you also using Images API to create a serving URL?)

On a side note, you can now access the Blobstore viewer from the new production console under 'Compute' -> 'App Engine' -> 'Blobstore'. It looks like this was added as the old console is about to be turned down. This will only show objects uploaded directly to the Blobstore though and not files in GCS. For these you still need to access 'Storage' -> 'Storage'.
Reply all
Reply to author
Forward
0 new messages