|
|
@@ -0,0 +1,106 @@
|
|
|
+package nju.seec.helper.util;
|
|
|
+
|
|
|
+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.PutObjectRequest;
|
|
|
+import lombok.Data;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import nju.seec.helper.enums.ExceptionType;
|
|
|
+import nju.seec.helper.exception.HelperException;
|
|
|
+import nju.seec.helper.util.cache.CaffeineCacheUtils;
|
|
|
+import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.io.InputStream;
|
|
|
+import java.net.URL;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+/**
|
|
|
+ * use oss
|
|
|
+ *
|
|
|
+ * @author cst
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+@Data
|
|
|
+@ConfigurationProperties("aliyun.oss")
|
|
|
+public class OssUtils {
|
|
|
+ private String endpoint;
|
|
|
+ private String accessKeyId;
|
|
|
+ private String accessKeySecret;
|
|
|
+ private String bucketName;
|
|
|
+ private long objectUrlLivingSeconds;
|
|
|
+ private String objectUrlCacheName;
|
|
|
+ private final CaffeineCacheUtils cacheUtils;
|
|
|
+
|
|
|
+ public void upload(String objectName, InputStream inputStream) {
|
|
|
+ OSS ossClient = getOss();
|
|
|
+ PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
|
|
|
+ try {
|
|
|
+ ossClient.putObject(putObjectRequest);
|
|
|
+ } catch (OSSException | ClientException e) {
|
|
|
+ log.error(e.getMessage());
|
|
|
+ throw HelperException.of(ExceptionType.ERROR, "文件上传失败");
|
|
|
+ } finally {
|
|
|
+ if (ossClient != null) {
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getUrl(String objectName) {
|
|
|
+ String cacheUrl = (String) cacheUtils.get(objectUrlCacheName, objectName);
|
|
|
+ if (cacheUrl != null) {
|
|
|
+ return cacheUrl;
|
|
|
+ }
|
|
|
+ OSS ossClient = getOss();
|
|
|
+ Date expiration = new Date(System.currentTimeMillis() + objectUrlLivingSeconds * 1000);
|
|
|
+ try {
|
|
|
+ URL url = ossClient.generatePresignedUrl(bucketName, objectName, expiration);
|
|
|
+ return url.toString();
|
|
|
+ } catch (OSSException | ClientException e) {
|
|
|
+ log.error(e.getMessage());
|
|
|
+ throw HelperException.of(ExceptionType.ERROR, "获取文件链接失败");
|
|
|
+ } finally {
|
|
|
+ if (ossClient != null) {
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void copy(String sourceObjectName, String destinationObjectName) {
|
|
|
+ OSS ossClient = getOss();
|
|
|
+ try {
|
|
|
+ ossClient.copyObject(bucketName, sourceObjectName, bucketName, destinationObjectName);
|
|
|
+ } catch (OSSException | ClientException e) {
|
|
|
+ log.error(e.getMessage());
|
|
|
+ throw HelperException.of(ExceptionType.ERROR, "文件复制失败");
|
|
|
+ } finally {
|
|
|
+ if (ossClient != null) {
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Async
|
|
|
+ public void delete(String objectName) {
|
|
|
+ OSS ossClient = getOss();
|
|
|
+ try {
|
|
|
+ ossClient.deleteObject(bucketName, objectName);
|
|
|
+ } catch (OSSException | ClientException e) {
|
|
|
+ log.error(e.getMessage());
|
|
|
+ throw HelperException.of(ExceptionType.ERROR, "文件删除失败");
|
|
|
+ } finally {
|
|
|
+ if (ossClient != null) {
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private OSS getOss() {
|
|
|
+ return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
+ }
|
|
|
+}
|