English 中文(简体)
我如何获得文件/同位素S3中的关键尺寸?
原标题:How do I get the file / key size in boto S3?

无需从整个档案中提取文件,就必须采取容易的办法,使档案规模(关键尺寸)更加容易。 我可以把它看见AWS S3的浏览器。 我认为,我可以从“HEAD”请求的“Content-length”头开始。 但是,我不把如何做到这一点的钥匙与同位素联系起来。 添加库多斯语,如果你把一些比标准boto docs更全面的例子联系起来。

EDIT:因此,似乎有以下几条trick子(从对源代码I的看看不出):

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

现在我搁置了这个问题,以征求意见、更好的解决办法或举几个例子。

最佳回答

这将发挥作用:

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

寻找方法只是就关键名称的标本提出欧洲复兴开发银行的要求,这样它就能够把所有头脑(包括内容宽)归还钥匙,但不会转移钥匙的任何实际内容。

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.

注:关于<代码>后每条旧链接,如,将404加在/en/latest>上,.com>: http://boto.cloudhackers.com/en/latest/s3_tut.html>。 (人们需要探讨改写......)

问题回答

www.un.org/Depts/DGACM/index_spanish.htm

s3.head_object 此外,还执行欧洲环保局关于检索有关物体的元数据的请求:

s3 = boto3.client( s3 )
response = s3.head_object(Bucket= bucketname , Key= keyname )
size = response[ ContentLength ]

http://www.ohchr.org。

Using S3 Object you can fetch the file (a.k.a object) size in bytes. It is a resource representing the Amazon S3 Object.

事实上,你可以获取与物体有关的所有元数据。 同content_length 标的大小,content_English 语 内容载于content_encoding,last_modised等。

import boto3

s3 = boto3.resource( s3 )
object = s3.Object( bucket_name , key )
file_size = object.content_length #size in bytes

参考boto3 doc

使用S3资源boto3:

boto3.resource( s3 ).Bucket(bucketname).Object(keyname).content_length

The head_object calls of the S3user re me an http://403 Forbidden”

如果需要核对多个档案,你还可以获得所有物体的清单。 特定锅炉的操作list_objects_v2,然后通过回复检索。 例如:

s3_client = boto3.client( s3 )
response_contents = s3_client.list_objects_v2(
        Bucket= name_of_bucket 
        ).get( Contents )

页: 1

[{ Key :  path/to/object1 ,  LastModified : datetime,  ETag :  "some etag" ,  Size : 2600,  StorageClass :  STANDARD }, { Key :  path/to/object2 ,  LastModified :  datetime ,  ETag :  "some etag" ,  Size : 454,  StorageClass :  STANDARD }, ... ]

Notice that each dictionary in the list contains Size key, which is the size of your particular object. It s iterable

for rc in response_contents:
    print(f"Size: {rc.get( Size )}")

您对你可能感兴趣的所有档案拥有一定规模:

Size: 2600
Size: 454
Size: 2600
...

很多答案是很简单的答复(肯定会奏效),你不需要履行欧洲开发合作署的请求,也没有代表亚马孙S3目标的资源。

适当阅读桶内的3个物体(次要物体)。 规模不大可能起作用,而将大米改为文档/钥匙的大小元数据。

import boto3
s3 = boto3.resource("s3")
bucket = s3.Bucket(AWS_S3_BUCKET)
//prefix is the path following bucket_name
obj=bucket.objects.filter(Prefix= prefix )
for key in obj:
    file_size=round(key.size*1.0/1024, 2)
    print(file_size)




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...