一、前言
1.阿里云的对象存储oss的java api中没有直接获取bucket容量使用情况的接口,度娘N久之后整理出一种间接统计方法:即递归获取根目录下所有文件大小,相加后得出已用容量。
2.网上还有一种方法:通过OSS监控服务获取使用情况,有兴趣的朋友可以试试,
地址链接:https://developer.aliyun.com/ask/2278
二、正文
1.引入maven依赖
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.8.2</version>
</dependency>
2.示例代码
package com.hzjp.dev.util;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.*;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.arms.model.v20190808.QueryMetricRequest;
import com.aliyuncs.arms.model.v20190808.QueryMetricResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import java.io.InputStream;
import java.util.*;
/**
* Created by Administrator on 2018/5/9.
*/
@Slf4j
public class OssUtils {
private static String ENDPOINT = "your oss point";
private static String ACCESSKEYID = "your accesskeyid";
private static String ACCESSKEYSECRET = "your accesskeysecret";
private static String NEW_BUCKET = "your bucket name";
private static OSSClient client = null;
/** 创建OSSClient实例
*
* @return
*/
private static OSSClient getOssClient(){
client = new OSSClient(ENDPOINT, ACCESSKEYID, ACCESSKEYSECRET);
return client;
}
/** 关闭OSSClient
*
*/
private static void close(){
OssUtils.getOssClient().shutdown();
}
/**
* 获取根目录下所有文件大小
* @param bucketName
* @return
*/
public static Long getOss(String bucketName){
try {
if (StringUtils.isBlank(bucketName)) {
bucketName = NEW_BUCKET;
}
long sizeTotal = 0L;
ObjectListing objectListing = null;
do {
// 列举bucket所有文件或目录,每次1000个
ListObjectsRequest request = new ListObjectsRequest(bucketName).withDelimiter("/").withMaxKeys(1000);
// 列举bucket的指定目录(yourPrefix)下文件,每次1000个
// ListObjectsRequest request = new ListObjectsRequest(bucketName).withMaxKeys(1000).withPrefix("yourPrefix");
if (objectListing != null) {
request.setMarker(objectListing.getNextMarker());
}
objectListing = OssUtils.getOssClient().listObjects(request);
//获取当前文件夹下所有子目录大小
List<String> folders = objectListing.getCommonPrefixes();
for (String folder : folders) {
sizeTotal = calculateFolderLength(OssUtils.getOssClient(), bucketName, folder)+sizeTotal;
}
//获取当前文件夹下所有文件大小
List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
if (sums.size()==0){
continue;
}
for (OSSObjectSummary s : sums) {
sizeTotal = sizeTotal+s.getSize();
}
} while (objectListing.isTruncated());
//转换为TB
sizeTotal = sizeTotal/1024/1024/1024/1024;
log.info(" 文件夹下所有子目录大小 : [{}] TB",sizeTotal);
return sizeTotal;
}catch (Exception e){
e.printStackTrace();
return 0L;
}finally {
OssUtils.close();
}
}
private static long calculateFolderLength(OSS ossClient, String bucketName, String folder) {
long size = 0L;
ObjectListing objectListing = null;
do {
// MaxKey默认值为100,最大值为1000。
ListObjectsRequest request = new ListObjectsRequest(bucketName).withPrefix(folder).withMaxKeys(1000);
if (objectListing != null) {
request.setMarker(objectListing.getNextMarker());
}
objectListing = ossClient.listObjects(request);
List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
for (OSSObjectSummary s : sums) {
size += s.getSize();
}
} while (objectListing.isTruncated());
return size;
}
}