FileUtils.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package nju.seec.helper.util;
  2. import com.aliyun.oss.ClientException;
  3. import com.aliyun.oss.OSS;
  4. import com.aliyun.oss.OSSClientBuilder;
  5. import com.aliyun.oss.OSSException;
  6. import com.aliyun.oss.model.PutObjectRequest;
  7. import lombok.extern.slf4j.Slf4j;
  8. import nju.seec.helper.config.properties.OssProperties;
  9. import nju.seec.helper.util.enums.ExceptionType;
  10. import nju.seec.helper.util.exception.HelperException;
  11. import org.springframework.scheduling.annotation.Async;
  12. import org.springframework.stereotype.Component;
  13. import java.io.InputStream;
  14. import java.net.URL;
  15. import java.util.Date;
  16. /**
  17. * use oss
  18. *
  19. * @author cst
  20. */
  21. @Slf4j
  22. @Component
  23. public class FileUtils {
  24. private final OssProperties ossProperties;
  25. public FileUtils(OssProperties ossProperties) {
  26. this.ossProperties = ossProperties;
  27. }
  28. public void upload(String objectName, InputStream inputStream) {
  29. OSS ossClient = getOss();
  30. PutObjectRequest putObjectRequest = new PutObjectRequest(ossProperties.getBucketName(), objectName, inputStream);
  31. try {
  32. ossClient.putObject(putObjectRequest);
  33. } catch (OSSException | ClientException e) {
  34. log.error(e.getMessage());
  35. throw HelperException.of(ExceptionType.ERROR, "文件上传失败");
  36. } finally {
  37. if (ossClient != null) {
  38. ossClient.shutdown();
  39. }
  40. }
  41. }
  42. public String getUrl(String objectName, long seconds) {
  43. OSS ossClient = getOss();
  44. Date expiration = new Date(System.currentTimeMillis() + seconds * 1000);
  45. try {
  46. URL url = ossClient.generatePresignedUrl(ossProperties.getBucketName(), objectName, expiration);
  47. return url.toString();
  48. } catch (OSSException | ClientException e) {
  49. log.error(e.getMessage());
  50. throw HelperException.of(ExceptionType.ERROR, "获取文件链接失败");
  51. } finally {
  52. if (ossClient != null) {
  53. ossClient.shutdown();
  54. }
  55. }
  56. }
  57. public void copy(String sourceObjectName, String destinationObjectName) {
  58. OSS ossClient = getOss();
  59. try {
  60. ossClient.copyObject(ossProperties.getBucketName(), sourceObjectName, ossProperties.getBucketName(), destinationObjectName);
  61. } catch (OSSException | ClientException e) {
  62. log.error(e.getMessage());
  63. throw HelperException.of(ExceptionType.ERROR, "文件复制失败");
  64. } finally {
  65. if (ossClient != null) {
  66. ossClient.shutdown();
  67. }
  68. }
  69. }
  70. @Async
  71. public void delete(String objectName) {
  72. OSS ossClient = getOss();
  73. try {
  74. ossClient.deleteObject(ossProperties.getBucketName(), objectName);
  75. } catch (OSSException | ClientException e) {
  76. log.error(e.getMessage());
  77. throw HelperException.of(ExceptionType.ERROR, "文件删除失败");
  78. } finally {
  79. if (ossClient != null) {
  80. ossClient.shutdown();
  81. }
  82. }
  83. }
  84. private OSS getOss() {
  85. return new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getAccessKeyId(), ossProperties.getAccessKeySecret());
  86. }
  87. }