Bladeren bron

refactor:添加oss

荭琪枫 2 jaren geleden
bovenliggende
commit
3ce468878d

+ 1 - 1
.gitignore

@@ -26,4 +26,4 @@ target
 data/elasticsearch
 .init
 *.db
-.flattened-pom.xml
+.flattened-pom.xml

+ 62 - 0
iot-components/iot-component-oss/pom.xml

@@ -0,0 +1,62 @@
+<?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">
+    <parent>
+        <artifactId>iot-components</artifactId>
+        <groupId>cc.iotkit</groupId>
+        <version>${revision}</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <properties>
+        <hutool.version>5.8.15</hutool.version>
+    </properties>
+
+    <artifactId>iot-component-oss</artifactId>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.amazonaws</groupId>
+            <artifactId>aws-java-sdk-s3</artifactId>
+            <version>1.12.470</version>
+        </dependency>
+        <dependency>
+            <groupId>cn.hutool</groupId>
+            <artifactId>hutool-core</artifactId>
+            <version>${hutool.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>${java.version}</source>
+                    <target>${java.version}</target>
+                    <encoding>utf8</encoding>
+                </configuration>
+            </plugin>
+        </plugins>
+        <resources>
+            <resource>
+                <directory>src/main/java</directory>
+                <includes>
+                    <include>**/*.xml</include>
+                </includes>
+                <filtering>false</filtering>
+            </resource>
+        </resources>
+    </build>
+
+</project>

+ 56 - 0
iot-components/iot-component-oss/src/main/java/cc/iotkit/oss/config/OssAutoConfiguration.java

@@ -0,0 +1,56 @@
+package cc.iotkit.oss.config;
+
+import cc.iotkit.oss.properties.OssProperties;
+import cc.iotkit.oss.service.OssTemplate;
+import cc.iotkit.oss.service.impl.OssTemplateImpl;
+import com.amazonaws.ClientConfiguration;
+import com.amazonaws.auth.AWSCredentials;
+import com.amazonaws.auth.AWSCredentialsProvider;
+import com.amazonaws.auth.AWSStaticCredentialsProvider;
+import com.amazonaws.auth.BasicAWSCredentials;
+import com.amazonaws.client.builder.AwsClientBuilder;
+import com.amazonaws.services.s3.AmazonS3;
+import com.amazonaws.services.s3.AmazonS3Client;
+import lombok.RequiredArgsConstructor;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * @Author: 石恒
+ * @Date: 2023/5/22 21:15
+ * @Description:
+ */
+
+@Configuration
+@RequiredArgsConstructor
+@EnableConfigurationProperties(OssProperties.class)
+public class OssAutoConfiguration {
+
+    @Bean
+    @ConditionalOnMissingBean
+    public AmazonS3 ossClient(OssProperties ossProperties) {
+        // 客户端配置,主要是全局的配置信息
+        ClientConfiguration clientConfiguration = new ClientConfiguration();
+        clientConfiguration.setMaxConnections(ossProperties.getMaxConnections());
+        // url以及region配置
+        AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(
+                ossProperties.getEndpoint(), ossProperties.getRegion());
+        // 凭证配置
+        AWSCredentials awsCredentials = new BasicAWSCredentials(ossProperties.getAccessKey(),
+                ossProperties.getSecretKey());
+        AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);
+        // build amazonS3Client客户端
+        return AmazonS3Client.builder().withEndpointConfiguration(endpointConfiguration)
+                .withClientConfiguration(clientConfiguration).withCredentials(awsCredentialsProvider)
+                .disableChunkedEncoding().withPathStyleAccessEnabled(ossProperties.getPathStyleAccess()).build();
+    }
+
+    @Bean
+    @ConditionalOnBean(AmazonS3.class)
+    public OssTemplate ossTemplate(AmazonS3 amazonS3){
+        return new OssTemplateImpl(amazonS3);
+    }
+}

+ 45 - 0
iot-components/iot-component-oss/src/main/java/cc/iotkit/oss/properties/OssProperties.java

