Search code examples
pythonpython-3.xamazon-web-servicesfileboto3

saving the downloaded files to a specified directory boto3


below code would download the file into the current dir. I want to make a folder in the current directory and download all the files to that dir.

for obj in bucket.objects.all():
s3.meta.client.download_file(
    bucket.name, obj.key, obj.key)

Solution

  • If you look at the download_file method documentation for the S3 Bucket resource, you will find a reference to downloading a file to the /tmp/ directory. So with that in mind, you can change the 3rd parameter on your download_file call to point to any directory you want.

    s3.meta.client.download_file(
        bucket.name, obj.key, f"/tmp/{obj.key}")
    

    https://boto3.amazonaws.com/v1/documentation/api/1.9.42/reference/services/s3.html#S3.Bucket.download_file