I have some data stored in an AWS
S3
bucket.
If from terminal I do:
aws s3 ls s3://myBucket/folder/ --profile myProfile
2020-04-23 01:04:09 96858539 2020-01-01-file.csv.gz
If I try to download the file using boto3
import boto3
session = boto3.session.Session(profile_name='myProfile')
s3 = boto3.resource('s3')
f1 = '2020-01-01-file.csv.gz'
s3.meta.client.download_file('myBucket', 'folder/%s'%f1, f1)
I get the following error
ClientError: An error occurred (403) when calling the HeadObject operation: Forbidden
The reason why it doesn't work for you is that you create boto3 session
session = boto3.session.Session(profile_name='myProfile')
and you do not use it . So instead of boto3.resource('s3')
, should try session.resource('s3')
But anyway, the boto3 docs have an entire section called Downloading Files . It shows two examples with explanation:
import boto3
s3 = boto3.client('s3')
s3.download_file('BUCKET_NAME', 'OBJECT_NAME', 'FILE_NAME')
or
s3 = boto3.client('s3')
with open('FILE_NAME', 'wb') as f:
s3.download_fileobj('BUCKET_NAME', 'OBJECT_NAME', f)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.