| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- 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.extern.slf4j.Slf4j;
- import nju.seec.helper.config.properties.OssProperties;
- import nju.seec.helper.util.enums.ExceptionType;
- import nju.seec.helper.util.exception.HelperException;
- 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
- public class FileUtils {
- private final OssProperties ossProperties;
- public FileUtils(OssProperties ossProperties) {
- this.ossProperties = ossProperties;
- }
- public void upload(String objectName, InputStream inputStream) {
- OSS ossClient = getOss();
- PutObjectRequest putObjectRequest = new PutObjectRequest(ossProperties.getBucketName(), 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(ossProperties.getBucketName(), 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(ossProperties.getBucketName(), sourceObjectName, ossProperties.getBucketName(), 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(ossProperties.getBucketName(), 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(ossProperties.getEndpoint(), ossProperties.getAccessKeyId(), ossProperties.getAccessKeySecret());
- }
- }
|