user1187968
user1187968

Reputation: 7986

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)

Upvotes: 4

Views: 10502

Answers (2)

ExploringApple
ExploringApple

Reputation: 1484

Also to download the file with default KMS key:

#!/usr/bin/env python
import boto3
from botocore.client import Config
s3_client = boto3.client('s3', config=Config(signature_version='s3v4'))
s3_client.download_file('testtesttest', 'test.txt', '/tmp/test.txt')

Here is an example of upload and download an s3 file using KMS

https://www.justdocloud.com/2018/09/21/use-boto3-download_file-aws-kms/

Upvotes: -1

bmargulies
bmargulies

Reputation: 99993

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>)

Upvotes: 13

Related Questions