Python S3文件上传显示权限错误

时间:2019-06-12 14:37:18

标签: python-3.x amazon-web-services amazon-s3

我正在尝试将文件上传到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

1 个答案:

答案 0 :(得分:0)

耦合事物:

  • 确保您确实通过仪表板成功创建了安全凭证
  • 您创建了存储桶吗?如果未创建存储桶,并且您的安全凭据所对应的用户组没有上载到该存储桶的权限,那么您将无法将对象放入该存储桶,对吗?
  • 次要细节:请使用环境变量,而不是将凭据复制粘贴到脚本中
import os

ACCESS_KEY = os.environ['access_key_id']
SECRET_KEY = os.environ['secret_access_key']