I have my code here that I am using to move my files in my S3 bucket. I am using a trigger in my lambda to move files upon “all objects created”.
<code>import boto3
import urllib
TARGET_BUCKET = 'final bucket name'
def lambda_handler(event, context):
# Get incoming bucket and key
source_bucket = event['Records'][0]['s3']['bucket']['name']
source_key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'])
# Copy object to different bucket
s3_resource = boto3.resource('s3')
copy_source = {
'Bucket': source_bucket,
'Key': source_key
}
s3_resource.Bucket(TARGET_BUCKET).Object(source_key).copy(copy_source)
# Delete the source object
#s3_resource.Bucket(source_bucket).Object(source_key).delete()
</code>
<code>import boto3
import urllib
TARGET_BUCKET = 'final bucket name'
def lambda_handler(event, context):
# Get incoming bucket and key
source_bucket = event['Records'][0]['s3']['bucket']['name']
source_key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'])
# Copy object to different bucket
s3_resource = boto3.resource('s3')
copy_source = {
'Bucket': source_bucket,
'Key': source_key
}
s3_resource.Bucket(TARGET_BUCKET).Object(source_key).copy(copy_source)
# Delete the source object
#s3_resource.Bucket(source_bucket).Object(source_key).delete()
</code>
import boto3
import urllib
TARGET_BUCKET = 'final bucket name'
def lambda_handler(event, context):
# Get incoming bucket and key
source_bucket = event['Records'][0]['s3']['bucket']['name']
source_key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'])
# Copy object to different bucket
s3_resource = boto3.resource('s3')
copy_source = {
'Bucket': source_bucket,
'Key': source_key
}
s3_resource.Bucket(TARGET_BUCKET).Object(source_key).copy(copy_source)
# Delete the source object
#s3_resource.Bucket(source_bucket).Object(source_key).delete()
I test it by creating a new folder in the my source bucket, running the lambda, then checking the destination bucket. When I create a folder called “test_1” it doesn’t move to the destination. When I change the name to “test_1.zip” then it moves the folder. I am not sure why.
I did notice the “package type” section in the lambda description has “zip” but I can’t seem to remove that. Is that the issue? I’m using python 3.10