当前位置:网站首页>OSS文件上传
OSS文件上传
2022-08-09 22:01:00 【㏒灵韵№】
OSS文件上传
阿里云操作图解
1、注册登录阿里云
https://www.aliyun.com/
2、实名认证

3、开启OSS服务


4、使用OSS
(1)进入控制台
(2)创建根目录
创建bucket
(3)上传文件

阿里云OSS服务调用
1、如何对接




创建SpringBoot工程
自行创建好springBoot工程并引入相关启动类
引入相关依赖
<!-- 阿里云oss依赖 -->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
</dependency>
<!-- 日期工具栏依赖 -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
写好application.properties配置文件

#服务端口
server.port=8002
#服务名
spring.application.name=service-oss
#环境设置:dev、test、prod
spring.profiles.active=dev
#写自己的服务器地址参考如下
aliyun.oss.file.endpoint=oss-cn-shenzhen.aliyuncs.com
aliyun.oss.file.keyid=写自己的
aliyun.oss.file.keysecret=写自己的
#bucket可以在控制台创建,也可以使用java代码创建
#写自己的创建的文件路径 参考如下
aliyun.oss.file.bucketname=guli-da-file2022
创建配置类

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/** * @description: TODO * @author MIS * @date 2022/8/3 17:15 * @version 1.0 */
@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.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层
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.atguigu.baseservice.handler.GuliException;
import com.atguigu.ossservice.service.FileService;
import com.atguigu.ossservice.utils.ConstantPropertiesUtil;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
/** * @description: TODO * @author MIS * @date 2022/8/3 14:28 * @version 1.0 */
@Service
public class FileServiceImp implements FileService {
@Override
public String uploadFileOss(MultipartFile file) {
// yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
String endpoint = ConstantPropertiesUtil.END_POINT;
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
String accessKeyId = ConstantPropertiesUtil.ACCESS_KEY_ID;
String accessKeySecret = ConstantPropertiesUtil.ACCESS_KEY_SECRET;
String bucketName = ConstantPropertiesUtil.BUCKET_NAME;
String fileName = file.getOriginalFilename();
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
//上传文件流
InputStream inputStream = file.getInputStream();
//优化文件名不重复
fileName= UUID.randomUUID().toString()+fileName;
//优化文件存储路径//优化文件存储路径(/2022/08/03/uuid+01.jpg)
String path=new DateTime().toString("yyyy/MM/dd");
fileName=path+"/"+fileName;
ossClient.putObject(bucketName, fileName, inputStream);
// 关闭OSSClient。
ossClient.shutdown();
//https://guli-file201021.oss-cn-beijing.aliyuncs.com/01.jpg
String url ="https://"+bucketName+"."+endpoint+"/"+fileName;
return url;
} catch (IOException e) {
e.printStackTrace();
throw new GuliException(20001,"上传失败");
}
}
}
写好Controllerceng
/** * @author MIS * @version 1.0 * @description: TODO * @date 2022/8/3 14:29 */
@Api(description = "文件管理")
@RestController
@RequestMapping("/eduoss/fileoss")
@CrossOrigin
public class FileController {
@Autowired
FileService fileService;
@ApiOperation(value = "文件上传")
@PostMapping("/uploadFile")
public R uploadFile(MultipartFile file) {
String url = fileService.uploadFileOss(file);
return R.ok().data("url", url);
}
}
调用

边栏推荐
- Evolution of MLOps
- R语言检验时间序列的平稳性:使用tseries包的adf.test函数实现增强的Dickey-Fuller(ADF)检验、检验时序数据是否具有均值回归特性(平稳性)、不具有均值回归特性的案例
- 17-GuliMall 搭建虚拟域名访问环境
- Common commands and technical function modules of Metasploit
- R语言ggplot2可视化:使用ggpubr包的ggerrorplot函数可视化误差线(可视化不同水平均值点以及se标准误差)、设置add参数为dotplot添加点阵图
- MLOps的演进历程
- R语言拟合ARIMA模型并使用拟合模型进行预测推理:使用forecast函数计算ARIMA模型未来值(包含时间点、预测值、两个置信区间)
- Flutter 绘制美不胜收的光影流动效果
- Rust 解引用
- JuiceFS 在多云存储架构中的应用 | 深势科技分享
猜你喜欢

In-depth analysis of Apache EventMesh cloud-native distributed event-driven architecture

Five Star Holdings Wang Jianguo: Deepen the track with "plant spirit" and promote growth with "animal spirit"

leetcode:332. 重新安排行程

xctf攻防世界 Web高手进阶区 ics-05

Jinshanyun earthquake, the epicenter is in bytes?

月薪5K的运维小白如何成为月薪5W的高级架构师?

leetcode:286.墙和门

Good future, want to be a second new Oriental
![This article lets you quickly understand implicit type conversion [integral promotion]!](/img/16/4edc7ef23384b22d50ebd894b8911a.png)
This article lets you quickly understand implicit type conversion [integral promotion]!

十步以内,用小程序快速生成App!
随机推荐
mysql 、pg 查询日期处理
In-depth analysis of Apache EventMesh cloud-native distributed event-driven architecture
[Microservice~Nacos] Configuration Center of Nacos
面试官:Redis 大 key 要如何处理?
阿里云架构师金云龙:基于云XR平台的视觉计算应用部署
R语言检验时间序列的平稳性:使用tseries包的adf.test函数实现增强的Dickey-Fuller(ADF)检验、检验时序数据是否具有均值回归特性(平稳性)、不具有均值回归特性的案例
开发者必备:一文快速熟记【数据库系统】和【软件开发模型】常用知识点
一本通2074:【21CSPJ普及组】分糖果(candy)
十步以内,用小程序快速生成App!
【Apifox】为什么如此受青睐,此篇文章和大家分享
迅为瑞芯微RK3399开发板设置Buildroot文件系统测试MYSQL允许远程访问
你的 Link Button 能让用户选择新页面打开吗?
R语言使用mean函数计算样本(观测)数据中指定变量的相对频数:计算时间序列数据中大于前一个观测值的观测值所占的比例总体的比例
C. Binary String Reconstruction
three.js镂空圆球拖拽变形js特效
FileZilla搭建FTP服务器图解教程
Space not freed after TRUNCATE table
Arcgis工具箱无法使用,显示“XML包含错误“的解决方法
【GORM】模型关系-HasMany关系
Let's talk about what DDL, DML, DQL and DCL are in SQL statements