From 7114fc27207ea8f8aca86dd91ff2acfe470c9349 Mon Sep 17 00:00:00 2001 From: 疯狂的狮子li <15040126243@163.com> Date: 星期五, 23 七月 2021 18:37:39 +0800 Subject: [PATCH] update OSS模块 更新 阿里云 腾讯云 相关代码 重构部分代码结构 --- ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/MinioCloudStorageServiceImpl.java | 57 +++++++++++++++++++++++++++++++++++---------------------- 1 files changed, 35 insertions(+), 22 deletions(-) diff --git a/ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/MinioCloudStorageServiceImpl.java b/ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/MinioCloudStorageServiceImpl.java index e53c77c..70ed684 100644 --- a/ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/MinioCloudStorageServiceImpl.java +++ b/ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/MinioCloudStorageServiceImpl.java @@ -1,5 +1,6 @@ package com.ruoyi.oss.service.impl; +import cn.hutool.core.util.StrUtil; import com.ruoyi.oss.entity.UploadResult; import com.ruoyi.oss.enumd.CloudServiceEnumd; import com.ruoyi.oss.enumd.PolicyType; @@ -12,6 +13,7 @@ import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; +import org.springframework.http.MediaType; import org.springframework.stereotype.Service; import java.io.ByteArrayInputStream; @@ -37,18 +39,28 @@ .endpoint(this.properties.getEndpoint()) .credentials(this.properties.getAccessKey(), this.properties.getSecretKey()) .build(); - String bucketName = this.properties.getBucketName(); - boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build()); - // 涓嶅瓨鍦ㄥ氨鍒涘缓妗� - if (!exists) { - minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build()); - minioClient.setBucketPolicy(SetBucketPolicyArgs.builder() - .bucket(bucketName) - .config(getPolicy(bucketName, PolicyType.READ)) - .build()); - } + createBucket(); } catch (Exception e) { throw new IllegalArgumentException("Minio瀛樺偍閰嶇疆閿欒! 璇锋鏌ョ郴缁熼厤缃�!"); + } + } + + @Override + public void createBucket() { + try { + String bucketName = properties.getBucketName(); + boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build()); + if (exists) { + return; + } + // 涓嶅瓨鍦ㄥ氨鍒涘缓妗� + minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build()); + minioClient.setBucketPolicy(SetBucketPolicyArgs.builder() + .bucket(bucketName) + .config(getPolicy(bucketName, PolicyType.READ)) + .build()); + } catch (Exception e) { + throw new OssException("鍒涘缓Bucket澶辫触, 璇锋牳瀵筂inio閰嶇疆淇℃伅"); } } @@ -58,28 +70,28 @@ } @Override - public UploadResult 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 UploadResult upload(InputStream inputStream, String path) { + public UploadResult upload(InputStream inputStream, String path, String contentType) { try { minioClient.putObject(PutObjectArgs.builder() .bucket(properties.getBucketName()) .object(path) - .contentType("application/octet-stream") + .contentType(StrUtil.blankToDefault(contentType, MediaType.APPLICATION_OCTET_STREAM_VALUE)) .stream(inputStream, inputStream.available(), -1) .build()); } catch (Exception e) { throw new OssException("涓婁紶鏂囦欢澶辫触锛岃鏍稿Minio閰嶇疆淇℃伅"); } - return new UploadResult().setUrl(getBaseUrl() + path).setFilename(path); + return new UploadResult().setUrl(getEndpointLink() + "/" + path).setFilename(path); } @Override public void delete(String path) { - path = path.replace(getBaseUrl(), ""); + path = path.replace(getEndpointLink() + "/", ""); try { minioClient.removeObject(RemoveObjectArgs.builder() .bucket(properties.getBucketName()) @@ -91,13 +103,13 @@ } @Override - public UploadResult uploadSuffix(byte[] data, String suffix) { - return upload(data, getPath("", suffix)); + public UploadResult uploadSuffix(byte[] data, String suffix, String contentType) { + return upload(data, getPath("", suffix), contentType); } @Override - public UploadResult uploadSuffix(InputStream inputStream, String suffix) { - return upload(inputStream, getPath("", suffix)); + public UploadResult uploadSuffix(InputStream inputStream, String suffix, String contentType) { + return upload(inputStream, getPath("", suffix), contentType); } @Override @@ -105,8 +117,9 @@ OssFactory.register(getServiceType(), this); } - private String getBaseUrl() { - return properties.getEndpoint() + "/" + properties.getBucketName() + "/"; + @Override + public String getEndpointLink() { + return properties.getEndpoint() + "/" + properties.getBucketName(); } private String getPolicy(String bucketName, PolicyType policyType) { -- Gitblit v1.9.3