@@ -0,0 +1,45 @@
+package cc.iotkit.oss.properties;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+/**
+ * @Author: 石恒
+ * @Date: 2023/5/22 21:10
+ * @Description:
+ */
+@Data
+@ConfigurationProperties(prefix = "oss")
+public class OssProperties {
+    /**
+     * 对象存储服务的URL
+     */
+    private String endpoint;
+
+    /**
+     * 区域
+     */
+    private String region;
+
+    /**
+     * true path-style nginx 反向代理和S3默认支持 pathStyle模式 {http://endpoint/bucketname}
+     * false supports virtual-hosted-style 阿里云等需要配置为 virtual-hosted-style 模式{http://bucketname.endpoint}
+     * 只是url的显示不一样
+     */
+    private Boolean pathStyleAccess = true;
+
+    /**
+     * Access key
+     */
+    private String accessKey;
+
+    /**
+     * Secret key
+     */
+    private String secretKey;
+
+    /**
+     * 最大线程数,默认:100
+     */
+    private Integer maxConnections = 100;
+}

+ 87 - 0
iot-components/iot-component-oss/src/main/java/cc/iotkit/oss/service/OssTemplate.java

@@ -0,0 +1,87 @@
+package cc.iotkit.oss.service;
+
+import com.amazonaws.services.s3.model.Bucket;
+import com.amazonaws.services.s3.model.S3Object;
+import com.amazonaws.services.s3.model.S3ObjectSummary;
+
+import java.io.InputStream;
+import java.util.List;
+
+/**
+ * @Author: 石恒
+ * @Date: 2023/5/22 21:13
+ * @Description:
+ */
+public interface OssTemplate {
+
+    /**
+     * 创建bucket
+     * @param bucketName bucket名称
+     */
+    void createBucket(String bucketName);
+
+    /**
+     * 获取所有的bucket
+     * @return
+     */
+    List<Bucket> getAllBuckets();
+
+    /**
+     * 通过bucket名称删除bucket
+     * @param bucketName
+     */
+    void removeBucket(String bucketName);
+
+    /**
+     * 上传文件
+     * @param bucketName bucket名称
+     * @param objectName 文件名称
+     * @param stream 文件流
+     * @param contextType 文件类型
+     * @throws Exception
+     */
+    void putObject(String bucketName, String objectName, InputStream stream, String contextType) throws Exception;
+
+    /**
+     * 上传文件
+     * @param bucketName bucket名称
+     * @param objectName 文件名称
+     * @param stream 文件流
+     * @throws Exception
+     */
+    void putObject(String bucketName, String objectName, InputStream stream) throws Exception;
+
+    /**
+     * 获取文件
+     * @param bucketName bucket名称
+     * @param objectName 文件名称
+     * @return S3Object
+     */
+    S3Object getObject(String bucketName, String objectName);
+
+    /**
+     * 获取对象的url
+     * @param bucketName
+     * @param objectName
+     * @param expires
+     * @return
+     */
+    String getObjectURL(String bucketName, String objectName, Integer expires);
+
+    /**
+     * 通过bucketName和objectName删除对象
+     * @param bucketName
+     * @param objectName
+     * @throws Exception
+     */
+    void removeObject(String bucketName, String objectName) throws Exception;
+
+    /**
+     * 根据文件前置查询文件
+     * @param bucketName bucket名称
+     * @param prefix 前缀
+     * @param recursive 是否递归查询
+     * @return S3ObjectSummary 列表
+     */
+    List<S3ObjectSummary> getAllObjectsByPrefix(String bucketName, String prefix, boolean recursive);
+}

+ 172 - 0
iot-components/iot-component-oss/src/main/java/cc/iotkit/oss/service/impl/OssTemplateImpl.java

