本文介绍了如何在 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

查找方法只是在存储桶上对键名执行 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​​ 教程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 的旧链接,添加 "/en/latest"".com" 之后: 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 中获取文件/密钥大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 03:21