|
@@ -5,24 +5,29 @@ import cn.hutool.core.date.DateTime;
|
|
|
import cn.hutool.core.io.IoUtil;
|
|
|
import cn.hutool.core.io.file.FileNameUtil;
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
+import com.aliyun.oss.ClientException;
|
|
|
import com.aliyun.oss.OSS;
|
|
|
import com.aliyun.oss.OSSClientBuilder;
|
|
|
+import com.aliyun.oss.OSSException;
|
|
|
+import com.aliyun.oss.model.*;
|
|
|
import lombok.SneakyThrows;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.http.util.TextUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
import java.io.InputStream;
|
|
|
-import java.net.HttpURLConnection;
|
|
|
-import java.net.URI;
|
|
|
-import java.net.URL;
|
|
|
-import java.net.URLEncoder;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.net.*;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class OSSService {
|
|
|
private final OSS ossClient = new OSSClientBuilder().build("https://oss-cn-chengdu.aliyuncs.com",
|
|
|
"LTAI4GEBqfF1GX4VwsYU2Wpg",
|
|
@@ -98,6 +103,56 @@ public class OSSService {
|
|
|
return "https://cxzx.smcic.net/" + encodedString;
|
|
|
}
|
|
|
|
|
|
+ public void bidDel(Long id){
|
|
|
+ String prefix = "open/bid/" + id + "/";
|
|
|
+ delete(prefix);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 删除oss文件
|
|
|
+ public void delete(String prefix) {
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 删除目录及目录下的所有文件。
|
|
|
+ String nextMarker = null;
|
|
|
+ ObjectListing objectListing = null;
|
|
|
+ do {
|
|
|
+ ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucket)
|
|
|
+ .withPrefix(prefix)
|
|
|
+ .withMarker(nextMarker);
|
|
|
+
|
|
|
+ objectListing = ossClient.listObjects(listObjectsRequest);
|
|
|
+ if (objectListing.getObjectSummaries().size() > 0) {
|
|
|
+ List<String> keys = new ArrayList<String>();
|
|
|
+ for (OSSObjectSummary s : objectListing.getObjectSummaries()) {
|
|
|
+ log.info("key name: " + s.getKey());
|
|
|
+ keys.add(s.getKey());
|
|
|
+ }
|
|
|
+ DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucket).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);
|
|
|
+ log.info(deleteObj);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ nextMarker = objectListing.getNextMarker();
|
|
|
+ } while (objectListing.isTruncated());
|
|
|
+ } catch (OSSException oe) {
|
|
|
+ log.error("Caught an OSSException, which means your request made it to OSS, "
|
|
|
+ + "but was rejected with an error response for some reason.");
|
|
|
+ log.error("Error Message:" + oe.getErrorMessage());
|
|
|
+ log.error("Error Code:" + oe.getErrorCode());
|
|
|
+ log.error("Request ID:" + oe.getRequestId());
|
|
|
+ log.error("Host ID:" + oe.getHostId());
|
|
|
+ } catch (ClientException ce) {
|
|
|
+ log.error("Caught an ClientException, which means the client encountered "
|
|
|
+ + "a serious internal problem while trying to communicate with OSS, "
|
|
|
+ + "such as not being able to access the network.");
|
|
|
+ log.error("Error Message:" + ce.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
}
|
|
|
|