Search code examples
pythonamazon-web-servicesamazon-s3aws-lambdaboto3

S3 File Copy:Same bucket: using python boto3 from one folder to the bucket


We have a file in s3bucket named: aftoem-resolver-group-data-1

in the format as follows:

fftoem-operation-group-data-1
|-Folder Name: 2023-09-25 //Today's Date
  |- File Name: {randomstring coming as an output from AWS Glue}.csv

We have written a lambda function to copy the file inside the 2023-09-25 folder and paste it in the bucket and delete the folder.

Resultant Output:

fftoem-operation-group-data-1
|- File Name: resolver_group-2023-09-25.csv

We have written the following code for lambda however it is giving us unexpected results.

The below code gives the results but it is into another folder inside the bucket, however we do not want any folder into the bucket we need directly the file inside the bucket. Any possibility in achieving the same.

import boto3
import botocore
import json
import os
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)

s3 = boto3.resource('s3')
client = boto3.client('s3')

def lambda_handler(event, context):
    logger.info("New files uploaded to the source bucket.")
    print("FP: Printing Event", event)
        
    key = event['Records'][0]['s3']['object']['key']
    source_bucket = event['Records'][0]['s3']['bucket']['name']
    new_key = f'resolver_group-{key[3:].partition("%")[0]}.csv'
    
    print(f"FP: Printing key={key}")
    print(f"FP: Prining source_bucket={source_bucket}")
    print(f"FP: Printing new_key={new_key}")
    
    destination_bucket = source_bucket
    source = {'Bucket': source_bucket, 'Key': key}
    print(f"FP: Printing source={source}")
    
    
    response = client.list_objects_v2(Bucket= source_bucket, Prefix = key)
    print(f"FP: Printing response={response}")
    source_key = response["Prefix"]
    print(f"FP: Printing source_key={source_key}")
    copy_source = {'Bucket': source_bucket, 'Key': source_key}
    print(f"FP: Printing copy_source={copy_source}")

    try:
        client.copy_object(Bucket = destination_bucket, CopySource = copy_source, Key = ''+new_key)
        logger.info("File copied to the destination bucket successfully!")
        
    except botocore.exceptions.ClientError as error:
        logger.error("There was an error copying the file to the destination bucket")
        print('Error Message: {}'.format(error))
        
    except botocore.exceptions.ParamValidationError as error:
        logger.error("Missing required parameters while calling the API.")
        print('Error Message: {}'.format(error))

Solution

  • Assuming that all you need to do is extract the YYYY-MM-DD portion and it is always in the same location in the S3 object's key, you can use:

    new_key  = f'resolver_group-{key[:10]}.csv'