检查S3上的文件大小而不下载?

时间:2010-10-11 21:24:10

标签: amazon-s3

我将客户文件上传到Amazon S3,我想添加一项功能来计算每个客户的文件大小。有没有办法“偷看”文件大小而不下载它们?我知道您可以从亚马逊控制面板查看,但我需要以编程方式进行查看。

15 个答案:

答案 0 :(得分:35)

向对象发送HTTP HEAD 请求。 HEAD请求将检索与GET请求相同的HTTP标头,但它不会检索对象的主体(节省带宽)。然后,您可以从HTTP响应标头中解析出Content-Length标头值。

答案 1 :(得分:7)

Node.js one:

const AWS = require('aws-sdk');
const s3 = new AWS.S3();

function sizeOf(key, bucket) {
    return s3.headObject({ Key: key, Bucket: bucket })
        .promise()
        .then(res => res.ContentLength);
}


// A test
sizeOf('ahihi.mp4', 'output').then(size => console.log(size));

Doc是here

答案 2 :(得分:6)

使用Michael的建议,我的成功代码如下所示:

require 'net/http'
require 'uri'

file_url = MyObject.first.file.url

url = URI.parse(file_url)
req = Net::HTTP::Head.new url.path
res = Net::HTTP.start(url.host, url.port) {|http|
  http.request(req)
}

file_length = res["content-length"]

答案 3 :(得分:2)

我在Python中做了类似的事情,以获得给定前缀下所有文件的累积大小:

import boto3

bucket = 'your-bucket-name'
prefix = 'some/s3/prefix/'

s3 = boto3.client('s3')

size = 0

result = s3.list_objects_v2(Bucket=bucket, Prefix=prefix)
size += sum([x['Size'] for x in result['Contents']])

while result['IsTruncated']:
    result = s3.list_objects_v2(
        Bucket=bucket, Prefix=prefix,
        ContinuationToken=result['NextContinuationToken'])
    size += sum([x['Size'] for x in result['Contents']])

print('Total size in MB: ' + str(size / (1000**2)))

答案 4 :(得分:1)

您还可以列出存储桶的内容。清单中的元数据包含所有对象的文件大小。这是它在AWS SDK for PHP中的实现方式。

答案 5 :(得分:1)

Android解决方案

整合aws sdk,你会得到一个非常直接的解决方案:

// ... put this in background thread
List<S3ObjectSummary> s3ObjectSummaries;
s3ObjectSummaries = s3.listObjects(registeredBucket).getObjectSummaries();
for (int i = 0; i < s3ObjectSummaries.size(); i++) {
    S3ObjectSummary s3ObjectSummary = s3ObjectSummaries.get(i);
    Log.d(TAG, "doInBackground: size " + s3ObjectSummary.getSize());
}
  • 以下是官方documentation
  • 的链接
  • 非常重要的是在AsyncTask中执行代码或任何方法让你进入后台线程,否则你会在ui线程上运行网络时遇到异常。

答案 6 :(得分:1)

有更好的解决方案。

$info = $s3->getObjectInfo($yourbucketName, $yourfilename);
print $info['size'];

答案 7 :(得分:1)

AWS SDK for .NET ---- ListObjectsRequest&amp; ListObjectsResponse

AmazonS3Client s3 = new AmazonS3Client();
SpaceUsed(s3, "putBucketNameHere");

static void SpaceUsed(AmazonS3Client s3Client, string bucketName)
    {
        ListObjectsRequest request = new ListObjectsRequest();
        request.BucketName = bucketName;
        ListObjectsResponse response = s3Client.ListObjects(request);
        long totalSize = 0;
        foreach (S3Object o in response.S3Objects)
        {
            totalSize += o.Size;
        }
        Console.WriteLine("Total Size of bucket " + bucketName + " is " +
            Math.Round(totalSize / 1024.0 / 1024.0, 2) + " MB");
    }

答案 8 :(得分:0)

用于检查s3对象大小(或任何其他对象标头)的PHP代码,请注意使用stream_context_set_default以确保它仅使用HEAD请求

stream_context_set_default(
            array(
                'http' => array(
                    'method' => 'HEAD'
                )
            )
        );

$headers = get_headers('http://s3.amazonaws.com/bucketname/filename.jpg', 1);
$headers = array_change_key_case($headers); 

$size = trim($headers['content-length'],'"'); 

答案 9 :(得分:0)

以下python代码将提供从s3单独打印的前1000个文件的大小:

import boto3

bucket = 'bucket_name'
prefix = 'prefix'

s3 = boto3.client('s3')
contents = s3.list_objects_v2(Bucket=bucket,  MaxKeys=1000, Prefix=prefix)['Contents']

for c in contents:
    print('Size (KB):', float(c['Size'])/1000)

答案 10 :(得分:0)

这是使用Java和Amazon提供的S3 Java库的任何人的解决方案。如果您使用的是com.amazonaws.services.s3.AmazonS3,则可以使用GetObjectMetadataRequest请求,该请求允许您查询对象的长度。

您必须使用的库是:

<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-s3 -->
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-s3</artifactId>
    <version>1.11.511</version>
</dependency>

进口:

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.*;

还有需要获取内容长度的代码:

GetObjectMetadataRequest metadataRequest = new GetObjectMetadataRequest(bucketName, fileName);
final ObjectMetadata objectMetadata = s3Client.getObjectMetadata(metadataRequest);
long contentLength = objectMetadata.getContentLength();

在执行上面的代码之前,您将需要构建S3客户端。这是一些示例代码:

AWSCredentials credentials = new BasicAWSCredentials(
            accessKey,
            secretKey
);
s3Client = AmazonS3ClientBuilder.standard()
            .withRegion(clientRegion)
            .withCredentials(new AWSStaticCredentialsProvider(credentials))
            .build();

答案 11 :(得分:0)

您可以简单地使用s3 ls命令:

aws s3 ls s3://mybucket --recursive --human-readable --summarize

输出

2013-09-02 21:37:53   10 Bytes a.txt
2013-09-02 21:37:53  2.9 MiB foo.zip
2013-09-02 21:32:57   23 Bytes foo/bar/.baz/a
2013-09-02 21:32:58   41 Bytes foo/bar/.baz/b
2013-09-02 21:32:57  281 Bytes foo/bar/.baz/c
2013-09-02 21:32:57   73 Bytes foo/bar/.baz/d
2013-09-02 21:32:57  452 Bytes foo/bar/.baz/e
2013-09-02 21:32:57  896 Bytes foo/bar/.baz/hooks/bar
2013-09-02 21:32:57  189 Bytes foo/bar/.baz/hooks/foo
2013-09-02 21:32:57  398 Bytes z.txt

Total Objects: 10
   Total Size: 2.9 MiB

参考:https://docs.aws.amazon.com/cli/latest/reference/s3/ls.html

答案 12 :(得分:0)

golange示例,相同的原理,再次运行头请求所涉及的对象:

func returnKeySizeInMB(bucketName string, key string) {
    output, err := svc.HeadObject(
        &s3.HeadObjectInput{
            Bucket: aws.String(bucketName),
            Key:    aws.String(key),
        })
    if err != nil {
        log.Fatalf("Unable to to send head request to item %q, %v", e.Detail.RequestParameters.Key, err)
    }

    return int(*output.ContentLength / 1024 / 1024)
}

答案 13 :(得分:0)

AWS C ++解决方案以获取文件大小

//! Step 1: create s3 client
Aws::S3::S3Client s3Client(cred, config); //!Used cred & config,You can use other options.

//! Step 2: Head Object request
Aws::S3::Model::HeadObjectRequest headObj;
headObj.SetBucket(bucket);
headObj.SetKey(key);

//! Step 3: read size from object header metadata
auto object = s3Client.HeadObject(headObj);
if (object.IsSuccess())
{
    fileSize = object.GetResultWithOwnership().GetContentLength();
}
else
{
    std::cout << "Head Object error: "
        << object .GetError().GetExceptionName() << " - "
        << object .GetError().GetMessage() << std::endl;
}

注意:请勿使用GetObject提取大小,而是读取文件以提取信息。

答案 14 :(得分:0)

带有 head_object

Ruby 解决方案:

require 'aws-sdk-s3'

s3 = Aws::S3::Client.new(
  region:               'us-east-1',     #or any other region
  access_key_id:        AWS_ACCESS_KEY_ID,
  secret_access_key:    AWS_SECRET_ACCESS_KEY
)

res = s3.head_object(bucket: bucket_name, key: object_key)
file_size = res[:content_length]