Created
November 6, 2018 00:43
-
-
Save tom-butler/32fd690a7d65f254ed13dc655193b4a9 to your computer and use it in GitHub Desktop.
Downloads Files from s3 using AWS Boto3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import os | |
import boto3 | |
from botocore.handlers import disable_signing | |
import logging | |
BUCKET_NAME='dea-public-data' | |
BUCKET_PATH='L2/sentinel-2-nrt/S2MSIARD/2018-07-30/S2B_OPER_MSI_ARD_TL_EPAE_20180730T055204_A007293_T51KVV_N02.06/' | |
def get_files(bucket_name, prefix): | |
# Create a boto3 client that doesn't require credentials | |
s3 = boto3.resource('s3') | |
s3.meta.client.meta.events.register('choose-signer.s3.*', disable_signing) | |
# Loop through the bucket and download files | |
bucket = s3.Bucket(bucket_name) | |
logging.info("Bucket : %s prefix: %s ", bucket_name, str(prefix)) | |
for obj in bucket.objects.filter(Prefix = str(prefix)): | |
obj_key = obj.key | |
logging.info("Downloading %s", obj_key) | |
# create filepath if it doesn't exist | |
folder, filename = os.path.split(obj_key) | |
if not os.path.exists('./' + folder): | |
logging.info('Creating local directory %s' % ('./' + folder,)) | |
os.makedirs('./' + folder) | |
bucket.download_file(obj_key, './' + obj_key) | |
def main(): | |
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=logging.INFO) | |
get_files(BUCKET_NAME, BUCKET_PATH) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment