Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. The trick is that I do not want to supply credentials (which boto3 apparently requires). Is it even possible?

like image 292
Aramakus Avatar asked May 29 '20 12:05

Aramakus


People also ask

Can I access S3 bucket without access key and secret key?

You can access an S3 bucket privately without authentication when you access the bucket from an Amazon Virtual Private Cloud (Amazon VPC). However, make sure that the VPC endpoint used points to Amazon S3.

How do I download an entire folder in S3 bucket?

Use the s3 cp command with the --recursive parameter to download an S3 folder to your local file system. The s3 cp command takes the S3 source folder and the destination directory as inputs and downloads the folder.

Can we download folder from S3 bucket?

Use the cp command to download a folder inside a bucket from S3 to local. recursive option will download all files and folders if you have a recursive folder/file structure.


1 Answers

You can use GetObject from the S3 REST API, together with the Requests library in Python. If you grant READ access to the anonymous user, you can return the object without using an authorization header.

Example of such an S3 REST call::

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

Python(rough example):

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

Requests

GetObject

like image 78
Adi Dembak Avatar answered Oct 09 '22 02:10

Adi Dembak