我正在设置一些文件传输脚本,并使用boto3来做到这一点。
我需要将一些文件从本地发送到第三方AWS账户(跨账户)。我在另一个帐户上有一个角色设置,并具有写入存储桶的权限,并将此角色分配给了我帐户上的用户。
我可以在CLI上做到这一点,但是Boto继续为存储桶排除AccessDenied错误。
我已经阅读了关于boto3的文档,例如here,并按预期设置了凭据和配置文件(假设它们在CLI方法有效的情况下是正确的) ,但我无法正常工作。
凭据文件:-
[myuser]
aws_access_key_id = XXXXXXXXXXXXXXXXXXXXXX
aws_secret_access_key = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
配置文件:-
[profile crossaccount]
region = eu-west-2
source_profile=myuser
role_arn = arn:aws:iam::0123456789:role/crossaccountrole
这是我正在尝试使用的代码:-
#set-up variables
bucket_name = 'otheraccountbucket'
file_name = 'C:\\Users\\test\\testfile.csv'
object_name = 'testfile.csv'
#create a boto session with profile name for assume role call to be made with correct credentials
session = boto3.Session(profile_name='crossaccount')
#Create s3_client from that profile based session
s3_client = session.client('s3')
#try and upload the file
response = s3_client.upload_file(
file_name, bucket, object_name,
ExtraArgs={'ACL': 'bucket-owner-full-control'}
)
编辑: 为了响应John的多部分权限评论,我尝试通过put_object方法上载以绕过此方法-但仍然获得AccessDenied,但现在具有PutObject权限-我已经确认该权限到位:-
#set-up variables
bucket_name = 'otheraccountbucket'
file_name = 'C:\\Users\\test\\testfile.csv'
object_name = 'testfile.csv'
#create a boto session with profile name for assume role call to be made with correct credentials
session = boto3.Session(profile_name='crossaccount')
#Create s3_client from that profile based session
s3_client = session.client('s3')
#try and upload the file
with open(file_name, 'rb') as fd:
response = s3_client.put_object(
ACL='bucket-owner-full-control',
Body=fd,
Bucket=bucket,
ContentType='text/csv',
Key=object_name
)
交叉帐户具有PutObject权限-错误是:-
An error occurred (AccessDenied) when calling the PutObject operation: Access Denied
END EDIT
这是有效的aws-cli命令:-
aws s3 cp "C:\Users\test\testfile.csv" s3://otheraccountbucket --profile crossaccount
我希望它能像等效的cli代码一样正确上载,但是我收到了S3UploadFailedError异常-调用CreateMultipartUpload操作时发生错误(AccessDenied):访问被拒绝
任何帮助将不胜感激
答案 0 :(得分:1)
我遇到了同样的问题,最终导致我的AWS CLI配置了与我尝试使用Boto3将文件上传到s3存储桶中的Python应用程序不同的凭证的事实。
这对我有用,这仅适用于安装了AWS CLI的人:
aws configure