码迷,mamicode.com
首页 > 其他好文 > 详细

自定义 oss starter

时间:2020-06-14 21:01:33      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:turn   dep   rgs   xxxxxx   sage   pat   exception   回调   sam   

软件架构

基于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

标签:turn   dep   rgs   xxxxxx   sage   pat   exception   回调   sam   

原文地址:https://www.cnblogs.com/taoyuanju/p/13126388.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!