|
@@ -2,6 +2,9 @@ package com.lqkj.link.util;
|
2
|
2
|
|
3
|
3
|
import com.aliyun.oss.OSS;
|
4
|
4
|
import com.aliyun.oss.OSSClientBuilder;
|
|
5
|
+import com.aliyun.oss.model.ListObjectsV2Request;
|
|
6
|
+import com.aliyun.oss.model.ListObjectsV2Result;
|
|
7
|
+import com.aliyun.oss.model.OSSObjectSummary;
|
5
|
8
|
import org.springframework.beans.factory.annotation.Autowired;
|
6
|
9
|
import org.springframework.stereotype.Component;
|
7
|
10
|
import org.springframework.web.multipart.MultipartFile;
|
|
@@ -13,6 +16,8 @@ import java.io.InputStream;
|
13
|
16
|
import java.time.LocalDate;
|
14
|
17
|
import java.time.LocalDateTime;
|
15
|
18
|
import java.time.LocalTime;
|
|
19
|
+import java.time.LocalDate;
|
|
20
|
+import java.util.List;
|
16
|
21
|
import java.util.UUID;
|
17
|
22
|
import java.util.concurrent.Callable;
|
18
|
23
|
|
|
@@ -38,6 +43,11 @@ public class AliOSSUtils {
|
38
|
43
|
String originalFilename = file.getName();
|
39
|
44
|
String fileName = "file/" + LocalDate.now() + "/" + UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
|
40
|
45
|
//上传文件到 OSS
|
|
46
|
+ try {
|
|
47
|
+ Thread.sleep(200);
|
|
48
|
+ } catch (InterruptedException e) {
|
|
49
|
+ throw new RuntimeException(e);
|
|
50
|
+ }
|
41
|
51
|
OSS ossClient = new OSSClientBuilder().build(aliProperties.getEndpoint(), aliProperties.getAccessKeyId(), aliProperties.getAccessKeySecret());
|
42
|
52
|
threadPoolUtil.getTaskExecutor().execute(() -> {
|
43
|
53
|
ossClient.putObject(aliProperties.getBucketName(), fileName, file);
|
|
@@ -70,5 +80,70 @@ public class AliOSSUtils {
|
70
|
80
|
}
|
71
|
81
|
}
|
72
|
82
|
|
73
|
|
-
|
|
83
|
+
|
|
84
|
+ public String addModel(MultipartFile file, String path) {
|
|
85
|
+ // 获取上传的文件的输入流
|
|
86
|
+ try {
|
|
87
|
+ InputStream inputStream = file.getInputStream();
|
|
88
|
+ // 避免文件覆盖
|
|
89
|
+ String originalFilename = file.getOriginalFilename();
|
|
90
|
+ String fileName = path + UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
|
|
91
|
+ //上传文件到 OSS
|
|
92
|
+ OSS ossClient = new OSSClientBuilder().build(aliProperties.getEndpoint(), aliProperties.getAccessKeyId(), aliProperties.getAccessKeySecret());
|
|
93
|
+ threadPoolUtil.getTaskExecutor().execute(() -> {
|
|
94
|
+ ossClient.putObject(aliProperties.getBucketName(), fileName, inputStream);
|
|
95
|
+ ossClient.shutdown();
|
|
96
|
+ });
|
|
97
|
+ //文件访问路径
|
|
98
|
+ return aliProperties.getOsspath().split("//")[0] + "//" + aliProperties.getBucketName() + "." + aliProperties.getOsspath().split("//")[1] + "/" + fileName;
|
|
99
|
+ } catch (IOException e) {
|
|
100
|
+ throw new RuntimeException(e);
|
|
101
|
+ }
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ public Long ossCheckCapacity(String path) {
|
|
105
|
+ OSS ossClient = new OSSClientBuilder().build(aliProperties.getEndpoint(), aliProperties.getAccessKeyId(), aliProperties.getAccessKeySecret());
|
|
106
|
+ return calculateFolderLength(ossClient, aliProperties.getBucketName(), path);
|
|
107
|
+ }
|
|
108
|
+
|
|
109
|
+ private static long calculateFolderLength(OSS ossClient, String bucketName, String folder) {
|
|
110
|
+ long size = 0L;
|
|
111
|
+ ListObjectsV2Result result = null;
|
|
112
|
+ do {
|
|
113
|
+ // MaxKey默认值为100,最大值为1000。
|
|
114
|
+ ListObjectsV2Request request = new ListObjectsV2Request(bucketName).withPrefix(folder).withMaxKeys(1000);
|
|
115
|
+ if (result != null) {
|
|
116
|
+ request.setContinuationToken(result.getNextContinuationToken());
|
|
117
|
+ }
|
|
118
|
+ result = ossClient.listObjectsV2(request);
|
|
119
|
+ List<OSSObjectSummary> sums = result.getObjectSummaries();
|
|
120
|
+ for (OSSObjectSummary s : sums) {
|
|
121
|
+ size += s.getSize();
|
|
122
|
+ }
|
|
123
|
+ } while (result.isTruncated());
|
|
124
|
+ return size;
|
|
125
|
+ try {
|
|
126
|
+ InputStream inputStream = file.getInputStream();
|
|
127
|
+ // 避免文件覆盖
|
|
128
|
+ String originalFilename = file.getOriginalFilename();
|
|
129
|
+ String fileName = "file/" + LocalDate.now() + "/" + UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
|
|
130
|
+ //上传文件到 OSS
|
|
131
|
+ OSS ossClient = new OSSClientBuilder().build(aliProperties.getEndpoint(), aliProperties.getAccessKeyId(), aliProperties.getAccessKeySecret());
|
|
132
|
+ threadPoolUtil.getTaskExecutor().execute(() -> {
|
|
133
|
+ ossClient.putObject(aliProperties.getBucketName(), fileName, inputStream);
|
|
134
|
+ ossClient.shutdown();
|
|
135
|
+ });
|
|
136
|
+ //文件访问路径
|
|
137
|
+ return aliProperties.getOsspath().split("//")[0] + "//" + aliProperties.getBucketName() + "." + aliProperties.getOsspath().split("//")[1] + "/" + fileName;
|
|
138
|
+ } catch (IOException e) {
|
|
139
|
+ throw new RuntimeException(e);
|
|
140
|
+ }
|
|
141
|
+ }
|
|
142
|
+
|
|
143
|
+ public void deleteModel(String path) {
|
|
144
|
+ OSS ossClient = new OSSClientBuilder().build(aliProperties.getEndpoint(), aliProperties.getAccessKeyId(), aliProperties.getAccessKeySecret());
|
|
145
|
+ String prefix = aliProperties.getOsspath().split("//")[0] + "//" + aliProperties.getBucketName() + "." + aliProperties.getOsspath().split("//")[1] + "/";
|
|
146
|
+ ossClient.deleteObject(aliProperties.getBucketName(), path.replace(prefix, ""));
|
|
147
|
+ }
|
|
148
|
+
|
74
|
149
|
}
|