AWS Python SDK Sentinel-2 – How to Download Sentinel-2 L1C Using AWS Python SDK (boto3)

amazon s3amazon-web-servicespythonsentinel-2

The permissions for Sentinel2 L1C changed today

https://www.sentinel-hub.com/faq/how-will-access-permission-changes-aws-bucket-affect-users-accessing-data-directly-non-sentinel#

How can I send a 'requester pays' header when using boto3?

My previous code (below) which worked previously is now hitting a 403

  s3 = boto3.resource('s3')
  bucket = s3.Bucket(bucket_name)
  bucket.download_file(key, fpath)

Best Answer

Here is a sample code that should work (given that your config and credentials are saved in ~/.aws)

import boto3

s3_client = boto3.Session().client('s3')
response = s3_client.get_object(Bucket='sentinel-s2-l1c',
                                Key='tiles/7/W/FR/2018/3/31/0/B01.jp2', 
                                RequestPayer='requester')
response_content = response['Body'].read()

with open('./B01.jp2', 'wb') as file:
    file.write(response_content)

I extracted this code from what we have implemented in sentinelhub Python package. The full code is available here and is basically also handling multithreaded download and certain errors which can occur during download.

By the way, sentinelhub supports download of Sentinel-2 L1C and L2A data from AWS: examples.

Related Question