Created
May 23, 2020 18:06
-
-
Save Allwin12/b522a16030a28c16ac782e71011a0c33 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
from boto3 import Session | |
import datetime | |
from datetime import timezone | |
ACCESS_KEY = "your access key" | |
SECRET_KEY = "your_secret_key" | |
REGION_NAME = "your bucket's region name" | |
BUCKET_NAME = "your bucket name" | |
ses = Session(aws_access_key_id=ACCESS_KEY, | |
aws_secret_access_key=SECRET_KEY, | |
region_name=REGION_NAME) | |
client = ses.client('s3') | |
objects = client.list_objects(Bucket=BUCKET_NAME) | |
contents = objects.get("Contents") | |
if contents: | |
for o in objects["Contents"]: | |
file_creation_date = o["LastModified"] | |
diff = file_creation_date - datetime.datetime.now(timezone.utc) | |
seconds = diff.seconds | |
if seconds > 3600: | |
client.delete_object(Bucket=BUCKET_NAME, Key=o['Key']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment