Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download file from AWS S3 using Python

I am trying to download a file from Amazon S3 bucket to my local using the below code but I get an error saying "Unable to locate credentials"

Given below is the code I have written:

from boto3.session import Session import boto3  ACCESS_KEY = 'ABC' SECRET_KEY = 'XYZ'  session = Session(aws_access_key_id=ACCESS_KEY,               aws_secret_access_key=SECRET_KEY) s3 = session.resource('s3') your_bucket = s3.Bucket('bucket_name')  for s3_file in your_bucket.objects.all():     print(s3_file.key) # prints the contents of bucket  s3 = boto3.client ('s3')  s3.download_file('your_bucket','k.png','/Users/username/Desktop/k.png') 

Could anyone help me on this. Thanks.

like image 624
Taukheer Avatar asked Apr 30 '18 12:04

Taukheer


People also ask

How do I download data from AWS S3?

You can download an object from an S3 bucket in any of the following ways: Select the object and choose Download or choose Download as from the Actions menu if you want to download the object to a specific folder. If you want to download a specific version of the object, select the Show versions button.


2 Answers

You are not using the session you created to download the file, you're using s3 client you created. If you want to use the client you need to specify credentials.

your_bucket.download_file('k.png', '/Users/username/Desktop/k.png') 

or

s3 = boto3.client('s3', aws_access_key_id=... , aws_secret_access_key=...) s3.download_file('your_bucket','k.png','/Users/username/Desktop/k.png') 
like image 174
Joaquín Bucca Avatar answered Sep 23 '22 20:09

Joaquín Bucca


From an example in the official documentation, the correct format is:

import boto3  s3 = boto3.client('s3', aws_access_key_id=... , aws_secret_access_key=...) s3.download_file('BUCKET_NAME', 'OBJECT_NAME', 'FILE_NAME') 

You can also use a file-like object opened in binary mode.

s3 = boto3.client('s3', aws_access_key_id=... , aws_secret_access_key=...) with open('FILE_NAME', 'wb') as f:     s3.download_fileobj('BUCKET_NAME', 'OBJECT_NAME', f)     f.seek(0) 

The code in question uses s3 = boto3.client ('s3'), which does not provide any credentials.

The format for authenticating a client is shown here:

import boto3 client = boto3.client(     's3',     aws_access_key_id=ACCESS_KEY,     aws_secret_access_key=SECRET_KEY,     aws_session_token=SESSION_TOKEN, )  # Or via the Session session = boto3.Session(     aws_access_key_id=ACCESS_KEY,     aws_secret_access_key=SECRET_KEY,     aws_session_token=SESSION_TOKEN, ) 

And lastly you can also re-use the authenticated session you created to get the bucket, and then download then file from the bucket.

from boto3.session import Session import boto3  ACCESS_KEY = 'ABC' SECRET_KEY = 'XYZ'  session = Session(aws_access_key_id=ACCESS_KEY,               aws_secret_access_key=SECRET_KEY)  # session is authenticated and can access the resource in question  session.resource('s3')     .Bucket('bucket_name')     .download_file('k.png','/Users/username/Desktop/k.png') 
like image 36
Increasingly Idiotic Avatar answered Sep 20 '22 20:09

Increasingly Idiotic