自定义 oss starter
Posted taoyuanju
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义 oss starter相关的知识,希望对你有一定的参考价值。
软件架构
基于spring-boot2.1.9.RELEASE、阿里云aliyun-sdk-oss3.7.0
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.7.0</version>
</dependency>
定义一个实体类映射配置信息
@ConfigurationProperties(prefix = "demo") 它可以把相同前缀的配置信息通过配置项名称映射成实体类
@ConfigurationProperties(prefix = "aliyun.oss")
@Data
public class OssProperties {
private String endpoint;
private String endpointManager;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;
private ClientConfiguration clientConfig;
/**
* 存放目录
*/
private String dir = "mcs/";
private String host;
/**
* 过期时间
*/
private String expire;
/**
* 回调地址
*/
private String callback;
}
定义一个配置类
@Configuration
@EnableConfigurationProperties(OssProperties.class)
public class McsOssAutoConfiguration{
@Autowired
OssProperties ossProperties;
@Bean(destroyMethod = "shutdown")
public OSSClient ossClient() {
String endpoint = ossProperties.getEndpoint();
String accessKeyId = ossProperties.getAccessKeyId();
String accessKeySecret = ossProperties.getAccessKeySecret();
return (OSSClient) new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
}
}
自动配置
在resources下新建META-INF文件夹,然后创建spring.factories文件,
在该文件中加入如下配置,该配置指定上步骤中定义的配置类为自动装配的配置。
#-------starter自动装配---------
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.demo.starter.config.DemoConfig
导入pom依赖
在oss-starter工程中执行mvn clean install 一个自定义的starter,在需要的项目中导入。
<dependency>
<groupId>com.kuisama</groupId>
<artifactId>oss-spring-boot-starter</artifactId>
<version>1.0.5.RELEASE</version>
</dependency>
配置application.yml
aliyun:
oss:
accessKeyId: 你的阿里云accessKeyId
accessKeySecret: 你的阿里云accessKeySecret
endpoint: oss-cn-beijing.aliyuncs.com
bucketName: xxxxxx
使用说明
@SpringBootApplication
@EnableAliyunOss
@EnableSwagger2Doc
@RestController
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Autowired
private OssProperties aliyunOssProperties;
@Autowired
private OSSClient ossClient;
@ApiOperation("上传")
@PostMapping("/upload")
public String upload(MultipartFile multipartFile) {
try {
String url = AliyunOssHandler.upload(ossClient, aliyunOssProperties, multipartFile.getOriginalFilename(), multipartFile.getInputStream());
return url;
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
}
@ApiOperation("文件删除")
@PostMapping("/del")
public void del(@RequestParam String fileName) {
try {
AliyunOssHandler.deleteFile(ossClient, aliyunOssProperties, fileName);
} catch (Exception e) {
e.printStackTrace();
}
}
@ApiOperation("批量文件删除")
@PostMapping("/bathDel")
public List<String> bathDel(@RequestParam List<String> fileName) {
try {
List<String> keys = AliyunOssHandler.batchDeleteFile(ossClient, aliyunOssProperties, fileName);
return keys;
} catch (Exception e) {
e.printStackTrace();
return new ArrayList<>();
}
}
}
以上是关于自定义 oss starter的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot Starter自定义注解 - 接口加解密
自定义SpringBoot Starter 通过注解启动装配