|
|
@@ -0,0 +1,127 @@
|
|
|
+package com.izouma.tcg.service.storage;
|
|
|
+
|
|
|
+import com.izouma.tcg.exception.BusinessException;
|
|
|
+import com.ksyun.ks3.dto.AccessControlList;
|
|
|
+import com.ksyun.ks3.dto.CannedAccessControlList;
|
|
|
+import com.ksyun.ks3.dto.Grant;
|
|
|
+import com.ksyun.ks3.dto.ObjectMetadata;
|
|
|
+import com.ksyun.ks3.http.HttpClientConfig;
|
|
|
+import com.ksyun.ks3.service.Ks3;
|
|
|
+import com.ksyun.ks3.service.Ks3Client;
|
|
|
+import com.ksyun.ks3.service.Ks3ClientConfig;
|
|
|
+import com.ksyun.ks3.service.request.PutObjectRequest;
|
|
|
+import lombok.Data;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
+import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
|
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.net.URL;
|
|
|
+
|
|
|
+@Data
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+@EnableConfigurationProperties
|
|
|
+@ConfigurationProperties(prefix = "ksc")
|
|
|
+@ConditionalOnProperty(name = "storage.provider", havingValue = "ksc")
|
|
|
+public class KS3StorageService implements StorageService {
|
|
|
+ private String accessKeyId;
|
|
|
+ private String accessKeySecret;
|
|
|
+ private String ossBucketName;
|
|
|
+ private String ossEndPoint;
|
|
|
+ private String ossDomain;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String uploadFromInputStream(InputStream inputStream, String path) {
|
|
|
+ log.info("金山云OSS上传: inputStream -> {}", path);
|
|
|
+ try {
|
|
|
+ String result = upload(inputStream, path, getConfig());
|
|
|
+ log.info("上传成功 {}", result);
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("金山云OSS上传失败", e);
|
|
|
+ throw new BusinessException("上传失败", e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String uploadFromUrl(String url, String path) {
|
|
|
+ log.info("金山云OSS上传: {} -> {}", url, path);
|
|
|
+ try {
|
|
|
+ InputStream inputStream = new URL(url).openStream();
|
|
|
+ String result = upload(inputStream, path, getConfig());
|
|
|
+ log.info("上传成功 {}", result);
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("金山云OSS上传失败", e);
|
|
|
+ throw new BusinessException("上传失败", e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String upload(InputStream inputStream, String path, Ks3ClientConfig config) {
|
|
|
+ Ks3 client = new Ks3Client(accessKeyId, accessKeySecret, config);
|
|
|
+ ObjectMetadata meta = new ObjectMetadata();
|
|
|
+ PutObjectRequest request = new PutObjectRequest(ossBucketName, path, inputStream, meta);
|
|
|
+ request.setCannedAcl(CannedAccessControlList.PublicRead);
|
|
|
+ client.putObject(request);
|
|
|
+ try {
|
|
|
+ inputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return ossDomain + "/" + path;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Ks3ClientConfig getConfig() {
|
|
|
+ Ks3ClientConfig config = new Ks3ClientConfig();
|
|
|
+
|
|
|
+/**
|
|
|
+ * 设置服务地址
|
|
|
+ * 中国(北京)| ks3-cn-beijing.ksyun.com
|
|
|
+ * 中国(上海)| ks3-cn-shanghai.ksyun.com
|
|
|
+ * 中国(香港)| ks3-cn-hk-1.ksyun.com
|
|
|
+ * 中国(广州)| ks3-cn-guangzhou.ksyun.com
|
|
|
+ * 中国(青岛)| ks3-cn-qingdao.ksyun.com
|
|
|
+ * 金融专区(北京)| ks3-jr-beijing.ksyun.com
|
|
|
+ * 金融专区(上海)| ks3-jr-shanghai.ksyun.com
|
|
|
+ * 俄罗斯 | ks3-rus.ksyun.com
|
|
|
+ * 新加坡 | ks3-sgp.ksyun.com
|
|
|
+ * 如果使用自定义域名,设置endpoint为自定义域名,同时设置domainMode为true
|
|
|
+
|
|
|
+ */
|
|
|
+
|
|
|
+ config.setEndpoint(ossEndPoint); //此处以北京region为例
|
|
|
+
|
|
|
+/**
|
|
|
+ *true:表示以自定义域名访问
|
|
|
+ *false:表示以KS3的外网域名或内网域名访问,默认为false
|
|
|
+
|
|
|
+ */
|
|
|
+
|
|
|
+ config.setDomainMode(false);
|
|
|
+
|
|
|
+ config.setProtocol(Ks3ClientConfig.PROTOCOL.http);
|
|
|
+
|
|
|
+/**
|
|
|
+ *true表示以 endpoint/{bucket}/{key}的方式访问
|
|
|
+ *false表示以 {bucket}.endpoint/{key}的方式访问
|
|
|
+ *如果domainMode设置为true,pathStyleAccess可忽略设置
|
|
|
+
|
|
|
+ */
|
|
|
+
|
|
|
+ config.setPathStyleAccess(false);
|
|
|
+
|
|
|
+
|
|
|
+ HttpClientConfig hconfig = new HttpClientConfig();
|
|
|
+
|
|
|
+//在HttpClientConfig中可以设置httpclient的相关属性,比如代理,超时,重试等。
|
|
|
+
|
|
|
+ config.setHttpClientConfig(hconfig);
|
|
|
+ return config;
|
|
|
+ }
|
|
|
+}
|