Parcourir la source

引入oss模块,并调试基础文件上传

xucaiqin il y a 1 an
Parent
commit
184b73fd6b

+ 12 - 1
iot-dependencies/pom.xml

@@ -76,6 +76,7 @@
         <weixin-java.version>4.5.5.B</weixin-java.version>
         <coap.version>3.11.0</coap.version>
         <netty.version>4.1.107.Final</netty.version>
+        <aliyun-oss.version>3.17.4</aliyun-oss.version>
     </properties>
 
     <dependencyManagement>
@@ -273,7 +274,11 @@
                 <artifactId>guava</artifactId>
                 <version>${guava.version}</version>
             </dependency>
-
+            <dependency>
+                <groupId>com.aliyun.oss</groupId>
+                <artifactId>aliyun-sdk-oss</artifactId>
+                <version>${aliyun-oss.version}</version>
+            </dependency>
             <dependency>
                 <groupId>com.alibaba</groupId>
                 <artifactId>transmittable-thread-local</artifactId> <!-- 解决 ThreadLocal 父子线程的传值问题 -->
@@ -311,6 +316,12 @@
                 <artifactId>iot-module-auth-api</artifactId>
                 <version>${revision}</version>
             </dependency>
+            <dependency>
+                <groupId>com.middle.platform</groupId>
+                <artifactId>iot-starter-oss</artifactId>
+                <version>${revision}</version>
+            </dependency>
+
         </dependencies>
     </dependencyManagement>
 

+ 39 - 0
iot-framework/iot-starter-oss/pom.xml

@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>com.middle.platform</groupId>
+        <artifactId>iot-framework</artifactId>
+        <version>${revision}</version>
+    </parent>
+
+    <artifactId>iot-starter-oss</artifactId>
+    <description>oss-starter</description>
+    <packaging>jar</packaging>
+
+    <properties>
+        <maven.compiler.source>17</maven.compiler.source>
+        <maven.compiler.target>17</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>com.middle.platform</groupId>
+            <artifactId>iot-common-core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.aliyun.oss</groupId>
+            <artifactId>aliyun-sdk-oss</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-web</artifactId>
+            <scope>provided</scope>
+        </dependency>
+
+    </dependencies>
+
+</project>

+ 54 - 0
iot-framework/iot-starter-oss/src/main/java/com/middle/platform/oss/config/AliyunOss.java

@@ -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;
+    }
+
+}

+ 1 - 0
iot-framework/iot-starter-oss/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

@@ -0,0 +1 @@
+com.middle.platform.oss.config.AliyunOss

+ 1 - 0
iot-framework/pom.xml

@@ -30,6 +30,7 @@
         <module>iot-starter-excel</module>
         <module>iot-starter-kafka</module>
         <module>iot-starter-coap</module>
+        <module>iot-starter-oss</module>
     </modules>
 
     <properties>