当前位置:网站首页>文件管理-阿里云OSS学习(一)
文件管理-阿里云OSS学习(一)
2022-07-13 18:05:00 【易天雨】
目录
一 导入依赖
<dependency>
<groupId>OSSUtils</groupId>
<artifactId>ecc-oss-utils</artifactId>
<version>0.0.2</version>
</dependency>
二 上传接口
2.1 OSS基础配置
ACCESS_KEY_ID=DDSFsdfsdfF5IoOlwV
ACCESS_KEY_SECRET=4sdfUsdfdsfeW4sdfds2tQ4vJ
ENDPOINT=http://oss-cn-shenzhen.aliyuncs.com
BUCKET_NAME=Sdfd-sdfds
FOLDER:上传的文件夹(如:prod)
public static String uploadOSSFile(File file) {
// 上传到阿里云
OSSClient ossClient = EccOSSClientUtils.getOSSClient(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
String url = EccOSSClientUtils.uploadObject(ossClient, file, BUCKET_NAME, FOLDER);
return url;
}
三 删除阿里云OSS文件
public static void deleteOSSFile(String url) {
int indexOf = url.indexOf("?");
url = url.substring(0, indexOf);
String[] split = url.split("/");
String fileName = split[split.length-1];
OSSClient ossClient = EccOSSClientUtils.getOSSClient(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
// 删除文件
EccOSSClientUtils.deleteFile(ossClient, BUCKET_NAME, FOLDER, fileName);
ossClient.shutdown();
}
四 文件先存储至本地服务器(springboot方式)
主要解决方式:一般先存储在本地服务器,然后最后上传至阿里云
4.1 配置文件添加
hcl.upload-path=D:/hcl-file/
spring.resources.static-locations=classpath:/public/,file:${hcl.upload-path}


@Override
public String uploadDraft(HttpServletRequest request,MultipartFile uploadFile) {
try {
String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd/"));
File file = new File(uploadPath + format);
if (!file.isDirectory()) {
file.mkdirs();
}
String oldName = uploadFile.getOriginalFilename();
String newName = UUID.randomUUID().toString().replace("-","") + oldName.substring(oldName.lastIndexOf("."));
uploadFile.transferTo(new File(file,newName));
String filePath = format + newName;
filePath = request.getScheme() + "://" + request.getServerName()+ ":" + request.getServerPort() + File.separator+ filePath;
return filePath;
}catch (IOException e){
throw new RuntimeException("文件上传失败!");
}
}
边栏推荐
- Week4
- Tree array + discretization
- 【LeetCode】1606. Find the server that handles the most requests
- Set up in Jenkins to show the summary of allure Report
- [software quality assurance notes] software quality assurance
- 丑数
- 26岁,干了三年自动化,月薪才12k,能跳槽找到一个更高薪资的工作吗?
- Basic introduction to flask 6 - Context
- Leetcode lecture - 1 Sum of two numbers (difficulty: simple)
- 都说软件测试有手就行,每个人都能做,但为何每年仍有大批被劝退的?
猜你喜欢

appium中控件坐标及控件属性获取

一位年薪35W的测试被开除,回怼的一番话,令人沉思

TCP协议详解

It is said that software testing can be done by everyone, but why are there still a large number of people who are discouraged every year?

基于SonarQube代码质量检查

Five years' experience: the monthly salary is 3000 to 30000, and the change of Test Engineers

從功能測試到自動化測試,實現薪資翻倍,我整理的超全學習指南【附學習筆記】
![Du test de fonction au test automatique pour doubler le salaire, mon guide d'apprentissage Super complet [joindre les notes d'apprentissage]](/img/59/dfc87939871832548acecc8ef1d2bf.jpg)
Du test de fonction au test automatique pour doubler le salaire, mon guide d'apprentissage Super complet [joindre les notes d'apprentissage]

2-3树 B树 b+树

软件测试真的干不到35岁吗?那咋办呢...我36了...
随机推荐
Generally speaking, cookies, sessions, and tokens are different
基于SonarQube代码质量检查
2021-11-7bugku做题记录25——POST
回溯
select / poll / epoll 讲解
2-3树 B树 b+树
【LeetCode】2028. Find the missing observation data
Flask基礎入門七-Cookie和Session
The core difference between fairness and unfairness of reentrantlock
刚接手的新产品怎么快速展开测试
【LeetCode】676. Implement a magic dictionary
通俗讲Cookie,Session,Token区别
MySQL lock mechanism
ReentrantLock的公平与非公平核心区别
Pytest系列-01-安装与入门
Interviewer: let's talk about how you solve the transaction problem in the distributed scenario?
Basic introduction to flask 7 cookies and sessions
【LeetCode】1217. 玩筹码
Scheduledthreadpoolexecutor source code and error explanation
数制转换与子网划分