我使用了一个字典并对值进行排序。如果你需要它,这会给你留下名字和last_modified。否则,一个简单的列表可能会更快。
from boto.s3.connection import S3Connection
conn = S3Connection() # assumes region/keys setup in .boto
bucket = conn.get_bucket('mybucket')
dict = {key.name:key.last_modified for key in bucket.get_all_keys()}
dict = sorted(dict.items() key=lambda x: x[1]) # lambda sort order <
例如:
from boto.s3.connection import S3Connection
conn = S3Connection()
bucket = conn.get_bucket('cgseller-test')
dict = {key.name:key.last_modified for key in bucket.get_all_keys()}
print dict
>>> {u'newfolder/else': u'2015-04-01T01:33:43.000Z', u'newfolder/file': u'2015-04-01T01:23:51.000Z', u'newfolder/file1': u'2015-04-01T01:23:42.000Z', u'newfolder/file2': u'2015-04-01T01:23:34.000Z'}
dict = sorted(dict.items(), key=lambda x: x[1])
print dict
>>>[(u'newfolder/file2', u'2015-04-01T01:23:34.000Z'), (u'newfolder/file1', u'2015-04-01T01:23:42.000Z'), (u'newfolder/file', u'2015-04-01T01:23:51.000Z'), (u'newfolder/else', u'2015-04-01T01:33:43.000Z')]
感谢您的回答。我有一个问题,但。当我看到'k.last_modified'的时间戳时,我看到的时间与我前往S3时看到的时间不同。 'k.last_modified'是否在不同时区输出时间?我怎样才能将它转换为EST? – user2966197 2015-04-01 14:41:02
存储在S3服务中的时间戳应该是UTC。当你说“如果我去S3”,你的意思是你去AWS管理控制台吗?如果是这样,它可能是本地化您的时区的时间。 – garnaat 2015-04-01 14:43:06
是的,我的意思是AWS管理控制台。 – user2966197 2015-04-01 14:49:24