我在Google上搜索了关于获取S3存储桶元数据的高效方法,比如它的大小和其中文件的数量。我找到了这个链接,讨论了这个问题。但是,它是针对PHP和使用CloudWatch的AWS CLI的。我想知道是否有Java API可以获取S3存储桶元数据?
谢谢。
谢谢。
您可以在此处找到AWS S3 Java库的详细文档:
http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/overview-summary.html
回答您的问题,您可以使用getSize()来获取S3中对象的大小,并且您可以迭代所有文件以获取存储桶的大小。 S3不支持直接获取磁盘使用情况(即不通过遍历所有项),但您可以使用CloudWatch进行单个请求以获取所需数据。aws cloudwatch get-metric-statistics --namespace AWS/S3 --start-time 2016-01-01T10:00:00 --end-time 2016-02-12T01:00:00 --period 86400 --statistics Average --region us-east-1 --metric-name BucketSizeBytes --dimensions Name=BucketName,Value=www.streambrightdata.com Name=StorageType,Value=StandardStorage
{
"Datapoints": [
{
"Timestamp": "2016-02-05T10:00:00Z",
"Average": 54027423.0,
"Unit": "Bytes"
},
{
"Timestamp": "2016-02-03T10:00:00Z",
"Average": 52917504.0,
"Unit": "Bytes"
},
{
"Timestamp": "2016-02-04T10:00:00Z",
"Average": 53417421.0,
"Unit": "Bytes"
},
{
"Timestamp": "2016-02-07T10:00:00Z",
"Average": 54949563.0,
"Unit": "Bytes"
},
{
"Timestamp": "2016-02-01T10:00:00Z",
"Average": 24951965.0,
"Unit": "Bytes"
},
{
"Timestamp": "2016-02-02T10:00:00Z",
"Average": 28254636.0,
"Unit": "Bytes"
},
{
"Timestamp": "2016-02-06T10:00:00Z",
"Average": 54577328.0,
"Unit": "Bytes"
}
],
"Label": "BucketSizeBytes"
}
使用 awssdk 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()
方法来获取所需的所有信息。MinioClient
相同。@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();
}