Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boto3 S3 upload using aws key

The boto3 documentation recommend to configure key from the command line. If there anyway I can put the AWS key into the python source code? Below are the code for reference.

If you have the AWS CLI, then you can use its interactive configure command to set up your credentials and default region:
aws configure
Follow the prompts and it will generate configuration files in the correct locations for you.

import boto3

s3 = boto3.resource('s3')
bucket = s3.Bucket('my-bucket')
for obj in bucket.objects.all():
    print(obj.key)
like image 876
user1187968 Avatar asked Dec 15 '22 09:12

user1187968


1 Answers

See under 'Method Parameters' in the official documentation;

from boto3.session import Session

session = Session(aws_access_key_id='<YOUR ACCESS KEY ID>',
                  aws_secret_access_key='<YOUR SECRET KEY>',
                  region_name='<REGION NAME>')
_s3 = session.resource("s3")
_bucket = _s3.Bucket(<BUCKET NAME>)
_bucket.download_file(Key=<KEY>, Filename=<FILENAME>)
like image 100
bmargulies Avatar answered Jan 13 '23 12:01

bmargulies