How do I get the file / key size in boto S3?

后端 未结 5 1897
隐瞒了意图╮
隐瞒了意图╮ 2020-12-13 05:33

There must be an easy way to get the file size (key size) without pulling over a whole file. I can see it in the Properties of the AWS S3 browser. And I think I can get it

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-13 06:20

    This would work:

    bk = conn.get_bucket('my_bucket_name')
    key = bk.lookup('my_key_name')
    print key.size
    

    The lookup method simply does a HEAD request on the bucket for the keyname so it will return all of the headers (including content-length) for the key but will not transfer any of the actual content of the key.

    The S3 tutorial mentions this but not very explicitly and not in this exact context. I'll add a section on this to help make it easier to find.

    Note: for every old link like http://boto.cloudhackers.com/s3_tut.html that returns a 404, add in "/en/latest" right after the ".com" : http://boto.cloudhackers.com/en/latest/s3_tut.html . (Someone needs to explore mod_rewrite...)

提交回复
热议问题