如何通过boto获取S3密钥的创建日期? [英] How do I get the S3 key's created date with boto?

查看:114
本文介绍了如何通过boto获取S3密钥的创建日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Boto的S3 Key对象包含last_modified日期(可通过parse_ts很好地使用它,谢谢@Gaarnat!),但是base_field日期"(即ctime)似乎无法访问,即使它已列在key.base_fields中

Boto's S3 Key object contains last_modified date (which is nicely available via parse_ts, thanks @Gaarnat!) but the base_field "date" (i.e., ctime) doesn't seem to be accessible, even though it's listed in key.base_fields.

基于 http://docs.aws.amazon .com/AmazonS3/latest/dev/UsingMetadata.html ,它似乎总是自动创建的(我无法想象为什么会这样).在对象属性中的某个位置找到它可能只是一个简单的问题,但是到目前为止,尽管我确实找到了包含"date"的base_fields属性,但仍无法找到它. (它们只是一个集合,似乎没有可用的方法,我无法找到有关检查方法的文档.)

Based on the table at http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html, it does seem that it is always automatically created (and I can't imagine a reason why it wouldn't be). It's probably just a simple matter of finding it somewhere in the object attributes, but I haven't been able to find it so far, although I did find the base_fields attribute which contains 'date'. (They're just a set and don't seem to have an available methods and I haven't been able to find documentation regarding ways to inspect them.)

例如,Amazon S3维护对象创建日期和大小元数据,并将此信息用作对象管理的一部分.

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

有趣的是,尽管last_modified可见,但create_time(上面链接中的系统元数据字段日期")也未显示在AWS S3控制台中.

Interestingly, create_time (system metadata field "Date" in link above) does not show up in the AWS S3 console, either, although last_modified is visible.

推荐答案

经过进一步研究,看来从list()返回的S3关键对象可能不包含此元数据字段!

After additional research, it appears that S3 key objects returned from a list() may not include this metadata field!

由迭代器返回的Key对象是通过在存储桶上解析GET的结果(也称为列表对象"请求)而获得的. 此请求返回的XML仅包含有关每个键的信息的子集. XML中没有某些诸如元数据类型和用户元数据之类的元数据字段.因此,如果您需要这些其他元数据字段,则必须对存储桶中的Key进行HEAD请求. (文档)

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

... 返回包含创建日期和其他一些系统元数据的完整密钥. (例如,它也缺少ACL数据).

... does not return the complete key with creation date and some other system metadata. (For example, it's also missing ACL data).

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

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

这需要额外的HTTP请求,如文档清楚地在上面所述.(另请参见

This necessitates an additional HTTP request as the docs clearly state above. (See also 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'

这篇关于如何通过boto获取S3密钥的创建日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