"""Step 3 of Image Asset Migration from S3 Bucket to S3 Bucket. Command line script to copy all images listed in a batch file from one S3 bucket to another. Example Usage: $ python copy_from_batch_file_script.py """ # !/usr/bin/env python # -*- coding: utf-8 -*- import argparse import datetime import logging import os import re import sys import time import boto3 def save_assets_to_destination_bucket(filename): """Save assets to the specified destination bucket. Args: filename (str): filename of the batch file to be read """ start_time = time.time() logging.warning('Date of execution: {}'.format(datetime.datetime.now())) with open(filename) as f: lines = f.readlines() for line in lines: bucket, key, destination_bucket_name, destination_file_path, correlation_id = line.strip().split(',') logging.warning('Now running migration with Correlation-Id: {}'.format( correlation_id)) copy_source = { 'Bucket': bucket, 'Key': key } s3.meta.client.copy(copy_source, destination_bucket_name, destination_file_path) run_time = time.time() - start_time logging.warning('copied {} objects in {} seconds'.format(len(lines), run_time)) sys.exit(0) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument( 'file_name', nargs='?', type=str, help='the file paths file to be processed') args = parser.parse_args() s3 = boto3.resource('s3') logging.basicConfig( filename='copy_s3_files.log', level=logging.WARNING) save_assets_to_destination_bucket(args.file_name)