Search code examples
pythonamazon-s3boto3

How to download files from s3 given the file path using boto3 in python


Pretty basic but I am not able to download files given s3 path.

for eg, I have this s3://name1/name2/file_name.txt

import boto3
locations = ['s3://name1/name2/file_name.txt']
s3_client = boto3.client('s3')
bucket = 'name1'
prefix = 'name2'

for file in locations:
    s3_client.download_file(bucket, 'file_name.txt', 'my_local_folder')

I am getting error as botocore.exceptions.ClientError: An error occurred (404) when calling the HeadObject operation: Not Found

This file exists as when I download. using aws cli as s3 path: s3://name1/name2/file_name.txt .


Solution

  • You need to have a list of filename paths, then modify your code like shown in the documentation:

    import os
    import boto3
    import botocore
    
    files = ['name2/file_name.txt']
    
    bucket = 'name1'
    
    s3 = boto3.resource('s3')
    
    for file in files:
       try:
           s3.Bucket(bucket).download_file(file, os.path.basename(file))
       except botocore.exceptions.ClientError as e:
           if e.response['Error']['Code'] == "404":
               print("The object does not exist.")
           else:
               raise