Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading the files from s3 recursively using boto python.

Tags:

I have a bucket in s3, which has deep directory structure. I wish I could download them all at once. My files look like this :

foo/bar/1. .  foo/bar/100 . .  

Are there any ways to download these files recursively from the s3 bucket using boto lib in python?

Thanks in advance.

like image 985
sriram Avatar asked Oct 25 '12 07:10

sriram


People also ask

How do I extract files from S3 bucket?

In the Amazon S3 console, choose your S3 bucket, choose the file that you want to open or download, choose Actions, and then choose Open or Download. If you are downloading an object, specify where you want to save it. The procedure for saving the object depends on the browser and operating system that you are using.


1 Answers

You can download all files in a bucket like this (untested):

from boto.s3.connection import S3Connection  conn = S3Connection('your-access-key','your-secret-key') bucket = conn.get_bucket('bucket') for key in bucket.list():     try:         res = key.get_contents_to_filename(key.name)     except:         logging.info(key.name+":"+"FAILED") 

Keep in mind that folders in S3 are simply another way of writing the key name and only clients will show this as folders.

like image 55
j0nes Avatar answered Sep 16 '22 21:09

j0nes