Created
June 8, 2021 07:21
-
-
Save mrpandat/61050c4d6f3d3bba37534035d4d41089 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
from datetime import datetime, timedelta | |
def handle(event, context): | |
SOURCE_BUCKET = 'from-bucket' | |
DESTINATION_BUCKET = 'source-bucket' | |
session = boto3.session.Session() | |
s3_client = session.client( | |
service_name='s3', | |
aws_access_key_id='SCWXXXXXXXXXXX', | |
aws_secret_access_key='xxxx-xxxx-xxxx-xxxx-xxxxxxxx', | |
endpoint_url='https://s3.fr-par.scw.cloud', | |
) | |
# Create a reusable Paginator | |
paginator = s3_client.get_paginator('list_objects_v2') | |
# Create a PageIterator from the Paginator | |
page_iterator = paginator.paginate(Bucket=SOURCE_BUCKET) | |
# Loop through each object, looking for ones older than a given time period | |
for page in page_iterator: | |
for object in page['Contents']: | |
if object['LastModified'] < datetime.now().astimezone() - timedelta(hours=1): # <-- Change time period here | |
print(f"Moving {object['Key']}") | |
# Copy object | |
s3_client.copy_object( | |
Bucket=DESTINATION_BUCKET, | |
Key=object['Key'], | |
CopySource={'Bucket':SOURCE_BUCKET, 'Key':object['Key']} | |
) | |
# Delete original object | |
s3_client.delete_object(Bucket=SOURCE_BUCKET, Key=object['Key']) | |
return { | |
"body": json.dumps({"message": "Hello"}), | |
"statusCode": 200, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment