【问题标题】:Downloading Files on a Public S3 Bucket Without Authentication Using Python使用 Python 在没有身份验证的情况下下载公共 S3 存储桶上的文件
【发布时间】:2021-01-28 22:30:47
【问题描述】:

我试图从 S3 存储桶中下载一个文件,该文件是公开的且不需要身份验证(这意味着无需硬编码访问和密钥来访问或将其存储在 AWS CLI 中),但我仍然无法通过 boto3 访问它。

Python 代码

import boto3
import botocore
from botocore import UNSIGNED
from botocore.config import Config

BUCKET_NAME = 'converted-parquet-bucket' 
PATH = 'json-to-parquet/names.snappy.parquet' 

s3 = boto3.client('s3', config=Config(signature_version=UNSIGNED))

try:
    s3.Bucket(BUCKET_NAME).download_file(PATH, 'names.snappy.parquet')
except botocore.exceptions.ClientError as e: 
    if e.response['Error']['Code'] == "404":
        print("The object does not exist.")
    else:
        raise

我在执行代码时收到此错误代码

AttributeError: 'S3' 对象没有属性 'Bucket'

如果有帮助,这是我的存储桶公共政策

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::converted-parquet-bucket/*"
        }
    ]
}

如果您的建议是存储密钥,请不要这样做,这不是我想要做的。

【问题讨论】:

  • 可以只使用requests 而无需设置boto3,请参阅here

标签: python amazon-web-services amazon-s3 boto3


【解决方案1】:

尝试资源s3 = boto3.resource('s3') 而不是s3 = boto3.client('s3')

【讨论】:

    猜你喜欢
    • 2020-08-04
    • 1970-01-01
    • 2022-01-19
    • 2014-11-09
    • 2016-07-11
    • 2018-06-03
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    相关资源
    最近更新 更多