boto3使用aws键上传S3

时间:2016-01-15 23:03:35

标签: amazon-s3 aws-sdk boto3

boto3文档建议从命令行配置密钥。无论如何,我可以将AWS密钥放入python源代码中吗?以下是供参考的代码。

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)

2 个答案:

答案 0 :(得分:12)

请参阅“方法参数”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>)

答案 1 :(得分:-1)

还可以使用默认的KMS密钥下载文件:

#!/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')

以下是使用KMS上传和下载s3文件的示例

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