refactor:添加oss

V0.5.x
荭琪枫 2023-06-06 17:35:25 +08:00
parent 6c1bd70c8e
commit 3ce468878d
10 changed files with 505 additions and 1 deletions

2
.gitignore vendored
View File

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

View File

@ -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>

View File

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

View File

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

View File

@ -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();
/**
* bucketbucket
* @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);
/**
* bucketNameobjectName
* @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);
}

View File

@ -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
* AmazonS3https://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
* AmazonS3https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListBuckets.html
* @return
*/
@Override
@SneakyThrows
public List<Bucket> getAllBuckets() {
return amazonS3.listBuckets();
}
/**
* BucketBucket
* AmazonS3https://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
* AmazonS3https://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
* AmazonS3https://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");
}
/**
* bucketNameobjectName
* @param bucketName bucket
* @param objectName
* @return
* AmazonS3https://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
* AmazonS3https://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();
}
/**
* bucketNameobjectName
* @param bucketName
* @param objectName
* AmazonS3https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteObject.html
*/
@Override
@SneakyThrows
public void removeObject(String bucketName, String objectName) {
amazonS3.deleteObject(bucketName, objectName);
}
/**
* bucketNameprefix
* @param bucketName bucket
* @param prefix
* @param recursive
* @return
* AmazonS3https://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);
}
}

View File

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

View File

@ -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>

View File

@ -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());
}*/
}

View File

@ -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 {
}