boto3 在通过 python 上传文件时给出拒绝访问错误
boto3 giving Access Denied error while uploading file through python
当我尝试在 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 的客户端时添加了访问密钥和秘密密钥
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 将图像上传到 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 的客户端时添加了访问密钥和秘密密钥
s3_client = boto3.client('s3', aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
存在一个很好的文档at boto documentation