I'm writing a Python script that uploads files to S3 using Boto. I only want to upload changed files (which I can check by their "last modified" datetimes), but I can't find the Boto API endpoint to get the last modified date.
9 Answers
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
>>>
Boto3 returns a datetime object for LastModified when you use the the (S3) Object python object:
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)
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.
import boto3
s3 = boto3.client('s3')
response = client.head_object(Bucket, Key)
datetime_value = response["LastModified"]
Convert the last_modified attribute to struct_time as given below
import time
for key in bucket.get_all_keys():
time.strptime(key.last_modified[:19], "%Y-%m-%dT%H:%M:%S")
This will give a time.struct_time(tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst) tuple for each key in the S3 bucket
If you're using Django and django-storages, you can an unofficial API in the s3boto backend:
>>> from storages.backends.s3boto import _parse_datestring
>>> _parse_datestring("Fri, 20 Jul 2012 16:57:27 GMT")
datetime.datetime(2012, 7, 21, 2, 57, 27)
Unfortunately as of django-storages 1.1.5, this gives a naive datetime. You need to use django.utils.timezone to create an aware version:
>>> from django.utils import timezone
>>> naive = _parse_datestring("Fri, 20 Jul 2012 16:57:27 GMT")
>>> timezone.make_aware(naive, timezone.get_current_timezone())
datetime.datetime(2012, 7, 21, 2, 57, 27, tzinfo=<DstTzInfo 'Australia/Brisbane' EST+10:00:00 STD>)
Using a Resource, you can get an iterator of all objects and then retrieve the last_modified attribute of an ObjectSummary.
import boto3
s3 = boto3.resource('s3')
bk = s3.Bucket(bucket_name)
[obj.last_modified for obj in bk.objects.all()][:10]
returns
[datetime.datetime(2020, 4, 17, 13, 23, 37, tzinfo=tzlocal()),
datetime.datetime(2020, 4, 17, 13, 23, 37, tzinfo=tzlocal()),
datetime.datetime(2020, 4, 17, 13, 23, 38, tzinfo=tzlocal()),
datetime.datetime(2020, 4, 17, 13, 23, 38, tzinfo=tzlocal()),
datetime.datetime(2020, 4, 17, 13, 23, 38, tzinfo=tzlocal()),
datetime.datetime(2020, 4, 17, 13, 23, 37, tzinfo=tzlocal()),
datetime.datetime(2020, 4, 17, 13, 23, 37, tzinfo=tzlocal()),
datetime.datetime(2020, 4, 17, 13, 20, 20, tzinfo=tzlocal()),
datetime.datetime(2020, 4, 20, 8, 30, 2, tzinfo=tzlocal()),
datetime.datetime(2020, 3, 26, 15, 33, 58, tzinfo=tzlocal())]
This is for recent s3 list_objectsv2. The boto3 client gives lastModifed in datetime.datetime format, and ways to convert it is as below
links: boto3 link
and
aws s3 listobj
import datetime
from dateutil.tz import tzutc
# node s3 response '2019-06-17T18:42:57.000Z'
# python boto3 s3 response datetime.datetime(2019, 10, 1, 22, 41, 55, tzinfo=tzutc())
''' {'ETag': '"c8ba0ad5003832f63690ea8ff9b66052"',
'Key': 'SOMEFILE',
'LastModified': datetime.datetime(2019, 10, 2, 18, 50, 47, tzinfo=tzutc()),
'Size': 6390623,
'StorageClass': 'STANDARD'}
'''
l = datetime.datetime(2019, 10, 1, 22, 41, 55, tzinfo=tzutc())
get_last_modified = int(l.strftime('%s'))
print(l)
print(get_last_modified)
last_modifiedattribute. Is that what you're looking for? Disclaimer: I've not used S3 :) - gfortune