From cec13aac1e7b053b7f6bbad912b7f2be443203c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E6=B2=90?= Date: Wed, 24 Aug 2022 11:04:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=B8=83=E7=89=9B=E4=BA=91?= =?UTF-8?q?=E5=AD=98=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../storage/properties/QiniuStorageProperties.java | 15 ++++++ .../maku/storage/service/QiniuStorageService.java | 56 ++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 fast-boot-system/src/main/java/net/maku/storage/properties/QiniuStorageProperties.java create mode 100644 fast-boot-system/src/main/java/net/maku/storage/service/QiniuStorageService.java diff --git a/fast-boot-system/src/main/java/net/maku/storage/properties/QiniuStorageProperties.java b/fast-boot-system/src/main/java/net/maku/storage/properties/QiniuStorageProperties.java new file mode 100644 index 0000000..2a842b2 --- /dev/null +++ b/fast-boot-system/src/main/java/net/maku/storage/properties/QiniuStorageProperties.java @@ -0,0 +1,15 @@ +package net.maku.storage.properties; + +import lombok.Data; + +/** + * 七牛云存储配置项 + * + * @author 阿沐 babamu@126.com + */ +@Data +public class QiniuStorageProperties { + private String accessKey; + private String secretKey; + private String bucketName; +} diff --git a/fast-boot-system/src/main/java/net/maku/storage/service/QiniuStorageService.java b/fast-boot-system/src/main/java/net/maku/storage/service/QiniuStorageService.java new file mode 100644 index 0000000..a684c24 --- /dev/null +++ b/fast-boot-system/src/main/java/net/maku/storage/service/QiniuStorageService.java @@ -0,0 +1,56 @@ +package net.maku.storage.service; + +import com.qiniu.http.Response; +import com.qiniu.storage.Configuration; +import com.qiniu.storage.Region; +import com.qiniu.storage.UploadManager; +import com.qiniu.util.Auth; +import com.qiniu.util.IOUtils; +import net.maku.framework.common.exception.FastException; +import net.maku.storage.properties.StorageProperties; + +import java.io.IOException; +import java.io.InputStream; + +/** + * 七牛云存储 + * + * @author 阿沐 babamu@126.com + */ +public class QiniuStorageService extends StorageService { + private final UploadManager uploadManager; + private final String token; + + public QiniuStorageService(StorageProperties properties) { + this.properties = properties; + + uploadManager = new UploadManager(new Configuration(Region.autoRegion())); + token = Auth.create(properties.getQiniu().getAccessKey(), properties.getQiniu().getSecretKey()). + uploadToken(properties.getQiniu().getBucketName()); + } + + @Override + public String upload(byte[] data, String path) { + try { + Response res = uploadManager.put(data, path, token); + if (!res.isOK()) { + throw new FastException(res.toString()); + } + + return properties.getConfig().getDomain() + "/" + path; + } catch (Exception e) { + throw new FastException("上传文件失败:", e); + } + } + + @Override + public String upload(InputStream inputStream, String path) { + try { + byte[] data = IOUtils.toByteArray(inputStream); + return this.upload(data, path); + } catch (IOException e) { + throw new FastException("上传文件失败:", e); + } + } + +}