Created
October 23, 2019 20:14
-
-
Save beatak/0c8db2f6753c2ab98e73c40187245c63 to your computer and use it in GitHub Desktop.
This requires `boto3` and `pymongo` in pip, also you need to run `aws configure` to be done.
This file contains hidden or 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 | |
import sys | |
from bson import json_util | |
import json | |
from datetime import datetime | |
def take_last_modified(obj): | |
return obj.last_modified | |
def bucket_last_modified(bucket_name: str) -> datetime: | |
""" | |
Given an S3 bucket, returns the last time that any of its objects was | |
modified, as a timezone-aware datetime. | |
""" | |
s3 = boto3.resource('s3') | |
bucket = s3.Bucket(bucket_name) | |
objects = list(bucket.objects.all()) | |
max_date = max(obj.last_modified for obj in objects) | |
objects.sort(key=take_last_modified, reverse=True) | |
return { | |
"bucket_name": objects[0].bucket_name, | |
"key": objects[0].key, | |
"last_modified": objects[0].last_modified | |
} | |
if __name__ == "__main__": | |
bucket = sys.argv[1] | |
print("Start digging %s" % (bucket)) | |
print("The last modifed object is: %s" % ( | |
json.dumps(bucket_last_modified(bucket), default=json_util.default) | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment