I’m trying a simple operation within a python lambda that just copies a file on upload to a specific folder within that same s3 bucket, but Im running into issues. If I hardcode the file, this works but if I try to pass in the src_key of the file, then I get an infinite loop on the folder im wanting to transfer the file(s) to.
Here is my lambda for this thus far.
import os
import logging
import boto3
DST_BUCKET = os.environ.get('DST_BUCKET')
REGION = os.environ.get('REGION')
s3 = boto3.resource('s3', region_name=REGION)
def handler(event, context):
LOGGER.info(f'Event structure: {event}')
LOGGER.info(f'DST_BUCKET: {DST_BUCKET}')
for record in event['Records']:
src_bucket = record['s3']['bucket']['name']
src_key = record['s3']['object']['key']
copy_source = {
'Bucket': src_bucket,
'Key': src_key
}
bucket = s3.Bucket(DST_BUCKET)
bucket.copy(copy_source, src_key)
try:
#Check file
s3.Object(DST_BUCKET, src_key).load()
LOGGER.info(f"File {src_key} uploaded to Bucket {DST_BUCKET}")
# had to hardcode bucket name here
src_bucket2 = s3.Bucket('send-bucket01')
# this is key bit with src_key (NOTE: This is pre-created folder in s3 bucket)
src_bucket2.copy(copy_source, 'fileProcessed/' + src_key)
except Exception as e:
return {"error":str(e)}
return {'status': 'ok'}
The key line is….
src_bucket2.copy(copy_source, 'fileProcessed/' + src_key)
I was thinking this would work, but this just continually copies the folder name and file over and over again in the fileProcessed folder.
Again, if I hardcode the actual file this works (i.e, ‘fileProcessed/myfile.pdf), however, I want to make this dynamic to accept any file uploaded to be put in the fileProcessd folder.
Note: I’ve got a requirement to copy these files from the root path of the s3 bucket not from a precreated folder which I could set the folder prefix in the copy event.
2
Answers
Props to @AnonCoward in above comments for answering my use case. If anyone else is figuring this out, the following worked for me:
src_bucket2.copy(copy_source, 'fileProcessed/' + src_key.split('/')[-1])
As stated above I’ve got a requirement to copy these files from the root path of the source s3 bucket not from a pre created folder. If you can copy files from a given folder then just set the folder path in the event configuration of the s3 bucket which is simple.
It would appear that your AWS Lambda function is configured to be triggered by the creation of an object in Amazon S3. Therefore, the reason why it "continually copies the folder name and file over and over again" is because the copied object is again triggering the Lambda function.
To prevent this happening, ensure that the Event configuration in the S3 bucket only triggers on a specific path (folder) rather than triggering on the bucket as a whole. For example, you might have an
incoming/
path.Then, when the Lambda function copies it to the
fileProcessed/
path, it will not trigger the event again (because the event only triggers on theincoming/
path).