[英]Size of file stored on the Amazon S3 bucket
I have following model我有以下型号
class MediaFile(Media):
s3_file = GenericFileField(tag="s3-tag", null=True, blank=True, max_length=300)
How can I get size of file stored on s3?如何获取存储在 s3 上的文件大小?
I tried this, but it's not work.我试过这个,但它不起作用。
def file_size(self):
try:
prefix = get_file_key(self.s3_file)
s3 = boto3.resource("s3")
bucket = s3.Bucket()
return bucket.Object(prefix).content_length
except:
pass
To access an existing Bucket using boto3, you need to supply the bucket name, for example:要使用 boto3 访问现有存储桶,您需要提供存储桶名称,例如:
import boto3
s3 = boto3.resource("s3")
bucket = s3.Bucket('mybucket')
length = bucket.Object('cats/persian.jpg').content_length
Alternatively:或者:
import boto3
s3 = boto3.resource("s3")
length = s3.Object('mybucket', 'cats/persian.jpg').content_length
FYI the value passed to Bucket.Object()
is a key (like cats/persian.jpg
), not a prefix (like cats/
).仅供参考,传递给
Bucket.Object()
的值是一个键(如cats/persian.jpg
),而不是前缀(如cats/
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.