python - 使用 Python 在无需身份验证的情况下下载公共(public) S3 存储桶上的文件

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

我正在尝试从公开且无需身份验证的 S3 存储桶下载文件(意味着无需对访问 key 和 key 进行硬编码即可访问或将其存储在 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' object has no attribute 'Bucket'

如果有帮助,这是我的公共(public)政策

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

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

最佳答案

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

关于python - 使用 Python 在无需身份验证的情况下下载公共(public) S3 存储桶上的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58544347/

相关文章:

python - 更新传递给线程python的变量

python - 如何在 R 中将交互式绘图保存为本地文件?

javascript - 制作我的第一个 listObjects from Amazon S3 -- Classic "' Access-Control-Allow-Origin'"遇到

amazon-s3 - 当我请求/foo 或/foo/时,为什么 Amazon S3 不自动提供/foo/index.html?

python - 每个子类都包含自己的列表吗?

python - 错误 "AttributeError: ' unicode' object has no attribute 'read' "on file upload

java - Spring-data-elasticsearch vs elasticsearch RestHighLevelClient 客户端库

php - 尝试在 Amazon AWS 上的 mySQL 数据库上使用 JSON_OBJECT 函数

amazon-web-services - AWS ALB : Path based routing not forwarding to target group

ios - Aws iOS sdk Pre-singed url 给出 "SignatureDoesNotMatch"错误