【问题标题】:Python S3 file upload showing permission errorPython S3 文件上传显示权限错误
【发布时间】:2019-06-12 14:37:18
【问题描述】:

我正在尝试在 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: Failed to upload-image-1.png to bucketname/image-1.png: 调用PutObject操作时发生错误(AccessDenied): Access Denied

我已经检查了存储桶权限及其罚款。权限是这样的:

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

【问题讨论】:

  • 是什么让您认为脚本使用的凭据有权 PutObject 进入该存储桶?存储桶是否属于您的帐户?如果是这样,请检查与该访问密钥关联的 IAM 策略以查看允许的 S3 权限。

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


【解决方案1】:

几件事:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    • 1970-01-01
    • 2012-06-06
    • 2012-12-31
    • 2012-08-08
    相关资源
    最近更新 更多