0

I wanted to download specific folder content from a S3 bucket. I am able to access s3 bucket but how can I access folder and download the content in the folder to my local drive.

ACCESS_KEY = 'def'
SECRET_KEY = 'abc'
session = Session(aws_access_key_id=ACCESS_KEY,aws_secret_access_key=SECRET_KEY)
s3 = session.resource('s3')
your_bucket = s3.Bucket('bucket_name') 
filename = []
for s3_file in your_bucket.objects.all():
    filename.append(s3_file.key)
    s3 = boto3.client('s3',aws_access_key_id = ACCESS_KEY,aws_secret_access_key= SECRET_KEY)
for l in range(len(filename)):
    s3.download_file('bucket_name',filename[l],'filepath'+filename[l])

The above code is used to access the content from the Aws s3 bucket. In my s3 bucket there are multiple folders I need to select the specific folder and download contents only from that folder. How can I do this?

Maurice
  • 11,482
  • 2
  • 25
  • 45
sai
  • 95
  • 2
  • 11
  • Downloading all files within a specific folder is exactly the same as downloading the whole bucket (shown in the linked answer), but you can specify a `Prefix` when performing the loop. Alternatively, you could use the [AWS Command-Line Interface (CLI) cp command](https://docs.aws.amazon.com/cli/latest/reference/s3/cp.html) `aws s3 cp --recursive` command rather than writing the code yourself. – John Rotenstein Jul 01 '21 at 14:07

2 Answers2

1

Here is an example of how to do that with Minio(Amazon S3 compatible) using python:

client = Minio(
    "localhost:port",
    access_key="access_key",
    secret_key="secret_key",
    secure=False,
)
objects = client.list_objects("index", prefix="public/")
for obj in objects:
    <Do something ....>
0

The best way to do this is to list the contents (files) of that prefix (folder) and then download the files individually.

Best, Stefan

StefanN
  • 527
  • 1
  • 4
  • 12
  • Hi @StefanN, I am unable to understand can you please give me in the form of code for better understandability – sai Jul 01 '21 at 14:03
  • Hi @StefanN, My problem is once I downloaded, how to move them to a folder of my choice or is there a way to download into a specific folder – Som Pra Jul 07 '22 at 09:21