Moving file from one folder to another in S3 using python boto3 client


Mar 29, 2022    Janaki Mahapatra, Python

import sys
import os
import boto3

bucket=os.getenv('BUCKET_NAME','my-bucket')

def getMinPDFTime(fileName, colName):
    s3 = boto3.client('s3')
    response = s3.get_object(Bucket=bucket, Key=fileName)
    status = response.get("ResponseMetadata", {}).get("HTTPStatusCode")
    if status == 200:
        df = pd.read_csv(response.get("Body"))    
        tstamp =  df[colName].min().split(" ")          
        return datetime.strptime(tstamp[0], "%m/%d/%Y").strftime("%Y%m")
    else:
        return datetime.today().strftime('%Y%m')

def moveFiles(source, desination):
    s3 = boto3.client('s3')	
    results = s3.list_objects(Bucket=bucket, Prefix=source)
    allowdFiles = ["file.csv", "file_addr.csv"]
    for obj in results.get('Contents'):
        fileKey = obj.get('Key')
        print("Moving file "+fileKey)
        copy_source = {'Bucket': bucket, 'Key': fileKey}
        try:	
            fileNameArray = fileKey.split("/")			
            fileName = fileNameArray[-1]
            extractExt = fileName.split(".")
            extn =  extractExt[-1].lower()	
            folderName = extractExt[0].replace("_","-").lower();  
            if fileName.lower() in allowdFiles:	
                destinationKey = desination+"/"+folderName+"/"+fileName
                print(" to destination "+destinationKey)
                ## Copy the file
                s3.copy_object(Bucket = bucket, CopySource = copy_source, Key = destinationKey)
                ## delete the file
                s3.delete_object(Bucket = bucket, Key = fileKey)
        except Exception as e:
            print(e)
            raise e

moveFiles("from_folder","to_folder")