Getting S3 objects' last modified datetimes with boto
Here's a snippet of Python/boto code that will print the last_modified attribute of all keys in a bucket:
>>> import boto
>>> s3 = boto.connect_s3()
>>> bucket = s3.lookup('mybucket')
>>> for key in bucket:
print key.name, key.size, key.last_modified
index.html 13738 2012-03-13T03:54:07.000Z
markdown.css 5991 2012-03-06T18:32:43.000Z
>>>
For just one s3 object you can use boto client's head_object()
method which is faster than list_objects_v2()
for one object as less content is returned. The returned value is datetime
similar to all boto responses and therefore easy to process.
head_object()
method comes with other features around modification time of the object which can be leveraged without further calls after list_objects()
result.
See this : https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.head_object
import boto3
s3 = boto3.client('s3')
response = client.head_object(Bucket, Key)
datetime_value = response["LastModified"]
this is working (tnx to jdennison from above):
after getting the key from s3:
import time
from time import mktime
from datetime import datetime
modified = time.strptime(key.last_modified, '%a, %d %b %Y %H:%M:%S %Z')
#convert to datetime
dt = datetime.fromtimestamp(mktime(modified))
Boto3 returns a datetime object for LastModified
when you use the the (S3) Object
python object:
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Object.last_modified
You shouldn't need to perform any tortuous string manipulations.
To compare LastModified
to today's date (Python3):
import boto3
from datetime import datetime, timezone
today = datetime.now(timezone.utc)
s3 = boto3.client('s3', region_name='eu-west-1')
objects = s3.list_objects(Bucket='my_bucket')
for o in objects["Contents"]:
if o["LastModified"] == today:
print(o["Key"])
You just need to be aware that LastModifed
is timezone aware, so any date you compare with it must also be timezone aware, hence:
datetime.now(timezone.utc)