调用PutObject操作时发生错误(AccessDenied):访问被拒绝python

时间:2019-06-28 21:17:17

标签: python amazon-s3 boto3

我正在尝试向我的AWS存储桶授予权限以能够上传文件。我尝试将公众访问权限更新为公众,并更改我的存储桶策略。我有点被困住了,我不确定如何解决这个问题。

时段政策:

    "Version": "2012-10-17",
    "Id": "PolicyID",
    "Statement": [
        {
            "Sid": "SID",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::id:user1"
            },
            "Action": [
                "s3:PutObject",
                "s3:PutObjectAcl"
            ],
            "Resource": "arn:aws:s3:::bucket/*"
        },
        {
            "Sid": "SID",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::id:user2"
            },
            "Action": [
                "s3:PutObject",
                "s3:PutObjectAcl"
            ],
            "Resource": "arn:aws:s3:::bucket/*"
        }
    ]
}

更新:我可以使用python命令控制台上传文件,但不能在源代码中上传文件。这就是我所拥有的

s3.boto3.resource('s3')
s3.meta.client.upload_file('file.csv', 'bucket', 'file.csv')

我也尝试过:

s3.boto.resource('s3')
s3.Object('bucket').upload_fileobj(object)

1 个答案:

答案 0 :(得分:0)

要成功连接到AWS,我必须将凭据传递到s3资源

s3 = boto3.resource('s3', aws_access_key_id='key', aws_secret_access_key='key')