如何在boto S3中获取文件/密钥大小? [英] How do I get the file / key size in boto S3?

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

问题描述

必须有一种简单的方法来获取文件大小(密钥大小),而又不会覆盖整个文件.我可以在AWS S3浏览器的属性中看到它.而且我认为我可以从"HEAD"请求的"Content-length"标头中获取它.但我没有将有关如何使用boto做到这一点的想法联系在一起.如果您发布的链接比标准boto文档 中的示例更全面,则特别有用.

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 off the "Content-length" header of a "HEAD" request. But I'm not connecting the dots about how to do this with boto. Extra kudos if you post a link to some more comprehensive examples than are in the standard boto docs.

因此,以下内容似乎可以解决问题(尽管从查看源代码的过程中我并不完全确定.):

So the following seems to do the trick (though from looking at source code I'm not completely sure.):

bk = conn.get_bucket('my_bucket_name')
ky = boto.s3.key.Key(bk)
ky.open_read()  ## This sends a GET request. 
print ky.size

现在,我将保留开放性问题供您提出评论,更好的解决方案或指向示例的指针.

For now I'll leave the question open for comments, better solutions, or pointers to examples.

推荐答案

这将起作用:

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

lookup方法只是在存储桶中针对键名进行HEAD请求,因此它将返回键的所有标头(包括内容长度),但不会传输键的任何实际内容.

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.

S3 tutoria l提到了这一点,但并没有非常明确地提及,也没有在确切的上下文中提及.我将为此添加一个部分,以使其更容易找到.

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.

注意:对于每个像http://boto.cloudhackers.com/s3_tut.html这样的旧链接,它返回404,请在".com"之后紧接"/en/latest":http://boto.cloudhackers.com/en/latest/s3_tut.html. (有人需要探索mod_rewrite ...)

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

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

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