前言
在项目开发中,很多涉及到需要存储文件的业务点,由于不想自建文件服务器,因此考虑使用阿里云的oss对象存储;
OSS对象存储简介
对象存储服务(Object Storage Service,简称OSS)
阿里云对象存储OSS(Object Storage Service)是一款海量、安全、低成本、高可靠的云存储服务,提供99.9999999999%(12个9)的数据持久性,99.995%的数据可用性。多种存储类型供选择,全面优化存储成本。非常适合存储非结构化数据,例如视频、图形、日志、文本文件以及各种App应用、多终端同步软件、网盘下载站的文件等,单个文件的大小从1字节到48.8TB,可以存储的个数无限制;
前置操作
1、注册阿里云账户并登录至控制台
可以直接使用自己的支付宝账户登录,或者使用手机号注册;
2、登录控制台,开通对象存储OSS服务
3、创建bucket
使用OSS存储的基本逻辑单元是bucket,bucket即为桶的意思,创建一个桶,即创建了一个可以上传文件的容器,然后客户端就可以将文件上传到这个桶中;
点击工作台,创建bucket,如下我这里创建了一个名叫 "zcy1"的桶
然后可以基于这个bucket创建文件目录,并可以测试在线上传文件
点击右上角个人头像设置那里进入到 AccessKey管理
这个里面维护着个人创建的AccessKey信息,客户端程序中,需要使用这个AccessKey信息进行连接,并上传文件到OSS的bucket;
代码集成步骤
1、导入官方提供的SDK
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.8.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.0</version>
</dependency>
2、配置文件中配置控制台的AccessKey
#服务端口
server.port=8002
#服务名
spring.application.name=service-oss
#阿里云 OSS
#不同的服务器,地址不同
aliyun.oss.file.endpoint=管控台的endpoint地址
aliyun.oss.file.keyid=accessKeyId
aliyun.oss.file.keysecret=secretKey
#bucket可以在控制台创建,也可以使用java代码创建
aliyun.oss.file.bucketname=你创建的bucket名称
3、添加一个配置类,读取配置文件信息
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "oss.aliyun")
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;
}
}
4、编写一个测试接口,用于文件上传
import com.congge.service.OssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
public class OssController {
@Autowired
private OssService ossService;
//上传头像,返回图片的url给
@PostMapping
public String uploadOssFile(MultipartFile file) throws Exception{
//获取上传文件 MultipartFile
//返回图片在oss上的路径
String url = ossService.uploadFileAvatar(file);
return url;
}
}
5、业务实现类
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.congge.configs.ConstantPropertiesUtils;
import com.congge.service.OssService;
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) throws Exception{
String endpoint = ConstantPropertiesUtils.END_POINT;
String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;
String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;
String bucketName = ConstantPropertiesUtils.BUCKET_NAME;
String url = null;
//创建OSSClient实例。
OSS ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
//获取上传文件输入流
InputStream inputStream = file.getInputStream();
//获取文件名称
String fileName = file.getOriginalFilename();
//保证文件名唯一,去掉uuid中的'-'
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
fileName = uuid + fileName;
//把文件按日期分类,构建日期路径:avatar/2019/02/26/文件名
//String datePath = new Date().toString("yyyy/MM/dd");
String datePath = "2021";
//拼接
fileName = datePath + "/" + fileName;
//调用oss方法上传到阿里云
//第一个参数:Bucket名称
//第二个参数:上传到oss文件路径和文件名称
//第三个参数:上传文件输入流
ossClient.putObject(bucketName, fileName, inputStream);
//把上传后把文件url返回
//https://xppll.oss-cn-beijing.aliyuncs.com/01.jpg
url = "https://" + bucketName + "." + endpoint + "/" + fileName;
//关闭OSSClient
ossClient.shutdown();
return url;
}
}
6、主启动类
@SpringBootApplication
public class VersionApp {
public static void main(String[] args) {
SpringApplication.run(VersionApp.class,args);
}
}
运行上面的程序,调用上传接口,使用swagger调用即可,
上传成功后,接口会返回一串上传成功的url,
这时候我们再返回到控制台中查看下我们上传的这种图片
通过上面的步骤,我们就基于springboot与oss的整合 ,完成了上传文件到OSS的需求