我正在尝试将文件上传到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:无法将image-1.png上传到bucketname / image-1.png:调用PutObject操作时发生错误(AccessDenied):访问被拒绝
我已经检查了存储桶的许可及其罚款。权限是这样的:
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
答案 0 :(得分:0)
耦合事物:
import os
ACCESS_KEY = os.environ['access_key_id']
SECRET_KEY = os.environ['secret_access_key']