1.对象存储OSS
为了解决海量数据存储与弹性扩容,项目中我们采用云存储的解决方案- 阿里云OSS。
1、开通“对象存储OSS”服务
(1)申请阿里云账号
(2)实名认证
(3)开通“对象存储OSS”服务
(4)进入管理控制台
2、创建Bucket
选择:标准存储、公共读、不开通

3、上传默认头像
创建文件夹avatar,上传默认的用户头像

4、创建RAM子用户



(1)添加用户

(2)设置用户组权限:AliyunOSSFullAccess

(3)获取子用户AccessKeyId,AccessKeySecret

2. 使用SDK
1、创建Mavaen项目
com.zzxx
aliyun-oss
2、pom
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <dependencies> <!--aliyunOSS--> <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>2.8.3</version> </dependency> <!--日期时间工具--> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.10.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
|
3、找到编码时需要用到的常量值
- aendpoint
- bucketName
- accessKeyId
- accessKeySecret
4、配置application.properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #服务端口 server.port=8001 #服务名 spring.application.name=guli-oss
#环境设置:dev、test、prod spring.profiles.active=dev
#阿里云 OSS #不同的服务器,地址不同 aliyun.oss.file.endpoint=your endpoint aliyun.oss.file.keyid=your accessKeyId aliyun.oss.file.keysecret=your accessKeySecret #bucket可以在控制台创建,也可以使用java代码创建 aliyun.oss.file.bucketname=guli-file aliyun.oss.file.filehost=avatar
|
5、创建启动类
创建OssApplication.java
1 2 3 4 5 6 7 8 9 10
| package com.guli.oss; @SpringBootApplication @ComponentScan(basePackages={"com.zzxx.oss","com.zzxx.common"}) public class OssApplication {
public static void main(String[] args) { SpringApplication.run(OssApplication.class, args); } }
|
6、启动项目
报错

spring boot 会默认加载org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration这个类,
而DataSourceAutoConfiguration类使用了@Configuration注解向spring注入了dataSource bean,又因为项目(oss模块)中并没有关于dataSource相关的配置信息,所以当spring创建dataSource bean时因缺少相关的信息就会报错。
解决办法:
方法1、在@SpringBootApplication注解上加上exclude,解除自动加载DataSourceAutoConfiguration
1
| @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
|
3. 实现文件上传
1、从配置文件读取常量
创建常量读取工具类:ConstantPropertiesUtil.java
使用@Value读取application.properties里的配置内容
用spring的 InitializingBean 的 afterPropertiesSet 来初始化配置信息,这个方法将在所有的属性被初始化后调用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| package com.zzxx.oss.util;
@Component
public class ConstantPropertiesUtil implements InitializingBean {
@Value("${aliyun.oss.file.endpoint}") private String endpoint;
@Value("${aliyun.oss.file.keyid}") private String keyId;
@Value("${aliyun.oss.file.keysecret}") private String keySecret;
@Value("${aliyun.oss.file.filehost}") private String fileHost;
@Value("${aliyun.oss.file.bucketname}") private String bucketName;
public static String END_POINT; public static String ACCESS_KEY_ID; public static String ACCESS_KEY_SECRET; public static String BUCKET_NAME; public static String FILE_HOST ;
@Override public void afterPropertiesSet() throws Exception { END_POINT = endpoint; ACCESS_KEY_ID = keyId; ACCESS_KEY_SECRET = keySecret; BUCKET_NAME = bucketName; FILE_HOST = fileHost; } }
|
2、文件上传
创建Service接口:FileService.java
1 2 3 4 5 6 7 8 9 10
| package com.guli.oss.service; public interface FileService {
String upload(MultipartFile file); }
|
实现:FileServiceImpl.java
参考SDK中的:Java->上传文件->简单上传->流式上传->上传文件流

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| package com.zzxx.oss.service.impl; public class FileServiceImpl implements FileService {
@Override public String upload(MultipartFile file) {
String endPoint = ConstantPropertiesUtil.END_POINT; String accessKeyId = ConstantPropertiesUtil.ACCESS_KEY_ID; String accessKeySecret = ConstantPropertiesUtil.ACCESS_KEY_SECRET; String bucketName = ConstantPropertiesUtil.BUCKET_NAME; String fileHost = ConstantPropertiesUtil.FILE_HOST;
String uploadUrl = null;
try { OSSClient ossClient = new OSSClient(endPoint, accessKeyId, accessKeySecret); if (!ossClient.doesBucketExist(bucketName)) { ossClient.createBucket(bucketName); ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead); }
InputStream inputStream = file.getInputStream();
String filePath = new DateTime().toString("yyyy/MM/dd");
String original = file.getOriginalFilename(); String fileName = UUID.randomUUID().toString(); String fileType = original.substring(original.lastIndexOf(".")); String newName = fileName + fileType; String fileUrl = fileHost + "/" + filePath + "/" + newName;
ossClient.putObject(bucketName, fileUrl, inputStream);
ossClient.shutdown();
uploadUrl = "http://" + bucketName + "." + endPoint + "/" + fileUrl;
} catch (IOException e) { throw new GuliException(ResultCodeEnum.FILE_UPLOAD_ERROR); }
return uploadUrl; } }
|
添加枚举
1
| FILE_UPLOAD_ERROR(false, 21004, "文件上传错误");
|
3、控制层
创建controller:FileUploadController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| package com.zzxx.oss.controller;
@Api(description="阿里云文件管理") @CrossOrigin @RestController @RequestMapping("/admin/oss/file") public class FileController {
@Autowired private FileService fileService;
@ApiOperation(value = "文件上传") @PostMapping("upload") public R upload( @ApiParam(name = "file", value = "文件", required = true) @RequestParam("file") MultipartFile file) {
String uploadUrl = fileService.upload(file); return R.ok().message("文件上传成功").data("url", uploadUrl);
} }
|
配置Swagger
1
| http://zhangxin-imagesoss-cn-beijing.aliyuncs.com/career/2020/04/30/c98efa0e-f8b2-4977-8680-a669b944172a.png
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| package com.zzxx.oss.config;
@Configuration @EnableSwagger2 public class Swagger2Config {
@Bean public Docket webApiConfig(){
return new Docket(DocumentationType.SWAGGER_2) .apiInfo(webApiInfo()) .select() .paths(Predicates.not(PathSelectors.regex("/error.*"))) .build();
}
private ApiInfo webApiInfo(){
return new ApiInfoBuilder() .title("文件上传通用服务") .description("本文档描述了文件上传通用服务接口定义") .version("1.0") .contact(new Contact("Helen", "http://www.lovekhh.xyz", "zxzxzxzx@qq.com")) .build(); }
}
|
6、Swagger中测试文件上传
