From 86d5a1d19074b75eff9999fd99ae085487af43f7 Mon Sep 17 00:00:00 2001 From: 疯狂的狮子li <15040126243@163.com> Date: 星期四, 17 二月 2022 10:31:52 +0800 Subject: [PATCH] update 更新 所有 oss 均支持 https 配置 --- ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/QiniuOssStrategy.java | 178 +++++++++++++++++++++++++++++----------------------------- 1 files changed, 89 insertions(+), 89 deletions(-) diff --git a/ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/QiniuOssStrategy.java b/ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/QiniuOssStrategy.java index bf90aa8..20f13ec 100644 --- a/ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/QiniuOssStrategy.java +++ b/ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/QiniuOssStrategy.java @@ -7,11 +7,13 @@ import com.qiniu.storage.Region; import com.qiniu.storage.UploadManager; import com.qiniu.util.Auth; +import com.ruoyi.oss.constant.OssConstant; import com.ruoyi.oss.entity.UploadResult; import com.ruoyi.oss.enumd.OssEnumd; import com.ruoyi.oss.exception.OssException; import com.ruoyi.oss.properties.OssProperties; import com.ruoyi.oss.service.abstractd.AbstractOssStrategy; +import org.springframework.stereotype.Component; import java.io.InputStream; @@ -20,108 +22,106 @@ * * @author Lion Li */ +@Component public class QiniuOssStrategy extends AbstractOssStrategy { - private UploadManager uploadManager; - private BucketManager bucketManager; - private Auth auth; + private UploadManager uploadManager; + private BucketManager bucketManager; + private Auth auth; - @Override - public void init(OssProperties cloudStorageProperties) { - properties = cloudStorageProperties; - try { - Configuration config = new Configuration(getRegion(properties.getRegion())); - // https璁剧疆 - config.useHttpsDomains = false; - config.useHttpsDomains = "Y".equals(properties.getIsHttps()); - uploadManager = new UploadManager(config); - auth = Auth.create(properties.getAccessKey(), properties.getSecretKey()); - String bucketName = properties.getBucketName(); - bucketManager = new BucketManager(auth, config); - if (!ArrayUtil.contains(bucketManager.buckets(), bucketName)) { - bucketManager.createBucket(bucketName, properties.getRegion()); - } - } catch (Exception e) { - throw new OssException("涓冪墰浜戝瓨鍌ㄩ厤缃敊璇�! 璇锋鏌ョ郴缁熼厤缃�:[" + e.getMessage() + "]"); - } - } + @Override + public void init(OssProperties ossProperties) { + super.init(ossProperties); + try { + Configuration config = new Configuration(getRegion(properties.getRegion())); + // https璁剧疆 + config.useHttpsDomains = OssConstant.IS_HTTPS.equals(properties.getIsHttps()); + uploadManager = new UploadManager(config); + auth = Auth.create(properties.getAccessKey(), properties.getSecretKey()); + bucketManager = new BucketManager(auth, config); + createBucket(); + } catch (Exception e) { + throw new OssException("涓冪墰浜戝瓨鍌ㄩ厤缃敊璇�! 璇锋鏌ョ郴缁熼厤缃�:[" + e.getMessage() + "]"); + } + isInit = true; + } - @Override - public void createBucket() { - try { - String bucketName = properties.getBucketName(); - if (ArrayUtil.contains(bucketManager.buckets(), bucketName)) { - return; - } - bucketManager.createBucket(bucketName, properties.getRegion()); - } catch (Exception e) { - throw new OssException("鍒涘缓Bucket澶辫触, 璇锋牳瀵逛竷鐗涗簯閰嶇疆淇℃伅:[" + e.getMessage() + "]"); - } - } + @Override + public void createBucket() { + try { + String bucketName = properties.getBucketName(); + if (ArrayUtil.contains(bucketManager.buckets(), bucketName)) { + return; + } + bucketManager.createBucket(bucketName, properties.getRegion()); + } catch (Exception e) { + throw new OssException("鍒涘缓Bucket澶辫触, 璇锋牳瀵逛竷鐗涗簯閰嶇疆淇℃伅:[" + e.getMessage() + "]"); + } + } - @Override - public String getServiceType() { - return OssEnumd.QINIU.getValue(); - } + @Override + public OssEnumd getServiceType() { + return OssEnumd.QINIU; + } - @Override - public UploadResult upload(byte[] data, String path, String contentType) { - try { + @Override + public UploadResult upload(byte[] data, String path, String contentType) { + try { String token = auth.uploadToken(properties.getBucketName()); Response res = uploadManager.put(data, path, token, null, contentType, false); - if (!res.isOK()) { - throw new RuntimeException("涓婁紶涓冪墰鍑洪敊锛�" + res.error); - } - } catch (Exception e) { - throw new OssException("涓婁紶鏂囦欢澶辫触锛岃鏍稿涓冪墰閰嶇疆淇℃伅:[" + e.getMessage() + "]"); - } - return new UploadResult().setUrl(getEndpointLink() + "/" + path).setFilename(path); - } + if (!res.isOK()) { + throw new RuntimeException("涓婁紶涓冪墰鍑洪敊锛�" + res.error); + } + } catch (Exception e) { + throw new OssException("涓婁紶鏂囦欢澶辫触锛岃鏍稿涓冪墰閰嶇疆淇℃伅:[" + e.getMessage() + "]"); + } + return UploadResult.builder().url(getEndpointLink() + "/" + path).filename(path).build(); + } - @Override - public void delete(String path) { - try { - path = path.replace(getEndpointLink() + "/", ""); - Response res = bucketManager.delete(properties.getBucketName(), path); - if (!res.isOK()) { - throw new RuntimeException("鍒犻櫎涓冪墰鏂囦欢鍑洪敊锛�" + res.error); - } - } catch (Exception e) { - throw new OssException(e.getMessage()); - } - } + @Override + public void delete(String path) { + try { + path = path.replace(getEndpointLink() + "/", ""); + Response res = bucketManager.delete(properties.getBucketName(), path); + if (!res.isOK()) { + throw new RuntimeException("鍒犻櫎涓冪墰鏂囦欢鍑洪敊锛�" + res.error); + } + } catch (Exception e) { + throw new OssException(e.getMessage()); + } + } - @Override - public UploadResult uploadSuffix(byte[] data, String suffix, String contentType) { - return upload(data, getPath(properties.getPrefix(), suffix), contentType); - } + @Override + public UploadResult uploadSuffix(byte[] data, String suffix, String contentType) { + return upload(data, getPath(properties.getPrefix(), suffix), contentType); + } - @Override - public UploadResult uploadSuffix(InputStream inputStream, String suffix, String contentType) { - return upload(inputStream, getPath(properties.getPrefix(), suffix), contentType); - } + @Override + public UploadResult uploadSuffix(InputStream inputStream, String suffix, String contentType) { + return upload(inputStream, getPath(properties.getPrefix(), suffix), contentType); + } - @Override - public String getEndpointLink() { - return properties.getEndpoint(); - } + @Override + public String getEndpointLink() { + return properties.getEndpoint(); + } - private Region getRegion(String region) { - switch (region) { - case "z0": - return Region.region0(); - case "z1": - return Region.region1(); - case "z2": - return Region.region2(); - case "na0": - return Region.regionNa0(); - case "as0": - return Region.regionAs0(); - default: - return Region.autoRegion(); - } - } + private Region getRegion(String region) { + switch (region) { + case "z0": + return Region.region0(); + case "z1": + return Region.region1(); + case "z2": + return Region.region2(); + case "na0": + return Region.regionNa0(); + case "as0": + return Region.regionAs0(); + default: + return Region.autoRegion(); + } + } } -- Gitblit v1.9.3