标签: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
在oss-starter工程中执行mvn clean install 一个自定义的starter,在需要的项目中导入。
<dependency>
<groupId>com.kuisama</groupId>
<artifactId>oss-spring-boot-starter</artifactId>
<version>1.0.5.RELEASE</version>
</dependency>
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<>();
}
}
}
标签:turn dep rgs xxxxxx sage pat exception 回调 sam
原文地址:https://www.cnblogs.com/taoyuanju/p/13126388.html