|
|
@@ -0,0 +1,83 @@
|
|
|
+package com.izouma.zhirongip.service.storage;
|
|
|
+
|
|
|
+import com.aliyun.oss.OSSClient;
|
|
|
+import com.aliyun.oss.model.ObjectMetadata;
|
|
|
+import com.izouma.zhirongip.exception.BusinessException;
|
|
|
+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 software.amazon.awssdk.auth.credentials.AwsCredentials;
|
|
|
+import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
|
|
|
+import software.amazon.awssdk.core.sync.RequestBody;
|
|
|
+import software.amazon.awssdk.regions.Region;
|
|
|
+import software.amazon.awssdk.services.s3.S3Client;
|
|
|
+import software.amazon.awssdk.services.s3.model.PutObjectRequest;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.net.URI;
|
|
|
+import java.net.URL;
|
|
|
+
|
|
|
+@Data
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+@EnableConfigurationProperties
|
|
|
+@ConditionalOnProperty(name = "storage.provider", havingValue = "eos")
|
|
|
+public class EosStorageService implements StorageService {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String uploadFromInputStream(InputStream inputStream, String path) {
|
|
|
+ log.info("EOS上传: inputStream -> {}", path);
|
|
|
+ try {
|
|
|
+ String result = upload(inputStream, path);
|
|
|
+ log.info("上传成功 {}", result);
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("EOS上传失败", e);
|
|
|
+ throw new BusinessException("上传失败", e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String uploadFromUrl(String url, String path) {
|
|
|
+ log.info("EOS上传: {} -> {}", url, path);
|
|
|
+ try {
|
|
|
+ InputStream inputStream = new URL(url).openStream();
|
|
|
+ String result = upload(inputStream, path);
|
|
|
+ log.info("上传成功 {}", result);
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("EOS上传失败", e);
|
|
|
+ throw new BusinessException("上传失败", e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String upload(InputStream inputStream, String path) throws IOException {
|
|
|
+ S3Client client = S3Client.builder()
|
|
|
+ .credentialsProvider(() -> new AwsCredentials() {
|
|
|
+ @Override
|
|
|
+ public String accessKeyId() {
|
|
|
+ return "WI5C4BHR0HWYFIGP2I7Z";
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String secretAccessKey() {
|
|
|
+ return "e0a82e65ae784d83af6997a774f88375";
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .endpointOverride(URI.create("https://eos-fujian-1.cmecloud.cn"))
|
|
|
+ .region(Region.of("fujian1"))
|
|
|
+ .build();
|
|
|
+ PutObjectRequest objectRequest = PutObjectRequest.builder()
|
|
|
+ .bucket("yzip")
|
|
|
+ .key(path)
|
|
|
+ .build();
|
|
|
+ client.putObject(objectRequest, RequestBody.fromInputStream(inputStream, inputStream.available()));
|
|
|
+
|
|
|
+ return "https://yzip.eos-fujian-1.cmecloud.cn" + "/" + path;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|