|
@@ -1,5 +1,7 @@
|
|
|
package com.gis.oss.util;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import com.aliyun.oss.ClientException;
|
|
|
import com.aliyun.oss.OSSClient;
|
|
|
import com.aliyun.oss.model.*;
|
|
|
import com.gis.common.constant.ConfigConstant;
|
|
@@ -7,17 +9,13 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
-import java.io.ByteArrayInputStream;
|
|
|
-import java.io.File;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.io.*;
|
|
|
+import java.net.URLDecoder;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* 阿里云oss工具类
|
|
|
+ * 参考:https://help.aliyun.com/document_detail/84842.htm?spm=a2c4g.11186623.0.0.606a282dA00Gt3#t22290.html
|
|
|
*/
|
|
|
@Slf4j
|
|
|
@Component
|
|
@@ -137,7 +135,7 @@ public class AliYunOssUtil {
|
|
|
* @param prefix 图片路径
|
|
|
* @return
|
|
|
*/
|
|
|
- public int deleteFile(String prefix) {
|
|
|
+ public int deleteDir(String prefix) {
|
|
|
OSSClient ossClient = init();
|
|
|
ObjectListing objectListing = ossClient.listObjects(configConstant.ossBucket, prefix);
|
|
|
List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
|
|
@@ -151,6 +149,66 @@ public class AliYunOssUtil {
|
|
|
return sums.size();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 2023-1-31
|
|
|
+ * 删除单个文件, 不能删除目录
|
|
|
+ * @param ossKey 填写文件完整路径。文件完整路径中不能包含Bucket名称。
|
|
|
+ * 同时也不能以“/”或者“\”开头。
|
|
|
+ */
|
|
|
+ public void deleteFile(String ossKey) {
|
|
|
+ OSSClient ossClient = init();
|
|
|
+ try {
|
|
|
+ ossClient.deleteObject(configConstant.ossBucket, ossKey);
|
|
|
+ log.info("删除oss文件完成:{}", ossKey);
|
|
|
+ } catch (ClientException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public void deleteOssFile(String prefix){
|
|
|
+ int maxKeys = 200;
|
|
|
+ OSSClient ossClient = init();
|
|
|
+ try {
|
|
|
+
|
|
|
+ String nextMarker = null;
|
|
|
+ ObjectListing objectListing;
|
|
|
+
|
|
|
+ do {
|
|
|
+ objectListing = ossClient.listObjects(new ListObjectsRequest(configConstant.ossBucket).withPrefix(prefix).withMarker(nextMarker).withMaxKeys(maxKeys));
|
|
|
+ List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
|
|
|
+ if (CollUtil.isEmpty(sums)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<String> keys = new ArrayList<>();
|
|
|
+ for (OSSObjectSummary sum : sums) {
|
|
|
+ keys.add(sum.getKey());
|
|
|
+ }
|
|
|
+ DeleteObjectsRequest deleteObjectsRequest =
|
|
|
+ new DeleteObjectsRequest(configConstant.ossBucket).withKeys(keys).withEncodingType("url");
|
|
|
+ DeleteObjectsResult deleteObjectsResult = ossClient
|
|
|
+ .deleteObjects(deleteObjectsRequest);
|
|
|
+ List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();
|
|
|
+ try {
|
|
|
+ for (String deletedObject : deletedObjects) {
|
|
|
+ String decode = URLDecoder.decode(deletedObject, "UTF-8");
|
|
|
+ log.info("删除oss文件:{}", decode);
|
|
|
+ }
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ }while (objectListing.isTruncated());
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }finally {
|
|
|
+ if(ossClient != null){
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public void main(String[] args) throws IOException {
|
|
|
|
|
|
HashMap<String, String> map = new HashMap<>();
|