Reputation: 39
Basically. I want all the verisons of files in my bucket called awsbkt870 to another bucket called versions870. So i am using the following lambda function code to do it.
import boto3
def lambda_handler(event, context):
source_bucket = 'awsbkt870' # Replace with your actual source bucket name
destination_bucket = 'versions870' # Replace with your actual destination bucket name
s3 = boto3.client('s3')
# List all objects in the source bucket
objects = s3.list_objects(Bucket=source_bucket)
# Iterate through each object and its versions, and copy to the destination bucket
for obj in objects.get('Contents', []):
key = obj['Key']
# List all versions of the current object in the source bucket
versions = s3.list_object_versions(Bucket=source_bucket, Prefix=key)['Versions']
# Reverse the order of versions
versions.reverse()
# Iterate through each version and copy it to the destination bucket
for index, version in enumerate(versions, start=1):
version_id = version['VersionId']
new_key = f"{index}_{key.split('/')[-1]}"
# Check if the file with the new key already exists in the destination bucket
try:
s3.head_object(Bucket=destination_bucket, Key=new_key)
print(f"Version {index} of {key} already exists in {destination_bucket}/{new_key}")
except s3.exceptions.ClientError as e:
if e.response['Error']['Code'] == '404':
# File not found, proceed with copying
try:
s3.copy_object(
Bucket=destination_bucket,
CopySource={'Bucket': source_bucket, 'Key': key, 'VersionId': version_id},
Key=new_key
)
print(f"Version {index} of {key} copied to {destination_bucket}/{new_key}")
except s3.exceptions.ClientError as e:
print(f"Error copying {new_key}: {e}")
else:
Now i want to trigger the function whenever a version gets updated or a file gets added in the source bucket(awsbkt870). But i am getting "An error occurred when creating the trigger: Unable to validate the following destination configurations"
here are the policies attached to my function
What do i do to add the trigger?
I was expecting to add the tigger. I am not able to add the tigger. my test cases with the lambda function are successfull.
Upvotes: 0
Views: 207