阿里OSS获取文件列表和删除逻辑优化

This commit is contained in:
Jerry Yan 2024-12-13 09:42:53 +08:00
parent f73d950599
commit ba00a90412

View File

@ -38,6 +38,7 @@ public class AliOssStorageOperator extends CommonPieceGetter<DeviceAliOssConfig>
OSS ossClient = getClient();
ListObjectsV2Request listObjectsV2Request = new ListObjectsV2Request(config.getBucketName());
listObjectsV2Request.setPrefix(config.getPrefix() + prefix);
listObjectsV2Request.setMaxKeys(1000);
boolean isTruncated = true;
String continuationToken = null;
List<OSSObjectSummary> objectList = new ArrayList<>();
@ -94,16 +95,24 @@ public class AliOssStorageOperator extends CommonPieceGetter<DeviceAliOssConfig>
if (objectList == null) {
return false;
}
DeleteObjectsRequest request = new DeleteObjectsRequest(config.getBucketName());
request.setKeys(objectList.stream().map(OSSObjectSummary::getKey).collect(Collectors.toList()));
try {
ossClient.deleteObjects(request);
return true;
} catch (OSSException e) {
return false;
} finally {
ossClient.shutdown();
int idx = 0;
int batchSize = 999;
while (objectList.size() > idx) {
if (objectList.size() - idx < batchSize) {
batchSize = objectList.size() - idx;
}
List<OSSObjectSummary> subList = objectList.subList(idx, idx + batchSize);
idx += batchSize;
DeleteObjectsRequest request = new DeleteObjectsRequest(config.getBucketName());
request.setKeys(subList.stream().map(OSSObjectSummary::getKey).collect(Collectors.toList()));
try {
ossClient.deleteObjects(request);
} catch (OSSException e) {
return false;
}
}
ossClient.shutdown();
return true;
}
@Override