@@ -0,0 +1,172 @@
+package cc.iotkit.oss.service.impl;
+
+import cc.iotkit.oss.service.OssTemplate;
+import com.amazonaws.services.s3.AmazonS3;
+import com.amazonaws.services.s3.model.*;
+import com.amazonaws.util.IOUtils;
+import lombok.RequiredArgsConstructor;
+import lombok.SneakyThrows;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.List;
+
+/**
+ * @Author: 石恒
+ * @Date: 2023/5/22 21:14
+ * @Description:
+ */
+
+@RequiredArgsConstructor
+public class OssTemplateImpl implements OssTemplate {
+
+    private final AmazonS3 amazonS3;
+
+    /**
+     * 创建Bucket
+     * AmazonS3:https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateBucket.html
+     * @param bucketName bucket名称
+     */
+    @Override
+    @SneakyThrows
+    public void createBucket(String bucketName) {
+        if ( !amazonS3.doesBucketExistV2(bucketName) ) {
+            amazonS3.createBucket((bucketName));
+        }
+    }
+
+    /**
+     * 获取所有的buckets
+     * AmazonS3:https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListBuckets.html
+     * @return
+     */
+    @Override
+    @SneakyThrows
+    public List<Bucket> getAllBuckets() {
+        return amazonS3.listBuckets();
+    }
+
+    /**
+     * 通过Bucket名称删除Bucket
+     * AmazonS3:https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteBucket.html
+     * @param bucketName
+     */
+    @Override
+    @SneakyThrows
+    public void removeBucket(String bucketName) {
+        amazonS3.deleteBucket(bucketName);
+    }
+
+    /**
+     * 上传对象
+     * @param bucketName bucket名称
+     * @param objectName 文件名称
+     * @param stream 文件流
+     * @param contextType 文件类型
+     * AmazonS3:https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html
+     */
+    @Override
+    @SneakyThrows
+    public void putObject(String bucketName, String objectName, InputStream stream, String contextType) {
+        putObject(bucketName, objectName, stream, stream.available(), contextType);
+    }
+    /**
+     * 上传对象
+     * @param bucketName bucket名称
+     * @param objectName 文件名称
+     * @param stream 文件流
+     * AmazonS3:https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html
+     */
+    @Override
+    @SneakyThrows
+    public void putObject(String bucketName, String objectName, InputStream stream) {
+        putObject(bucketName, objectName, stream, stream.available(), "application/octet-stream");
+    }
+
+    /**
+     * 通过bucketName和objectName获取对象
+     * @param bucketName bucket名称
+     * @param objectName 文件名称
+     * @return
+     * AmazonS3:https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObject.html
+     */
+    @Override
+    @SneakyThrows
+    public S3Object getObject(String bucketName, String objectName) {
+        return amazonS3.getObject(bucketName, objectName);
+    }
+
+    /**
+     * 获取对象的url
+     * @param bucketName
+     * @param objectName
+     * @param expires
+     * @return
+     * AmazonS3:https://docs.aws.amazon.com/AmazonS3/latest/API/API_GeneratePresignedUrl.html
+     */
+    @Override
+    @SneakyThrows
+    public String getObjectURL(String bucketName, String objectName, Integer expires) {
+        Date date = new Date();
+        Calendar calendar = new GregorianCalendar();
+        calendar.setTime(date);
+        calendar.add(Calendar.DAY_OF_MONTH, expires);
+        URL url = amazonS3.generatePresignedUrl(bucketName, objectName, calendar.getTime());
+        return url.toString();
+    }
+
+    /**
+     * 通过bucketName和objectName删除对象
+     * @param bucketName
+     * @param objectName
+     * AmazonS3:https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteObject.html
+     */
+    @Override
+    @SneakyThrows
+    public void removeObject(String bucketName, String objectName) {
+        amazonS3.deleteObject(bucketName, objectName);
+    }
+
+    /**
+     * 根据bucketName和prefix获取对象集合
+     * @param bucketName bucket名称
+     * @param prefix 前缀
+     * @param recursive 是否递归查询
+     * @return
+     * AmazonS3:https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjects.html
+     */
+    @Override
+    @SneakyThrows
+    public List<S3ObjectSummary> getAllObjectsByPrefix(String bucketName, String prefix, boolean recursive) {
+        ObjectListing objectListing = amazonS3.listObjects(bucketName, prefix);
+        return objectListing.getObjectSummaries();
+    }
+
+
+    /**
+     *
+     * @param bucketName
+     * @param objectName
+     * @param stream
+     * @param size
+     * @param contextType
+     * @return
+     */
+    @SneakyThrows
+    private PutObjectResult putObject(String bucketName, String objectName, InputStream stream, long size,
+                                      String contextType)  {
+
+        byte[] bytes = IOUtils.toByteArray(stream);
+        ObjectMetadata objectMetadata = new ObjectMetadata();
+        objectMetadata.setContentLength(size);
+        objectMetadata.setContentType(contextType);
+        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
+        // 上传
+        return amazonS3.putObject(bucketName, objectName, byteArrayInputStream, objectMetadata);
+
+    }
+}

