【问题标题】:boto3 giving Access Denied error while uploading file through pythonboto3在通过python上传文件时出现拒绝访问错误
【发布时间】:2021-07-30 21:03:02
【问题描述】:

当我尝试在 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

【问题讨论】:

    标签: python amazon-web-services upload boto3 access-denied


    【解决方案1】:

    解决方案非常简单,因为我没有提供 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-11
      • 1970-01-01
      • 2023-03-03
      • 2021-02-03
      • 2012-10-05
      • 1970-01-01
      • 2016-02-07
      • 2022-08-02
      相关资源
      最近更新 更多