简体   繁体   English

使用 Python 在公共 S3 存储桶上下载文件而不进行身份验证

[英]Downloading Files on a Public S3 Bucket Without Authentication Using Python

Im trying to download a file from an S3 bucket that is public and requires no authentication (meaning no need to hardcode Access and Secret keys to access nor store it in AWS CLI), yet I still cannot access it via boto3.我试图从 S3 存储桶中下载一个文件,该文件是公开的且不需要身份验证(这意味着无需硬编码访问和密钥来访问或将其存储在 AWS CLI 中),但我仍然无法通过 boto3 访问它。

Python code 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

I get this error code when I execute the code执行代码时收到此错误代码

AttributeError: 'S3' object has no attribute 'Bucket' AttributeError: 'S3' object 没有属性 'Bucket'

If it helps, here is my bucket public policy如果有帮助,这是我的存储桶公共政策

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

If your suggestion is to store keys, please dont, that is not what I'm trying to do.如果您的建议是存储密钥,请不要,这不是我想要做的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM