English 中文(简体)
Aws利用java api
原标题:Aws get S3 Bucket Size using java api

我已探索了有效途径,获取S3桶的元数据,如其大小和档案数量。 见http://serverfault.com/questions/84815/how-can-i-get-the-size-of-an-amazon-s3-bucket>。 联系讨论此类问题。 但是,使用云层观察,它需要PHP和 a。 我想知道,有几只 j瓦皮,以 f取3 b子元数据吗?

增 编

最佳回答
问题回答

awsdk Java2.x

    Set<String> fileTypes = new HashSet<>();
    ListObjectsResponse listObjResp = amazonS3Client.listObjects(ListObjectsRequest.builder().bucket(bucketName).build());
    int iCount=1;
    //********************************************************************//
    log.info("listObjResp.isTruncated() : "+listObjResp.isTruncated());
    String nextMarker = null;
    do {
        String sKey = null;
        List<S3Object> s3ObjList = listObjResp.contents();
        for (S3Object s3Obj: s3ObjList) {
            sKey = s3Obj.key();
            String[] sKeyValues = sKey.split("\.");
            if(sKeyValues.length==2) {
                fileTypes.add(sKeyValues[1]);
            }else {
                fileTypes.add(NO_FILE_EXT);
            }
            ++iCount;
        }
        nextMarker = listObjResp.nextMarker();
        log.debug("listObjResp.nextMarker() : "+nextMarker);
        listObjResp = amazonS3Client.listObjects(ListObjectsRequest.builder().bucket(bucketName).marker(nextMarker).build());
    } while (nextMarker !=null);
    
    log.info("iCount of  "+bucketName+" : "+(iCount-1));

您可使用<代码>MinioAdminClient及其getDataUsageInfo(方法,以获得你们所需要的所有信息。

It can be connected via this link and configured in the same way as MinioClient by passing parameters with the help of a builder instead of a constructor.

@Bean
public MinioClient minioClient(
        @Value("${aws.endPoint}") String endPoint,
        @Value("${aws.accessKey}") String accessKey,
        @Value("${aws.secretKey}") String secretKey) throws InvalidPortException, InvalidEndpointException {

    return new MinioClient(endPoint, accessKey, secretKey);
}

@Bean
public MinioAdminClient minioAdminClient(
        @Value("${aws.endPoint}") String endPoint,
        @Value("${aws.accessKey}") String accessKey,
        @Value("${aws.secretKey}") String secretKey){

    return MinioAdminClient
            .builder()
            .endpoint(endPoint)
            .credentials(accessKey, secretKey)
            .build();
}

与Gite Hub Repo的档案链接:https://github.com/minio-java/blob/master/adminapi/rc/main/java/minio/admin/MinioAdminClient.java3

你们需要这个桶子,但这一解决办法依赖于AWS云层,因为S3目前并不直接支持getting水的大小(Feb 2024)。

With Amazon CloudWatch to get the stats of an s3 bucket (which includes size as a dimension) it would be something like this (needs some refactoring):

  package com.allpalabs.haces.config;

import com.amazonaws.services.cloudwatch.AmazonCloudWatch;
import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder;
import com.amazonaws.services.cloudwatch.model.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class AwsCloudWatchMetricClient {
    public static final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient();
    public static final String namespace = "AWS/S3";
    public static final String metricName = "BucketSizeBytes";

    public static double getAwsCloudWatchS3Metric(String bucketName) throws ResourceNotFoundException
    {
        double bucketStorageUsedInBytes = -1;

        final GetMetricDataRequest getMetricDataRequest = new GetMetricDataRequest();
        final MetricDataQuery metricDataQuery = new MetricDataQuery();
        metricDataQuery.setId("s3StorageMetric");
        final MetricStat metricStat = new MetricStat();
        final Metric metric = new Metric();
        metric.setNamespace(namespace);

        final Dimension dimensionStorageType = new Dimension();
        dimensionStorageType.setName("StorageType");
        dimensionStorageType.setValue("StandardStorage");

        final Dimension dimensionBucketName = new Dimension();
        dimensionBucketName.setName("BucketName");
        dimensionBucketName.setValue(bucketName);

        List<Dimension> dimensions = new ArrayList<>();
        dimensions.add(dimensionBucketName);
        dimensions.add(dimensionStorageType);

        metric.setDimensions(dimensions);
        metric.setMetricName(metricName);
        metricStat.setMetric(metric);
        metricStat.setPeriod(86400);
        metricStat.setStat("Maximum");
        metricDataQuery.setMetricStat(metricStat);
        getMetricDataRequest.setMetricDataQueries(Collections.singletonList(metricDataQuery));
        final Date endTime = new Date();
        getMetricDataRequest.setEndTime(endTime);
        getMetricDataRequest.setStartTime(new Date(endTime.getTime() - (3600*100000)));


        GetMetricDataResult response = cw.getMetricData(getMetricDataRequest);

        boolean done = false;


        while(!done) {
//            ListMetricsResult response = cw.listMetrics(request);

            for(MetricDataResult metricGathered : response.getMetricDataResults() ) {
                System.out.printf(
                        "Retrieved metric data is %s", metricGathered.getValues());
                System.out.println(metricGathered.toString());
                System.out.println(metricGathered.getMessages());

                List<Double> vals = metricGathered.getValues();

                if(!vals.isEmpty()) {
                    bucketStorageUsedInBytes = vals.get(vals.size() - 1);
                    return bucketStorageUsedInBytes;
                }else {
                    throw new ResourceNotFoundException("Problem adquiring S3 Maximum storage values for this bucket.");
                }

            }

            response.setNextToken(response.getNextToken());

            if(response.getNextToken() == null) {
                done = true;
            }
        }

        return bucketStorageUsedInBytes;
    }

}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签