From a4c019ace2841faa6af14b5373a041464c3dbbde Mon Sep 17 00:00:00 2001 From: 疯狂的狮子li <15040126243@163.com> Date: 星期二, 20 七月 2021 11:12:22 +0800 Subject: [PATCH] update OSS接口增加通用contentType --- ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/AliyunCloudStorageServiceImpl.java | 31 ++++++++++++++++++------------- 1 files changed, 18 insertions(+), 13 deletions(-) diff --git a/ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/AliyunCloudStorageServiceImpl.java b/ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/AliyunCloudStorageServiceImpl.java index 7ac46e1..b483eba 100644 --- a/ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/AliyunCloudStorageServiceImpl.java +++ b/ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/AliyunCloudStorageServiceImpl.java @@ -3,6 +3,7 @@ import com.aliyun.oss.ClientConfiguration; import com.aliyun.oss.OSSClient; import com.aliyun.oss.common.auth.DefaultCredentialProvider; +import com.ruoyi.oss.entity.UploadResult; import com.ruoyi.oss.enumd.CloudServiceEnumd; import com.ruoyi.oss.exception.OssException; import com.ruoyi.oss.factory.OssFactory; @@ -32,11 +33,15 @@ @Autowired public AliyunCloudStorageServiceImpl(CloudStorageProperties properties) { this.properties = properties.getAliyun(); - ClientConfiguration configuration = new ClientConfiguration(); - DefaultCredentialProvider credentialProvider = new DefaultCredentialProvider( - this.properties.getAccessKeyId(), - this.properties.getAccessKeySecret()); - client = new OSSClient(this.properties.getEndpoint(), credentialProvider, configuration); + try { + ClientConfiguration configuration = new ClientConfiguration(); + DefaultCredentialProvider credentialProvider = new DefaultCredentialProvider( + this.properties.getAccessKeyId(), + this.properties.getAccessKeySecret()); + client = new OSSClient(this.properties.getEndpoint(), credentialProvider, configuration); + } catch (Exception e) { + throw new IllegalArgumentException("闃块噷浜戝瓨鍌ㄩ厤缃敊璇�! 璇锋鏌ョ郴缁熼厤缃�!"); + } } @Override @@ -45,18 +50,18 @@ } @Override - public String upload(byte[] data, String path) { - return upload(new ByteArrayInputStream(data), path); + public UploadResult upload(byte[] data, String path, String contentType) { + return upload(new ByteArrayInputStream(data), path, contentType); } @Override - public String upload(InputStream inputStream, String path) { + public UploadResult upload(InputStream inputStream, String path, String contentType) { try { client.putObject(this.properties.getBucketName(), path, inputStream); } catch (Exception e) { throw new OssException("涓婁紶鏂囦欢澶辫触锛岃妫�鏌ラ厤缃俊鎭�"); } - return this.properties.getEndpoint() + "/" + path; + return new UploadResult().setUrl(properties.getEndpoint() + "/" + path).setFilename(path); } @Override @@ -70,13 +75,13 @@ } @Override - public String uploadSuffix(byte[] data, String suffix) { - return upload(data, getPath(this.properties.getPrefix(), suffix)); + public UploadResult uploadSuffix(byte[] data, String suffix, String contentType) { + return upload(data, getPath(this.properties.getPrefix(), suffix), contentType); } @Override - public String uploadSuffix(InputStream inputStream, String suffix) { - return upload(inputStream, getPath(this.properties.getPrefix(), suffix)); + public UploadResult uploadSuffix(InputStream inputStream, String suffix, String contentType) { + return upload(inputStream, getPath(this.properties.getPrefix(), suffix), contentType); } @Override -- Gitblit v1.9.3