|
|
@@ -1,98 +0,0 @@
|
|
|
-package nju.seec.helper.util.oss;
|
|
|
-
|
|
|
-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 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;
|
|
|
-
|
|
|
- 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, long seconds) {
|
|
|
- OSS ossClient = getOss();
|
|
|
- Date expiration = new Date(System.currentTimeMillis() + seconds * 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);
|
|
|
- }
|
|
|
-}
|