s2_amazonS3.py 2.09 KB
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 24 21:22:00 2016

@author: Michel Le Page

This script depends on "SDK AWS pour Python (Boto3)": https://aws.amazon.com/fr/sdk-for-python/
pip install boto3

https://github.com/sinergise/Sentinel2ProductIngestor
Each file is its own object in Amazon S3. The data are organised per tiles using the Military grid system. 
The latest KML file describing available tiles is available is ESA site (https://sentinels.copernicus.eu/web/sentinel/missions/sentinel-2/data-products), 
or via direct link (https://sentinels.copernicus.eu/documents/247904/1955685/S2A_OPER_GIP_TILPAR_MPC__20151209T095117_V20150622T000000_21000101T000000_B00.kml/ec05e22c-a2bc-4a13-9e84-02d5257b09a8). 
SHP version is available here: https://github.com/justinelliotmeyers/Sentinel-2-Shapefile-Index

"""

import boto3
from botocore.handlers import disable_signing
import os

# ------------------------------------------------------
# To be modified by the user:
# tiles = tiles ID as in the Kml or Shp indicated above
# years = the years to download
#
tiles=['43/Q/HV']
years=['2015','2016']
dst_dir='C:\\Users\\michel\\Documents\\SIE\\SIE_Download'

# ------------------------------------------------------

S2bucket2='sentinel-s2-l1c'

s3 = boto3.resource('s3')
s3.meta.client.meta.events.register('choose-signer.s3.*', disable_signing)

bucket = s3.Bucket(S2bucket2)

for tile in tiles:
    for year in years:
        kk=bucket.objects.filter(Prefix='tiles/'+tile+'/'+year)

        for key in kk.all():
            print(key.key,' ', key.size,' bytes')
            aaa=key.key.split('/')
            tileid=aaa[1]+aaa[2]+aaa[3]
            ladate=aaa[4]+'_'+aaa[5]+'_'+aaa[6]
            name=aaa[len(aaa)-1]
            path0 = os.path.join(dst_dir, tileid)
            if not(os.path.isdir(path0)):
                os.mkdir(path0)
            path1 = os.path.join(path0, ladate)
            if not(os.path.isdir(path1)):
                os.mkdir(path1)            
            path2 = os.path.join(path1, name)
            bucket.download_file(key.key, path2)