【发布时间】:2019-06-12 14:37:18
【问题描述】:
我正在尝试在 python 中将文件上传到 s3。到目前为止我的代码是这样的
import boto3
from botocore.exceptions import NoCredentialsError
ACCESS_KEY = 'XXXXXXXXXXXX'
SECRET_KEY = 'XXXXXXXXXXXX'
def upload_to_aws(local_file, bucket, s3_file):
s3 = boto3.client('s3', aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY)
try:
s3.upload_file(local_file, bucket, s3_file)
print("Upload Successful")
return True
except FileNotFoundError:
print("The file was not found")
return False
except NoCredentialsError:
print("Credentials not available")
return False
uploaded = upload_to_aws('image-1.png', 'bucketname', 'image-1.png')
但是当我尝试运行代码时,它会显示类似的错误
boto3.exceptions.S3UploadFailedError: Failed to upload-image-1.png to bucketname/image-1.png: 调用PutObject操作时发生错误(AccessDenied): Access Denied
我已经检查了存储桶权限及其罚款。权限是这样的:
Block all public access
Off
Block public access to buckets and objects granted through new access control lists (ACLs)
Off
Block public access to buckets and objects granted through any access control lists (ACLs)
Off
Block public access to buckets and objects granted through new public bucket policies
On
Block public and cross-account access to buckets and objects through any public bucket policies
On
【问题讨论】:
-
是什么让您认为脚本使用的凭据有权
PutObject进入该存储桶?存储桶是否属于您的帐户?如果是这样,请检查与该访问密钥关联的 IAM 策略以查看允许的 S3 权限。
标签: python-3.x amazon-web-services amazon-s3