"""Step 1 of Image Asset Migration from S3 Bucket to S3 Bucket. Command line script to identify all images in an S3 bucket and write batch files for further processing. Example Usage: $ python create_batch_keys_script.py """ # !/usr/bin/env python # -*- coding: utf-8 -*- import argparse import datetime import hashlib import logging import os from os.path import dirname from os.path import join import re import sys import time import uuid import boto3 from dotenv import load_dotenv import requests dotenv_path = join(dirname(__file__), '.env') load_dotenv(dotenv_path) OWS_PRODUCT_URL = os.environ.get('OWS_PRODUCT_URL') SOURCE_BUCKET_NAME = os.environ.get('SOURCE_BUCKET_NAME') SOURCE_SUB_FOLDER = os.environ.get('SOURCE_SUB_FOLDER') def step_through_images_from_s3_bucket_and_create_batch_files( source_bucket, source_bucket_name, source_sub_folder, batch_size, max_objects): """Loop through objects in an AWS S3 bucket and create batch files for further processing. Args: source_bucket (class): AWS S3 Bucket to copy objects from source_bucket_name (str): name of source bucket source_sub_folder (str): name of folder to search within batch_size (int): number of lines in a batch file max_objects (int): number of objects to copy """ start_time = time.time() count = 0 batch_count = 0 file_count = 1 file_prefix = 'image-asset-migration-batch-key-' correlation_id = str(uuid.uuid4()) logging.warning('Date of execution: {}'.format(datetime.datetime.now())) logging.warning('Now running migration with Correlation-Id: {}'.format( correlation_id)) f = open(file_prefix + str(file_count), 'w') for s3_object in source_bucket.objects.filter(Prefix=source_sub_folder): # Look for S3 objects that end with .jpg or .jpeg file_match = re.search('[^\/]*\..+$', s3_object.key, re.IGNORECASE) if file_match: filename = file_match.group(0) file_extension = re.search('\..+$', filename).group(0) upc = re.search('\d+', filename).group(0) file_line = '{bucket},{key},{upc},{file_extension},{correlation_id}'.format( bucket=source_bucket_name, key=s3_object.key, upc=upc, file_extension=file_extension, correlation_id=correlation_id) f.write(file_line + os.linesep) count += 1 batch_count += 1 if batch_count == batch_size: f.close() file_count += 1 batch_count = 0 f = open(file_prefix + str(file_count), 'w') if count == max_objects: break run_time = time.time() - start_time logging.warning('wrote {} batch files in {} seconds'.format(file_count, run_time)) sys.exit(0) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument( 'batch_size', nargs='?', default=1000, type=int, help='the max size of the batch files to be written') parser.add_argument( 'max_objects', nargs='?', default=10000, type=int, help='the max number of objects copied') args = parser.parse_args() s3 = boto3.resource('s3') source_bucket = s3.Bucket(SOURCE_BUCKET_NAME) logging.basicConfig( filename='create_batch_keys.log', level=logging.WARNING) step_through_images_from_s3_bucket_and_create_batch_files( source_bucket, SOURCE_BUCKET_NAME, SOURCE_SUB_FOLDER, args.batch_size, args.max_objects)