阿里OSS获取文件列表和删除逻辑优化
This commit is contained in:
parent
f73d950599
commit
ba00a90412
@ -38,6 +38,7 @@ public class AliOssStorageOperator extends CommonPieceGetter<DeviceAliOssConfig>
|
|||||||
OSS ossClient = getClient();
|
OSS ossClient = getClient();
|
||||||
ListObjectsV2Request listObjectsV2Request = new ListObjectsV2Request(config.getBucketName());
|
ListObjectsV2Request listObjectsV2Request = new ListObjectsV2Request(config.getBucketName());
|
||||||
listObjectsV2Request.setPrefix(config.getPrefix() + prefix);
|
listObjectsV2Request.setPrefix(config.getPrefix() + prefix);
|
||||||
|
listObjectsV2Request.setMaxKeys(1000);
|
||||||
boolean isTruncated = true;
|
boolean isTruncated = true;
|
||||||
String continuationToken = null;
|
String continuationToken = null;
|
||||||
List<OSSObjectSummary> objectList = new ArrayList<>();
|
List<OSSObjectSummary> objectList = new ArrayList<>();
|
||||||
@ -94,16 +95,24 @@ public class AliOssStorageOperator extends CommonPieceGetter<DeviceAliOssConfig>
|
|||||||
if (objectList == null) {
|
if (objectList == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
DeleteObjectsRequest request = new DeleteObjectsRequest(config.getBucketName());
|
int idx = 0;
|
||||||
request.setKeys(objectList.stream().map(OSSObjectSummary::getKey).collect(Collectors.toList()));
|
int batchSize = 999;
|
||||||
try {
|
while (objectList.size() > idx) {
|
||||||
ossClient.deleteObjects(request);
|
if (objectList.size() - idx < batchSize) {
|
||||||
return true;
|
batchSize = objectList.size() - idx;
|
||||||
} catch (OSSException e) {
|
}
|
||||||
return false;
|
List<OSSObjectSummary> subList = objectList.subList(idx, idx + batchSize);
|
||||||
} finally {
|
idx += batchSize;
|
||||||
ossClient.shutdown();
|
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
|
@Override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user