当前位置: 首页 > news >正文

对象存储-OSS

目录

对象存储背景        

阿里云OSS


对象存储背景        

        单节点环境下,文件往往存储在tomcat服务器内,随着业务需求的增多,单节点已不能满足需求,项目架构需要扩展到多节点(见下图),此时文件经过nginx转发存储到某一个节点,在分布式架构下对文件的访问就存在问题(文件存储到节点A,查看文件请求到节点B访问不了文件)。

        在分布式环境下,需要引入集中管理文件的节点服务,改进如下:

        分布式文件服务解决方案有很多,常见的有:独立搭建文件服务器,如基于开源FastDFS搭建;引入第三方云储存方案,如基于阿里云的OSS;相比独立搭建文件服务器,云存储方案有诸多优势:SDK使用简单、运维成本低、图形化的管理控制台、提供强大的文件处理功能、CDN加速、服务扩展简单等,本文重点讲述基于阿里云的OSS方案。

阿里云OSS

        使用步骤如下:

        1、【阿里云官网】开通OSS服务

        2、【阿里云官网】创建Bucket

        重点关注标签有:Bucket 名称、Endpoint,本文新建的Bucket 名称 = gingko-sawyer,Endpoint = oss-cn-beijing.aliyuncs.com

        3、【阿里云官网】创建并使用RAM账号进行API访问或日常运维,请登录RAM控制台创建RAM账号,创建AccessKey,并将AccessKeyId和AccessKeySecret设置到本地电脑环境变量中。

        4、后台编写代码实现基于阿里云OSS文件上传及下载

        4.1、添加aliyun.oss依赖

<dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>3.17.4</version>
</dependency>

        4.2、修改配置,增加对oss的支持

server:port: 8080
#aliyun oss config
aliyun:oss:endpoint: oss-cn-beijing.aliyuncs.comregion: cn-beijingbucket-name: gingko-sawyerfoler-prefix: oss/host: https://gingko-sawyer.oss-cn-beijing.aliyuncs.com

        4.3 文件上传&下载程序

package com.ginko.aliyunoss.controller;import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.common.comm.Protocol;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.InputStream;@RestController
@RequestMapping("oss")
@Slf4j
public class OSSController {@Value("${aliyun.oss.endpoint}")private String endpoint;@Value("${aliyun.oss.region}")private String region;@Value("${aliyun.oss.bucket-name}")private String bucketName;@Value("${aliyun.oss.foler-prefix}")private String folerPrefix;//上传到oss中具体的文件夹目录@Value("${aliyun.oss.host}")private String host;@PostMapping("/upload")public String upload(@RequestParam("file") MultipartFile file) throws Exception {// 创建 ClientBuilderConfiguration 实例,用于配置 OSS 客户端参数ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();// 设置签名算法版本为 V4clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);// 设置使用 HTTPS 协议访问 OSS,保证传输安全性clientBuilderConfiguration.setProtocol(Protocol.HTTPS);// 创建 OSS 客户端实例OSS ossClient = OSSClientBuilder.create()// endpoint.endpoint(this.endpoint)// 从环境变量中获取访问凭证(需提前配置 OSS_ACCESS_KEY_ID 和 OSS_ACCESS_KEY_SECRET).credentialsProvider(CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider())// 设置客户端配置.clientConfiguration(clientBuilderConfiguration)// 设置region.region(this.region).build();try {// 填写Bucket名称String bucketName = this.bucketName;// 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。String objectName = this.folerPrefix + file.getOriginalFilename();InputStream inputStream = file.getInputStream();// 创建PutObjectRequest对象。PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);// 创建PutObject请求。PutObjectResult result = ossClient.putObject(putObjectRequest);System.out.println(result);} finally {// 当OSSClient实例不再使用时,调用shutdown方法以释放资源ossClient.shutdown();}return "success";}@PostMapping("/download")public String download() throws Exception {// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。String endpoint = this.endpoint;//从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();// 填写Bucket名称,例如examplebucket。String bucketName = this.bucketName;// 填写不包含Bucket名称在内的Object完整路径,例如testfolder/exampleobject.txt。String objectName = this.folerPrefix + "1.png";// 填写Object下载到本地的完整路径。String pathName = "D:\\rename.png";// 填写Bucket所在地域。以华东1(杭州)为例,Region填写为cn-hangzhou。String region = this.region;// 创建OSSClient实例。// 当OSSClient实例不再使用时,调用shutdown方法以释放资源。ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);OSS ossClient = OSSClientBuilder.create().endpoint(endpoint).credentialsProvider(credentialsProvider).clientConfiguration(clientBuilderConfiguration).region(region).build();try {// 下载Object到本地文件,并保存到指定的本地路径中。如果指定的本地文件存在会覆盖,不存在则新建。// 如果未指定本地路径,则下载后的文件默认保存到示例程序所属项目对应本地路径中。ObjectMetadata object = ossClient.getObject(new GetObjectRequest(bucketName, objectName), new File(pathName));} catch (OSSException oe) {System.out.println("Caught an OSSException, which means your request made it to OSS, "+ "but was rejected with an error response for some reason.");System.out.println("Error Message:" + oe.getErrorMessage());System.out.println("Error Code:" + oe.getErrorCode());System.out.println("Request ID:" + oe.getRequestId());System.out.println("Host ID:" + oe.getHostId());} catch (ClientException ce) {System.out.println("Caught an ClientException, which means the client encountered "+ "a serious internal problem while trying to communicate with OSS, "+ "such as not being able to access the network.");System.out.println("Error Message:" + ce.getMessage());} finally {if (ossClient != null) {ossClient.shutdown();}}return "success";}
}

        5、测试,通过postman模拟上传及下载文件

        如下图所示,1.png文件已经被上传到oss中,rename.png是从oss下载到本地的文件

