SpringCloud Alibaba- 分布式文件存储 OSS

回到顶部(go to top)

一、文件存储架构演进

1.1 单机-普通上传

 

 

1.2 分布式-普通上传

由于负载均衡的存在,可能导致储存的时候路由到A服务器,获取的时候路由到B服务器,导致文件获取不到。

 

 

1.3 分布式-云存储

为文件存储单独设置一个服务器,哪怕有商品服务存在负载均衡,但是都存储在另外的文件存储服务器。

 

 

 

 

回到顶部(go to top)

二、云存储方案:阿里云OSS(object storage service)

 

 

2.1 相关术语

其中“访问密钥”很关键,说白了就是上传文件到OSS时所需的账号和密码

 

 

 

 

回到顶部(go to top)

三、文件上传架构演进

3.1 普通上传方式

优点:安全。通过自己的应用服务器上传,账号密码不会泄露。

缺点:每个请求要过一遍自己的应用服务器,高峰期会导致服务器资源紧张,出现性能瓶颈....

 

 

3.2 服务端签名后直传 (推荐)

 

 

 

回到顶部(go to top)

四、实践:普通上传方式

参考官方文档:https://github.com/alibaba/aliyun-spring-boot/tree/master/aliyun-spring-boot-samples/aliyun-oss-spring-boot-sample

4.1 引入依赖

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>aliyun-oss-spring-boot-starter</artifactId>
</dependency>

 

4.2 配置accessKeyId, secretAccessKey, endPoint

 

 

 

To get accessKey, secretKey, follow these steps:

  1. On the Alibaba Cloud console, click your avatar on the upper-right corner and click accesskeys. Or visit User Management page directly:

    undefined

  2. Get your accessKey、secretKey:

    undefined

Note: If you are using STS, you should configure securityToken in addition to accessKey, secretKey, and endpoint.

 

4.3 直接使用注入的OSSClient

Inject OSSClient and use it to upload files to the OSS server and download a file from OSS server.

Note: Direct injection into the OSSClient mode is typically used for scenarios where you need to handle a large number of file objects. If you only need to read the contents of the file object, OSS Starter also supports reading the file in Resource mode.

复制代码
 @Service
 public class YourService {
     @Autowired
     private OSSClient ossClient;

    //下载文件到本地
     public void saveFile() {
         ossClient.getObject(new GetObjectRequest(bucketName, objectName), new File("pathOfYourLocalFile"));
     }

     //上传文件到阿里云oss
     public void uploadFile(){
         InputStream inputStream = new FileInputStream("c:\\pic\\test.jpg");
         ossClient.putObject("bucketName","fileName",inputStream);
     }
    
 }
复制代码

 

 

回到顶部(go to top)

五、实践:服务端签名后直传

 

posted on   frank_cui  阅读(828)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2020-08-30 Java基础 - 继承里的 隐藏 vs覆盖?
2020-08-30 Java基础 - 父类和子类的初始化顺序
2020-08-30 java基础 - BIO、NIO、AIO 有什么区别?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

levels of contents
点击右上角即可分享
微信分享提示