如何使用 boto 获取 S3 密钥的创建日期?

How do I get the S3 key's created date with boto?

Boto 的 S3 密钥对象包含 last_modified 日期(可以通过 parse_ts 很好地获得,感谢@Gaarnat!)但是 base_field "date"(即 ctime)似乎无法访问,即使它列在 key.base_fields.

根据 http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html 的 table,它似乎总是自动创建的(我无法想象它不会自动创建的原因)。在对象属性的某处找到它可能只是一件简单的事情,但到目前为止我还没有找到它,尽管我确实找到了包含 'date' 的 base_fields 属性。 (它们只是一组,似乎没有可用的方法,我也找不到关于检查它们的方法的文档。)

For example, Amazon S3 maintains object creation date and size metadata and uses this information as part of object management.

有趣的是,create_time(上面 link 中的系统元数据字段 "Date")也没有出现在 AWS S3 控制台中,尽管 last_modified 是可见的。

经过进一步研究,似乎 return 从 list() 编辑的 S3 关键对象可能不包含此元数据字段!

The Key objects returned by the iterator are obtained by parsing the results of a GET on the bucket, also known as the List Objects request. The XML returned by this request contains only a subset of the information about each key. Certain metadata fields such as Content-Type and user metadata are not available in the XML. Therefore, if you want these additional metadata fields you will have to do a HEAD request on the Key in the bucket. (docs)

换句话说,通过键循环:

for key in conn.get_bucket(bucket_name).list():
     print key.date

... 不是 return 包含创建日期和一些其他系统元数据的完整密钥。 (例如,它还缺少 ACL 数据)。

相反,要检索完整的密钥元数据,请使用此方法:

key = bucket.get_key(key.name)
print key.date

这需要额外的 HTTP 请求,因为文档 上面已明确说明。(另请参阅 my original issue report。)

其他代码详细信息:

import boto

# get connection
conn = boto.connect_s3()

# get first bucket
bucket = conn.get_all_buckets()[0]

# get first key in first bucket
key = list(bucket.list())[0]

# get create date if available
print getattr(key, "date", False)
# (False)

# access key via bucket.get_key instead:
k = bucket.get_key(key.name)

# check again for create_date
getattr(k, "date", False)
# 'Sat, 03 Jan 2015 22:08:13 GMT'
# Wait, that's the current UTC time..?

# Also print last_modified...
print k.last_modified
# 'Fri, 26 Apr 2013 02:41:30 GMT'

回答老问题,以防其他人运行遇到同样的问题。

Amazon S3 maintains only the last modified date for each object. For example, the Amazon S3 console shows the Last Modified date in the object Properties pane. When you initially create a new object, this date reflects the date the object is created. If you replace the object, the date changes accordingly. So when we use the term creation date, it is synonymous with the term last modified date.

参考:https://docs.aws.amazon.com/AmazonS3/latest/dev/intro-lifecycle-rules.html

我建议使用 key.last_modified 因为 key.date 似乎 return 您上次查看文件时
所以像这样:

key = bucket.get_key(key.name)
print(key.last_modified)

如果您为 S3 存储桶启用了版本控制,您可以使用 list_object_versions 并找到您要查找的对象的最小日期,该日期应该是它的创建日期