当前位置:网站首页>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);
}
}
调用

边栏推荐
- Tencent continues to wield the "big knife" to reduce costs and increase efficiency, and free catering benefits for outsourced employees have been cut
- JS Deobfuscation - AST Restoration Case
- 接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
- Metasploit常用命令、技术功能模块
- 聊天尬死名场面,你遇到过吗?教你一键获取斗图表情包,晋升聊天达人
- 金山云地震,震源在字节?
- leetcode:332. 重新安排行程
- C. Omkar and Baseball
- Analyze the Add() method in Fragment management from the source code
- 国内手机厂商曾为它大打出手,如今它却最先垮台……
猜你喜欢
随机推荐
Flask之路由(app.route)详解
R语言拟合ARIMA模型并使用拟合模型进行预测推理:使用forecast函数计算ARIMA模型未来值(包含时间点、预测值、两个置信区间)
[Microservice~Nacos] Nacos service provider and service consumer
C 在函数声明前加typedef
xctf攻防世界 Web高手进阶区 shrine
p5.js实现的炫酷星体旋转动画
注意力引导网络用于视网膜图像分割
leetcode brush questions diary Calculate the number of elements on the right that is less than the current element
Arcgis工具箱无法使用,显示“XML包含错误“的解决方法
D. Binary String To Subsequences
Under the NVM node installation;The node environment variable configuration
开发者必备:一文快速熟记【数据库系统】和【软件开发模型】常用知识点
mysql multi-table left link query
mysql 、pg 查询日期处理
跨端技术方案选什么好?
【EF】数据表全部字段更新与部分字段更新
Leetcode.25 K个一组翻转链表(模拟/递归)
leetcode:332. 重新安排行程
In-depth analysis of Apache EventMesh cloud-native distributed event-driven architecture
C. Mere Array








