How can I get last modified datetime of S3 objects

2019-01-14 07:19发布

问题:

I'm writing a python scripts to upload files to s3 using boto. I want to only upload changed files which I can check by it's last modified datetime. But I can't find the api to get the last modify in boto API.

回答1:

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
>>>


回答2:

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))


回答3:

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>) 


回答4:

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