简体   繁体   English

使用 Python 从公共 AWS S3 下载文件/文件夹,无凭据

[英]Download file/folder from Public AWS S3 with Python, no credentials

I have opened a public access to S3 bucket and I need to download files / folders with files from the bucket using python.我已经打开了对 S3 存储桶的公共访问权限,我需要使用 python 从存储桶中下载文件/文件夹。 The trick is that I do not want to supply credentials (which boto3 apparently requires).诀窍是我不想提供凭据(boto3 显然需要)。 Is it even possible?甚至可能吗?

You can use GetObject from the S3 REST API, together with the Requests library in Python.您可以使用来自S3 REST API 的GetObject ,以及 Python 中的Requests库。 If you grant READ access to the anonymous user, you can return the object without using an authorization header.如果您授予匿名用户 READ 访问权限,则可以在不使用授权 header 的情况下返回 object。

Example of such an S3 REST call::此类 S3 REST 调用示例:

> GET /example-object HTTP/1.1
> Host: example-bucket.s3.<Region>.amazonaws.com    

Python(rough example): Python(粗略示例):

import requests
url = '/example-object'
headers = {'Host': 'example-bucket.s3.<Region>.amazonaws.com'}
r = requests.get(url, headers=headers)

Requests 要求

GetObject 获取对象

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

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