阿里云OSS文件上传
原创
©著作权归作者所有:来自51CTO博客作者别团等shy哥发育的原创作品,请联系作者获取转载授权,否则将追究法律责任
阿里云OSS文件上传
- 1.1 申请阿里云账号
- 1.2 实名认证
- 1.3 开启“对象存储OSS”服务
- 1.4 进入管理控制台
- 2、创建Bucket
- 3、创建accesskeys许可证
- 4、搭建阿里云OSS操作项目环境
- 4.1 创建项目
- 4.2 在项目中引入相关oss依赖
- 4.3 创建配置文件
- 5.1 创建常量类,读取配置文件内容
- 5.2 创建controll,创建service
- 5.3 在service实现类中实现上传文件到oss过程
1、开通“对象存储OSS”服务
1.1 申请阿里云账号
1.2 实名认证
1.3 开启“对象存储OSS”服务
1.4 进入管理控制台
![在这里插入图片描述 阿里云OSS文件上传_上传文件](https://s2.51cto.com/images/blog/202302/10150141_63e5ebd5a1e4584510.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
2、创建Bucket
选择:低频访问、公共读、不开通
![在这里插入图片描述 阿里云OSS文件上传_阿里云_02](https://s2.51cto.com/images/blog/202302/10150142_63e5ebd63962b7254.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
3、创建accesskeys许可证
![在这里插入图片描述 阿里云OSS文件上传_阿里云_03](https://s2.51cto.com/images/blog/202302/10150142_63e5ebd6b096c19201.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
这个地方需要让你使用短信验证码验证
![在这里插入图片描述 阿里云OSS文件上传_云计算_04](https://s2.51cto.com/images/blog/202302/10150143_63e5ebd7035f687921.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
4、搭建阿里云OSS操作项目环境
4.1 创建项目
这里我是微服务项目,所以可能和你会有点差别
![在这里插入图片描述 阿里云OSS文件上传_上传文件_05](https://s2.51cto.com/images/blog/202302/10150143_63e5ebd751ab672222.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
4.2 在项目中引入相关oss依赖
我的父项目中已经引入过依赖了,所以子项目这里只需要引入对应的依赖就行,不用指定版本了。
<dependencies>
<!-- 阿里云oss依赖 -->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
</dependency>
<!-- 日期工具栏依赖 -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
</dependencies>
4.3 创建配置文件
application.properties
#服务端口
server.port=8002
#服务名
spring.application.name=service-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
上面关于oss对应的配置 你去你的管理控制台自己取
5、上传文件到阿里云接口实现
5.1 创建常量类,读取配置文件内容
package com.atguigu.oss.utils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//当项目一启动,spring接口,spring加载之后就会执行接口中的方法
@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;
}
}
5.2 创建controll,创建service
@RestController
@RequestMapping("/eduoss/fileoss")
@CrossOrigin //解决跨域
public class OssController {
@Autowired
private OssService ossService;
//上传头像的方法
@PostMapping
public R uploadOssFile(MultipartFile file){
//获取上传的文件 MultipartFile
//返回上传到oss的路径
String url=ossService.uploadFileAvatar(file);
return R.ok()
.data("url",url);
}
}
5.3 在service实现类中实现上传文件到oss过程
@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;
try{
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
//获取上传文件输入流
InputStream inputStream = file.getInputStream();
//获取文件名称
String fileName = file.getOriginalFilename();
//调用oss方法实现上传
//第一个参数 Bucket名称
//第二个参数 上传到oss文件路径和文件名称
//第三个参数 上传文件输入流
ossClient.putObject(bucketName, fileName, inputStream);
// 关闭OSSClient。
ossClient.shutdown();
//把上传之后的文件路径返回
//需要把上传呢到阿里云Oss路径手动拼接出来
//https://edu-1033.oss-cn-beijing.aliyuncs.com/21211060759.jpg
String url="https://"+bucketName+"."+endpoint+"/"+fileName;
return url;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
}
6、文件上传测试
6.1 使用swagger测试
访问:http://localhost:8002/swagger-ui.html
![在这里插入图片描述 阿里云OSS文件上传_spring_06](https://s2.51cto.com/images/blog/202302/10150143_63e5ebd7b725d38123.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
随便选择一张图片,从结果可看出已经上传成功
![在这里插入图片描述 阿里云OSS文件上传_spring_07](https://s2.51cto.com/images/blog/202302/10150144_63e5ebd83cd2946053.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
去阿里云的对象存储的控制台查看
![在这里插入图片描述 阿里云OSS文件上传_上传文件_08](https://s2.51cto.com/images/blog/202302/10150144_63e5ebd89332b65455.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
![在这里插入图片描述 阿里云OSS文件上传_上传_09](https://s2.51cto.com/images/blog/202302/10150144_63e5ebd8d7bd882306.bmp?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)