可以使用SDK,指定文件夹路径列举所有文件夹下对象,获取文件大小,再累加。
以java SDK举例说明,查询桶bucketA下文件夹fileFolder的总大小。
public static void main(String[] args) {
String endpoint ="";
String ak = ""; //访问密钥ID,可通过控制台-密钥管理获取
String sk = "";//私有访问密钥,可通过控制台-密钥管理获取
String bucketName = "bucketA";
String fileFolder = "fileFolder";
Long fileFolderSize = 0L;
AmazonS3ClientBuilder amazonS3ClientBuilder =AmazonS3ClientBuilder.standard();
AWSCredentials credentials = new BasicAWSCredentials(ak, sk);
ClientConfiguration clientConfiguration = new ClientConfiguration();
String signingRegion = "cn-north-1";
AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(endpoint, signingRegion);
AmazonS3 s3Client = amazonS3ClientBuilder.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withClientConfiguration(clientConfiguration).withEndpointConfiguration(endpointConfiguration).build();
ListObjectsRequest req = new ListObjectsRequest();
req.setBucketName(bucketName);
req.setDelimiter("/");
req.setPrefix(fileFolder);
ObjectListing objectListing=s3Client.listObjects(req);
do{
for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
fileFolderSize += objectSummary.getSize();
}
objectListing=s3Client.listNextBatchOfObjects(objectListing);
}while (objectListing.isTruncated());
System.out.println("文件夹大小为:" + fileFolderSize);
}