aliyunOss

Spring Boot 结合 Aliyun Oss 实现图片等的上传

配置

获取所需的依赖包

1
2
3
4
5
6
# pom.yml
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.15.1</version>
</dependency>

在 applicaion 中配置一些 Oss 必要的环境变量

1
2
3
4
5
6
7
# application.ym/
aliyun:
oss:
endpoint: oss-cn-nanjing.aliyuncs.com
accessKeyId: LTAI5t99f26ZT
accessKeySecret: ZlUQ
bucketName: wcx0206

Oss 工具类的实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Component
@Getter
@Setter
@NoArgsConstructor
@ConfigurationProperties("aliyun.oss") // 从 application 中读取配置
public class OssUtil {
private String endpoint;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;

public String upload(String objectName, InputStream inputStream) {
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
try {
ossClient.putObject(putObjectRequest);
}finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
return ossClient.generatePresignedUrl(bucketName, objectName, new Date()).toString().split("\\?Expires")[0];
}
}

工具类的使用

Service

1
2
3
4
5
6
7
8
@Override
public String uploadImage(MultipartFile file) {
try {
return ossUtil.upload(file.getOriginalFilename(), file.getInputStream());
} catch (IOException e) {
throw new RuntimeException(e);
}
}

Controller

1
2
3
4
5
6
7
@RequestMapping("/upload")
@Operation(summary = "上传图片", description = "上传图片的接口")
public ResultVO<String> uploadImage(
@Parameter(name = "file", description = "图片文件", required = true) @RequestParam MultipartFile file
) {
return ResultVO.buildSuccess(imageService.uploadImage(file));
}