ps:文件下面还有文件夹,这代码不能完全保证是否遍历到所有该文件夹以及子文件夹的文件,因为不可能一点点上到服务器去数,只是代码上做到应该不会出现重复的文件夹以及出现死循环
java
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
System.out.println("CleanOssFileHandler凌晨一点清除的指定文件夹的文件开始执行时间:"+startTime);
//通过传入一个字符串参数,不同文件夹通过;来分割,每个文件夹的又通过|分割不同的参数,第一个参数是文件夹,第二个参数是保留天数,第三个参数是清除的文件类型
String params = "exportFile/|7|xls,zip;docfile/|7|xls,zip";
if (StrKit.isBlank(params)) {
return;
}
String endpoint = getAccessKey().get("endpoint") + "";//"oss-cn-beijing.aliyuncs.com";
String accessKeyId = getAccessKey().get("ossaccessKeyId") + "";
String accessKeySecret = getAccessKey().get("ossaccessKeySecret") + "";
String bucketName = getAccessKey().get("bucketName") + "";
// 创建 OSS 客户端实例
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
String[] split = params.split(";");
Set<String> filePath = new HashSet<>();
Set<String> foldPath = new HashSet<>();
for (String param : split) {
String[] split1 = param.split("\\|");
if (split1.length<3){
return;
}
//保留天数
int days = Integer.parseInt(split1[1]);
//保留天数转换成毫米
long millis = TimeUnit.DAYS.toMillis(days);
//获取文件类型
String fileType = split1[2];
//指定文件目录
String directory = split1[0];
deleteflieByFolderPath(directory,bucketName,ossClient,millis,fileType,filePath,foldPath);
}
long endTime = System.currentTimeMillis();
System.out.println("CleanOssFileHandler凌晨一点清除的指定文件夹的文件开始结束时间:" +endTime);
System.out.println("CleanOssFileHandler凌晨一点清除的指定文件夹的文件本次执行花费时间为:" +(endTime-startTime));
System.out.println(filePath.size());
System.out.println(foldPath.size());
//关闭 OSS 客户端
ossClient.shutdown();
}
//获取文件夹下的文件
public static void deleteflieByFolderPath(String path,String bucketName,OSS ossClient,long millis,String fileType, Set<String> filePath,Set<String> forlderPath){
//获取文件夹
ListObjectsRequest listObjectsRequest1 = new ListObjectsRequest(bucketName);
listObjectsRequest1.setPrefix(path);
listObjectsRequest1.setMaxKeys(1000); // 设置每次返回的最大文件数,默认值为1000
listObjectsRequest1.withDelimiter("/");
ObjectListing objectListing1 = ossClient.listObjects(listObjectsRequest1);
List<OSSObjectSummary> objectSummaries = objectListing1.getObjectSummaries();
if (CollectionUtil.isNotEmpty(objectSummaries)){
for (OSSObjectSummary s : objectSummaries) {
String key = "oss的域名" + s.getKey();
int length = key.length();
int i = key.lastIndexOf("/") + 1;
if (i != length) {
File file = new File(key);
String fileType1 = file.getName().split("\\.")[1];
System.out.println(s.getLastModified());
if (DateUtils.getCurrentTime() - s.getLastModified().getTime() > millis&&fileType.contains(fileType1)) {
System.out.println(key);
//这里为了测试是否有相同的文件
if (filePath.contains(key)){
System.out.println("重复的文件!!!");
return;
}
filePath.add(key);
fileDelete(key);
}
}
}
}
for (String commonPrefix : objectListing1.getCommonPrefixes()) {
if (commonPrefix.contains("//")){
continue;
}
//这里为了测试是否有相同的文件夹
if (forlderPath.contains(commonPrefix)){
return;
}
forlderPath.add(commonPrefix);
deleteflieByFolderPath(commonPrefix,bucketName,ossClient,millis,fileType,filePath,forlderPath);
}
}
private static Map<String,Object> getAccessKey() {
Properties properties = new Properties();
Map<String, Object> data = new HashMap<>();
try {
ClassPathResource resource = new ClassPathResource("\\xx.properties");
properties = PropertiesLoaderUtils.loadProperties(resource);
if (properties.get("ossaccessKeyId")==null||"".equals(properties.get("ossaccessKeyId"))){
throw new AposException(9999, "ossaccessKeyId不能为空!");
}
if (properties.get("ossaccessKeySecret")==null||"".equals(properties.get("ossaccessKeySecret"))){
throw new AposException(9999, "ossaccessKeySecret不能为空!");
}
if (properties.get("endpoint")==null||"".equals(properties.get("endpoint"))){
throw new AposException(9999, "endpoint不能为空!");
}
if (properties.get("bucketName")==null||"".equals(properties.get("bucketName"))){
throw new AposException(9999, "bucketName!");
}
data.put("endpoint",properties.get("endpoint"));
data.put("bucketName",properties.get("bucketName"));
data.put("ossaccessKeyId",properties.get("ossaccessKeyId"));
data.put("ossaccessKeySecret",properties.get("ossaccessKeySecret"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
/**
* 删除指定文件
* @param
* @return
*/
public static void fileDelete(String ossUrl) {
String endpoint = getAccessKey().get("endpoint")+"";//"oss-cn-beijing.aliyuncs.com";
String accessKeyId = getAccessKey().get("ossaccessKeyId")+"";;
String accessKeySecret = getAccessKey().get("ossaccessKeySecret")+"";
String bucketName = getAccessKey().get("bucketName")+"";
// 填写文件完整路径。文件完整路径中不能包含Bucket名称。
String objectName = ossUrl;
//String yuming = http+bucketName+"."+ endpoint+"/";
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
objectName = objectName.replace("域名","");
try {
boolean b = ossClient.doesObjectExist(bucketName, objectName);
// 删除文件或目录。如果要删除目录,目录必须为空。
if (!b){
System.out.println("文件已删除");
}
ossClient.deleteObject(bucketName, objectName);
} catch (OSSException oe) {
throw new ServiceException("文件删除失败:"+oe.getErrorMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
最后本地测试的结果:
大概两万个文件,需要花费四五分钟。(没包括删除的时间)