boto3使用aws键上传S3

use*_*968 4 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)
Run Code Online (Sandbox Code Playgroud)

bma*_*ies 13

请参阅官方文档中的 "方法参数" ;

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>)
Run Code Online (Sandbox Code Playgroud)