对象存储OSS技术
使用背景
为了解决海量数据存储与弹性扩容,项目中我们采用云存储的解决方案- 阿里云OSS。
使用流程
1.开通对象存储oss服务
(1)申请阿里云账号
(2)实名认证
(3)开通“对象存储OSS”服务
(4)进入管理控制台
2.创建Bucket
3.java中使用oss技术
- 在配置文件中配置以下东西
#阿里云 OSS
#不同的服务器,地址不同
aliyun.oss.file.endpoint=oss-cn-beijing.aliyuncs.com
aliyun.oss.file.keyid=your keyid
aliyun.oss.file.keysecret=your keysecret
#bucket可以在控制台创建,也可以使用java代码创建
aliyun.oss.file.bucketname=sunset-education
- 创建一个类交给spring管理,同时读取配置文件的信息
package com.sunset.oss.utils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class ConstantPropertiesUtils 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.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;
@Override
public void afterPropertiesSet() throws Exception {
END_POINT = endpoint;
ACCESS_KEY_ID = keyId;
ACCESS_KEY_SECRET = keySecret;
BUCKET_NAME = bucketName;
}
}
- 在service层编写上传代码
package com.sunset.oss.service.impl;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.sunset.oss.service.OssService;
import com.sunset.oss.utils.ConstantPropertiesUtils;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.util.UUID;
@Service
public class OssServiceImpl implements OssService {
@Override
public String uploadFileAvatar(MultipartFile file) {
// yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
String endpoint = ConstantPropertiesUtils.END_POINT;
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;
String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;
String bucketName = ConstantPropertiesUtils.BUCKET_NAME;
OSS ossClient = null;
try {
// 创建OSSClient实例。
ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 填写本地文件的完整路径。如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。
InputStream inputStream = file.getInputStream();
String fileName = file.getOriginalFilename();
// 在文件名中生成唯一的一个随机值
String s = UUID.randomUUID().toString().replaceAll("-", "");
fileName = s+fileName;
// 把文件按照日期分类
// 获取当前日期
String date = new DateTime().toString("yyyy/MM/dd");
// 拼接
fileName = date +"/"+fileName;
// 依次填写Bucket名称(例如examplebucket)和Object完整路径(例如exampledir/exampleobject.txt)。Object完整路径中不能包含Bucket名称。
ossClient.putObject(bucketName, fileName, inputStream);
//https://sunset-education.oss-cn-beijing.aliyuncs.com/%600Z%25ESLMDL%401CI%5D7%5D7GYZPP.jpg
String url = "https://"+bucketName+"."+endpoint+"/"+fileName;
return url;
}catch (Exception e){
e.printStackTrace();
return null;
}finally {
// 关闭OSSClient。
ossClient.shutdown();
}
}
}
- 在controller层中调用service层的上传代码
package com.sunset.oss.controller;
import com.sunset.commonutils.Result;
import com.sunset.oss.service.OssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
@RequestMapping("/eduoss/fileoss")
//@CrossOrigin
public class OssController {
@Autowired
private OssService ossService;
@PostMapping
public Result uploadOssFile(MultipartFile file){
String url = ossService.uploadFileAvatar(file);//方法返回阿里云oss里面图片的路径
// 获取上传文件
return Result.ok().data("url",url);
}
}
- 启动项目并使用swaggeer进行测试