Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete Files from Google Cloud Storage

So I've got a django application up and running on appengine and have it creating files when a user uploads them. The issue I'm having is trying to figure out how to delete them. My code for creating them looks like.

from google.appengine.api import files

file = request.FILES['assets_thumbnail']
filename = '/gs/mybucketname/example.jpg'
writable_file_name = files.gs.create(filename, mime_type='image/jpeg', acl='public-read')
with files.open(writable_file_name, 'a') as f:
    f.write(file.read())
files.finalize(writable_file_name)

This works fine, but in the documentation at:

https://developers.google.com/appengine/docs/python/googlestorage/functions

there isn't a delete method listed. However, if you look at the actual source for google.appengine.api.files at the link below (line 504)

http://code.google.com/p/googleappengine/source/browse/trunk/python/google/appengine/api/files/file.py

There is a delete method that I can call, but I can't figure out for the life of me exactly the argument it wants. I've tried plenty of different combinations with the bucket name and such. It seems to want it to start with /blobstore/ which is different than anything else I've done so far when interacting with Cloud Storage. I've been looking through the underlying blobstore classes that google.appengine.api.files is written on top of, but still can't figure out exactly how to delete items. It looks like I may need to find the BlobKeys for the items I've uploaded. I can delete them fine using the web based bucket manager that google hosts and also the gsutil command line utility that they provide.

Has anyone successfully deleted files from google cloud storage via a python app deployed to app engine? Any clues/thoughts/ideas are greatly appreciated.

like image 260
Matt Toigo Avatar asked May 11 '12 16:05

Matt Toigo


2 Answers

I arrived here looking for a way to empty a google storage directory with python. Is a workaround of the app-engine implementation (more applied to this question), but might be useful in a python script.

from google.cloud import storage
bucket_name = 'my-bucket'
directory_name = 'this/is/my/path/'

client = storage.Client()
bucket = client.get_bucket(bucket_name)
# list all objects in the directory
blobs = bucket.list_blobs(prefix=directory_name)
for blob in blobs:
    blob.delete()
like image 106
Pablo Avatar answered Sep 30 '22 16:09

Pablo


AppEngine release 1.7.0 has support for deleting Google Storage objects using the blobstore API.

key = blobstore.create_gs_key('/gs/my_bucket/my_object')
blobstore.delete(key)

Alternatively, you can use the REST API to make a call out to Google Storage to delete the file.

https://developers.google.com/storage/docs/reference-methods#deleteobject

like image 35
Stuart Langley Avatar answered Sep 30 '22 16:09

Stuart Langley