Say you have an s3 bucket on amazon web services. You may want to programmatically empty it. In order to empty a bucket it must have items in it. So I’ll demonstrate how to put and remove items from a bucket. We’ll use the excellent boto3 library.
Here we are using the Client object to put many small files in the s3 bucket. There’s a Bucket object but I didn’t find it very useful for this task. The script prints out it’s progress as I found writing to s3 fairly slow.
Note I used the str.format() method to zero-pad the path names. This
gives the very nice effect of seeing the numbers increase monotonically. .format
will basically replace any region between two
curly braces with the supplied argument, formatting it as specified. So {:04d}
will be replaced by i
to 4 characters of length
and padded with the 0
character.
I pulled the constants from a separate file as that’s closer to what I was doing in django.
Here I empty the bucket. The list_objects_v2 function is designed to paginate and will return at most 1000 object paths. So you’ll see we issue requests for more object paths if the number of keys in a response is 1000. The loop above will delete and paginate until all objects are deleted from the bucket.
It is also idempotent and will not throw if the bucket already empty.
The scripts are available in a gist.
If you need help solving your business problems with software read how to hire me.