1.创建一个service_ossspringboot项目

2.导入相关依赖
...其他的依赖大家自行导入
<!-- 阿里云oss依赖 -->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
<!-- 日期工具栏依赖 -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.1</version>
</dependency>
3.写yml配置文件
server:
port: 8205
spring:
application:
name: service-oss
profiles:
active: dev
servlet:
multipart:
max-file-size: 1024MB
max-request-size: 1024MB
aliyun:
oss:
bucketname: startqbb-oss
endpoint: oss-cn-hangzhou.aliyuncs.com
keyid: your accessKeyId
keysecret: your accessKeySecret
4.主启动类
package com.qbb.yygh.oss;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
/**
* @author QiuQiu&LL (个人博客:https://www.cnblogs.com/qbbit)
* @version 1.0
* @date 2022-04-25 19:47
* @Description:
*/
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class ServiceOssApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceOssApplication.class, args);
}
}
5.创建一个properties配置类,绑定oss配置信息
package com.qbb.yygh.oss.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author QiuQiu&LL (个人博客:https://www.cnblogs.com/qbbit)
* @version 1.0
* @date 2022-04-25 20:02
* @Description:
*/
@Data
@Component
@ConfigurationProperties(prefix = "aliyun.oss")
public class OssProperties {
private String endpoint;
private String keyid;
private String keysecret;
private String bucketname;
}
6.controller层,文件上传三要素
package com.qbb.yygh.oss.controller;
import com.qbb.yygh.oss.service.FileService;
import com.qbb.yygh.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
/**
* @author QiuQiu&LL (个人博客:https://www.cnblogs.com/qbbit)
* @version 1.0
* @date 2022-04-25 19:49
* @Description:
*/
@Api(tags = "阿里云OSS文件存储服务")
@RestController
@RequestMapping("/api/oss")
public class FileController {
@Autowired
private FileService fileService;
@ApiOperation("阿里云OSS文件上传")
@PostMapping("/upload")
public R upload(@ApiParam("上传的文件") @RequestParam("file") MultipartFile file) {
String url = fileService.upload(file);
return R.ok().data("url", url);
}
}
7.service上传核心代码,推荐参考官方文档
package com.qbb.yygh.oss.service.impl;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.qbb.yygh.oss.properties.OssProperties;
import com.qbb.yygh.oss.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.util.UUID;
/**
* @author QiuQiu&LL (个人博客:https://www.cnblogs.com/qbbit)
* @version 1.0
* @date 2022-04-25 19:55
* @Description:
*/
@Service
@Slf4j
public class FileServiceImpl implements FileService {
@Autowired
private OssProperties ossProperties;
@Override
public String upload(MultipartFile file) {
// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
String endpoint = ossProperties.getEndpoint();
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
String accessKeyId = ossProperties.getKeyid();
String accessKeySecret = ossProperties.getKeysecret();
// 填写Bucket名称,例如examplebucket。
String bucketName = ossProperties.getBucketname();
// 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
String randomStr = UUID.randomUUID().toString().replaceAll("-", "") + file.getOriginalFilename();
String dateStr = new DateTime().toString("yyyy/MM/dd");
String objectName = dateStr + "/" + randomStr;
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
InputStream inputStream = file.getInputStream();
// 创建PutObject请求。
ossClient.putObject(bucketName, objectName, inputStream);
// 返回上传的文件url ===> https://startqbb-hello.oss-cn-hangzhou.aliyuncs.com/文件名
StringBuilder sb = new StringBuilder();
sb.append("https://")
.append(bucketName)
.append(".")
.append(endpoint)
.append("/")
.append(objectName);
return sb.toString();
} catch (Exception oe) {
log.info("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
return null;
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
8.测试一下


整合完毕