gpt4 book ai didi

python - boto3 在通过 python 上传文件时给出拒绝访问错误

转载 作者:行者123 更新时间:2023-12-05 04:47:05 25 4
gpt4 key购买 nike

当我尝试在 python 中使用 boto3 将图像上传到 s3 时,我不断遇到错误。错误说:

An error occurred (AccessDenied) when calling the PutObject operation: Access Denied

我上传图片的代码是

def upload_file(file_name, bucket, object_name=None):
"""Upload a file to an S3 bucket

:param file_name: File to upload
:param bucket: Bucket to upload to
:param object_name: S3 object name. If not specified then file_name is used
:return: True if file was uploaded, else False
"""

# If S3 object_name was not specified, use file_name
if object_name is None:
object_name = file_name

# Upload the file
s3_client = boto3.client('s3')
try:
response = s3_client.upload_file(file_name, bucket, object_name, ExtraArgs={'ACL':'public-read'})
print(response)
except Exception as e:
print(e)
return False
return True

最佳答案

解决方案非常简单易行,因为我没有提供 ACCESS_KEY 和 SECRET_KEY,所以 AWS 不允许我将图像上传到 s3。

我在从 boto3 获取 s3 的客户端时添加了访问 key 和 secret key

s3_client = boto3.client('s3', aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY)

存在一个很好的文档 at boto documentation

关于python - boto3 在通过 python 上传文件时给出拒绝访问错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68597066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com