|
@@ -0,0 +1,54 @@
|
|
|
|
|
+package com.middle.platform.oss.config;
|
|
|
|
|
+
|
|
|
|
|
+import com.aliyun.oss.*;
|
|
|
|
|
+import com.aliyun.oss.common.auth.CredentialsProviderFactory;
|
|
|
|
|
+import com.aliyun.oss.common.auth.DefaultCredentialProvider;
|
|
|
|
|
+import com.aliyun.oss.common.comm.SignVersion;
|
|
|
|
|
+import com.aliyun.oss.model.PutObjectRequest;
|
|
|
|
|
+import com.aliyun.oss.model.PutObjectResult;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @author xucaiqin
|
|
|
|
|
+ * @date 2024-05-06 08:56:05
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+public class AliyunOss {
|
|
|
|
|
+ private final OSS oss;
|
|
|
|
|
+ private final String bucketName = "kaiwu-saas";
|
|
|
|
|
+
|
|
|
|
|
+ public AliyunOss() {
|
|
|
|
|
+ String secretAccessKey = "7mQLWMaBJeZPRV1SRGogctYGXwppjQ";
|
|
|
|
|
+ String accessKeyId = "LTAI5tPEbubCGq5Rdwygbz4Q";
|
|
|
|
|
+ DefaultCredentialProvider defaultCredentialProvider = CredentialsProviderFactory.newDefaultCredentialProvider(accessKeyId, secretAccessKey);
|
|
|
|
|
+ ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
|
|
|
|
|
+ clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
|
|
|
|
|
+ String endPoint = "https://oss-cn-chengdu.aliyuncs.com";
|
|
|
|
|
+ String region = "cn-chengdu";
|
|
|
|
|
+ oss = OSSClientBuilder.create().endpoint(endPoint).credentialsProvider(defaultCredentialProvider).clientConfiguration(clientBuilderConfiguration).region(region).build();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * https://kaiwu-saas.oss-cn-chengdu.aliyuncs.com/test/a.txt
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param key
|
|
|
|
|
+ * @param file
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public String upload(String key, MultipartFile file) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file.getInputStream());
|
|
|
|
|
+ PutObjectResult putObjectResult = oss.putObject(putObjectRequest);
|
|
|
|
|
+ } catch (OSSException oe) {
|
|
|
|
|
+ oe.printStackTrace();
|
|
|
|
|
+ log.error("OSSException:{}", oe.getMessage(), oe);
|
|
|
|
|
+ } catch (ClientException | IOException ce) {
|
|
|
|
|
+ log.error("ClientException:{}", ce.getMessage(), ce);
|
|
|
|
|
+ }
|
|
|
|
|
+ return "https://" + bucketName + ".oss-cn-chengdu.aliyuncs.com/" + key;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|