http://www.dtcms.com/a/268769.html

相关文章:

  • PC端基于SpringBoot架构控制无人机(三):系统架构设计
  • Vite 常用配置详解
  • 创造一个无限可能的机器人世界!——Genesis开源项目了解一下
  • 【Linux | 网络】网络基础
  • Java面试宝典:异常
  • 145.在 Vue3 中使用 OpenLayers 设置原始图、模糊、色相翻转、阴影效果
  • 创客匠人创始人IP打造实录:从行业观察者到生态构建者
  • 如何解决pip安装报错ModuleNotFoundError: No module named ‘datetime’问题
  • 软件架构升级中的“隐形地雷”:版本选型与依赖链风险
  • 用c#一款U盘批量按扇区复制的程序
  • Nat.C|RiNALMo:通用 RNA 语言模型新突破,3600 万序列预训练,跨家族结构预测、剪接识别与功能注释全能泛化
  • grant之后还需要跟flush privilege吗?
  • 广告系统中的RTB详解
  • IT 与动环一体化运维的技术融合实践
  • Pandas 学习(数学建模篇)
  • 牛客周赛 Round 99题解
  • C++ --- list的简单实现
  • 沙箱逃逸漏洞
  • STEP 7 MicroWIN SMART V2.2 的详细安装步骤及注意事项
  • 股票筹码分布及其数据获取
  • validate CRI v1 image API for endpoint “unix:///run/containerd/containerd.sock“
  • 代码详细注释:递归查找指定目录及其子目录中的所有BMP位图文件,并通过双重验证确保找到的文件确实是合法的BMP文件。以下是详细的功能说明:DIY机器人工房
  • Maven 私库
  • [特殊字符] Excel 读取收件人 + Outlook 批量发送带附件邮件 —— Python 自动化实战
  • pyautogui库的一些鼠标操作
  • 医学 LLM 评估相关论文笔记
  • OSPF路由过滤
  • 【python实用小脚本-130】基于 Python 的 HTML 到 Markdown 转换工具:实现高效文档格式转换
  • 深度学习7(梯度下降算法改进)
  • SLAM文献之Efficient and Consistent Bundle Adjustment on Lidar Point Clouds(BALM)