Search code examples
pythonamazon-web-servicesamazon-s3botobucket

Downloading subfolders files in S3 bucket from boto2


I am easily able to download the files from the bucket, but now I need to make a download from a particular folder in my S3 bucket.

I am currently using the below code :

import boto
import boto.s3.connection

access_key = 'XXXXXXXXXXXXXXXX'
secret_key = 'XXXXXXXXXXXXXXXXXXXXXX+XXXXXXXXXXXXX'

bucket_name = 'my_Bucket'
conn =   boto.connect_s3('XXXXXXXXXXXXXXXX','XXXXXXXXXXXXXXXXXXXXXX+XXXXXXXXXXXXX')
bucket = conn.get_bucket(bucket_name)
bucket_list = bucket.list()

LOCAL_PATH = '/Users/tcssig/Documents/EPG/'

for l in bucket_list:
    keyString = str(l.key)
    d = LOCAL_PATH + keyString
    try:
        l.get_contents_to_filename(d)
    except OSError:
        if not os.path.exists(d):
            os.mkdir(d)

How do I get files downloaded from my_folder in the bucket my_bucket ?


Solution

  • Provide the particular folder path as prefix to the list() method

    bucket_list = bucket.list(prefix='particular/folder/path')