|
|
@@ -10,6 +10,8 @@ import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import java.io.*;
|
|
|
+import java.net.URLDecoder;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.nio.file.FileVisitResult;
|
|
|
import java.nio.file.FileVisitor;
|
|
|
import java.nio.file.Files;
|
|
|
@@ -112,6 +114,36 @@ public class AliyunOss {
|
|
|
ossClient.deleteObject(bucketName, objectName);
|
|
|
}
|
|
|
|
|
|
+ public void deleteObjects(String bucketName, String prefix) {
|
|
|
+ // 删除目录及目录下的所有文件
|
|
|
+ String nextMarker = null;
|
|
|
+ ObjectListing objectListing = null;
|
|
|
+ do {
|
|
|
+ ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName)
|
|
|
+ .withPrefix(prefix)
|
|
|
+ .withMarker(nextMarker);
|
|
|
+ objectListing = ossClient.listObjects(listObjectsRequest);
|
|
|
+ if (objectListing.getObjectSummaries().size() > 0) {
|
|
|
+ List<String> keys = new ArrayList<>();
|
|
|
+ for (OSSObjectSummary s : objectListing.getObjectSummaries()) {
|
|
|
+ log.info("key name in bucket {} -> {}", bucketName, s.getKey());
|
|
|
+ keys.add(s.getKey());
|
|
|
+ }
|
|
|
+
|
|
|
+ /*DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName).withKeys(keys).withEncodingType("url");
|
|
|
+ DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(deleteObjectsRequest);
|
|
|
+ List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();
|
|
|
+ for(String obj : deletedObjects) {
|
|
|
+ String deleteObj = URLDecoder.decode(obj, StandardCharsets.UTF_8);
|
|
|
+ System.out.println(deleteObj);
|
|
|
+ log.info("delete object {} in bucket {}", deleteObj, bucketName);
|
|
|
+ }*/
|
|
|
+ }
|
|
|
+
|
|
|
+ nextMarker = objectListing.getNextMarker();
|
|
|
+ } while (objectListing.isTruncated());
|
|
|
+ }
|
|
|
+
|
|
|
public void close() {
|
|
|
ossClient.shutdown();
|
|
|
}
|