+ 2 - 0
iot-components/iot-component-oss/src/main/resources/META-INF/spring.factories

@@ -0,0 +1,2 @@
+org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
+  cc.iotkit.oss.config.OssAutoConfiguration

+ 1 - 0
iot-components/pom.xml

@@ -21,6 +21,7 @@
         <module>iot-component-tcp</module>
         <module>iot-DLT645-component</module>
         <module>iot-websocket-component</module>
+        <module>iot-component-oss</module>
    <!--     <module>iot-ctwing-component</module>-->
     </modules>
 

+ 61 - 0
iot-module/iot-manager/src/main/java/cc/iotkit/manager/controller/OtaController.java

@@ -0,0 +1,61 @@
+package cc.iotkit.manager.controller;
+
+import cc.iotkit.manager.service.OtaService;
+import io.swagger.annotations.Api;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.annotation.Resource;
+
+/**
+ * @Author: 石恒
+ * @Date: 2023/5/19 20:42
+ * @Description:
+ */
+@Api(tags = {"ota升级管理"})
+@Slf4j
+@RestController
+@RequestMapping("/ota")
+public class OtaController {
+
+    @Resource
+    private OtaService otaService;
+
+    /*@ApiOperation("升级包上传")
+    @PostMapping("/package/upload")
+    public String packageUpload(MultipartFile file) throws Exception {
+        if (!file.isEmpty()) {
+            String fileName = file.getOriginalFilename();
+            String suffix = StringUtils.isEmpty(fileName) ? "" : fileName.substring(fileName.lastIndexOf("."));
+            InputStream ins = file.getInputStream();
+            return otaService.uploadFile(ins, suffix);
+        }
+        return "";
+    }
+
+    @ApiOperation("新增升级包")
+    @PostMapping("/package/add")
+    public OtaPackage addChannelTemplate(@RequestBody @Valid Request<OtaPackage> request) throws Exception {
+        return otaService.addOtaPackage(request.getData());
+    }
+
+    @ApiOperation("删除升级包")
+    @PostMapping("/package/delById")
+    public Boolean delChannelConfigById(@RequestBody @Valid Request<String> request) {
+        return otaService.delOtaPackageById(request.getData());
+    }
+
+    @ApiOperation("升级包列表")
+    @PostMapping("/package/getList")
+    public Paging<OtaPackage> packageList(@RequestBody @Valid PageRequest<Void> request) {
+        return otaService.getOtaPackagePageList(request.getPageNo(), request.getPageSize());
+    }
+
+    @ApiOperation("设备获取升级包")
+    @PostMapping("/device/upgrade")
+    public void deviceUpgrade(@RequestBody Request<DeviceOta> deviceOtaRequest) {
+        DeviceOta deviceOta = deviceOtaRequest.getData();
+        otaService.findByVersionGreaterThan(deviceOta.getCurrentVersion(), deviceOta.getDeviceId());
+    }*/
+}

+ 18 - 0
iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/OtaService.java

@@ -0,0 +1,18 @@
+package cc.iotkit.manager.service;
+
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Service;
+
+/**
+ * @Author: 石恒
+ * @Date: 2023/5/19 20:49
+ * @Description:
+ */
+@Slf4j
+@Service
+@RequiredArgsConstructor
+public class OtaService {
+
+
+}