news 2026/9/30 17:38:39

SpringBoot 集成 RustFS

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
SpringBoot 集成 RustFS

SpringBoot 集成 RustFS

  • 1 配置
    • 1.1 Docker容器
    • 1.2 Spring配置
  • 2 依赖
  • 3 代码
    • 3.1 FileController
    • 3.2 RustFsConfig
    • 3.3 RustFsProperties
    • 3.4 RustFsStorageService

1 配置

1.1 Docker容器

services:rustfs:image:rustfs/rustfs:latestcontainer_name:rustfsrestart:unless-stoppedsecurity_opt:-"no-new-privileges:true"networks:-hyacinthports:-"9500:9000"# S3 API,避开 MinIO 的 9000-"9501:9001"# RustFS 控制台environment:# 本机单磁盘开发模式,避免把同一物理磁盘伪装成四块独立磁盘。RUSTFS_VOLUMES:/dataRUSTFS_ADDRESS:0.0.0.0:9000RUSTFS_CONSOLE_ADDRESS:0.0.0.0:9001RUSTFS_CONSOLE_ENABLE:"true"RUSTFS_CONSOLE_CORS_ALLOWED_ORIGINS:"*"RUSTFS_ACCESS_KEY:rustfsadminRUSTFS_SECRET_KEY:rustfsadminRUSTFS_OBS_LOGGER_LEVEL:infoRUSTFS_OBS_LOG_DIRECTORY:/app/logsvolumes:-D:/Env/docker/rustfs/data:/data-D:/Env/docker/rustfs/logs:/app/logs

1.2 Spring配置

spring:application:name:spring-fileservlet:multipart:max-file-size:100MBmax-request-size:100MBrustfs:endpoint:${RUSTFS_ENDPOINT:http://localhost:9500}access-key:${RUSTFS_ACCESS_KEY:rustfsadmin}secret-key:${RUSTFS_SECRET_KEY:rustfsadmin}region:${RUSTFS_REGION:us-east-1}bucket:${RUSTFS_BUCKET:spring-file}

2 依赖

<?xml version="1.0" encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>4.1.1</version><relativePath/></parent><groupId>com.xu</groupId><artifactId>spring-file</artifactId><version>1.0.0</version><properties><java.version>25</java.version></properties><dependencyManagement><dependencies><dependency><groupId>software.amazon.awssdk</groupId><artifactId>bom</artifactId><version>2.55.6</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>software.amazon.awssdk</groupId><artifactId>s3</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-webmvc</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-webmvc-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.graalvm.buildtools</groupId><artifactId>native-maven-plugin</artifactId></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><executions><execution><id>default-compile</id><phase>compile</phase><goals><goal>compile</goal></goals><configuration><annotationProcessorPaths><path><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></path></annotationProcessorPaths></configuration></execution><execution><id>default-testCompile</id><phase>test-compile</phase><goals><goal>testCompile</goal></goals><configuration><annotationProcessorPaths><path><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></path></annotationProcessorPaths></configuration></execution></executions></plugin></plugins></build></project>

3 代码

3.1 FileController

packagecom.xu.spring.file.rustfs;importjava.io.IOException;importjava.util.Map;importorg.springframework.http.MediaType;importorg.springframework.web.bind.annotation.DeleteMapping;importorg.springframework.web.bind.annotation.PathVariable;importorg.springframework.web.bind.annotation.PostMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;importorg.springframework.web.multipart.MultipartFile;@RestController@RequestMapping("/api/files")publicclassFileController{privatefinalRustFsStorageServicestorageService;publicFileController(RustFsStorageServicestorageService){this.storageService=storageService;}@PostMapping(consumes={MediaType.MULTIPART_FORM_DATA_VALUE,MediaType.MULTIPART_MIXED_VALUE})publicMap<String,String>upload(@RequestParam("file")MultipartFilefile)throwsIOException{StringobjectKey=storageService.upload(file);returnMap.of("message","上传成功","key",objectKey);}@DeleteMapping("/{key}")publicMap<String,String>delete(@PathVariableStringkey){storageService.delete(key);returnMap.of("message","删除成功");}}

3.2 RustFsConfig

packagecom.xu.spring.file.rustfs;importjava.net.URI;importorg.springframework.boot.context.properties.EnableConfigurationProperties;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importsoftware.amazon.awssdk.auth.credentials.AwsBasicCredentials;importsoftware.amazon.awssdk.auth.credentials.StaticCredentialsProvider;importsoftware.amazon.awssdk.regions.Region;importsoftware.amazon.awssdk.services.s3.S3Client;@Configuration@EnableConfigurationProperties(RustFsProperties.class)publicclassRustFsConfig{@BeanpublicS3ClientrustFsS3Client(RustFsPropertiesproperties){AwsBasicCredentialscredentials=AwsBasicCredentials.create(properties.accessKey(),properties.secretKey());returnS3Client.builder().endpointOverride(URI.create(properties.endpoint())).region(Region.of(properties.region())).credentialsProvider(StaticCredentialsProvider.create(credentials)).forcePathStyle(true).build();}}

3.3 RustFsProperties

packagecom.xu.spring.file.rustfs;importorg.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties(prefix="rustfs")publicrecordRustFsProperties(Stringendpoint,StringaccessKey,StringsecretKey,Stringregion,Stringbucket){}

3.4 RustFsStorageService

packagecom.xu.spring.file.rustfs;importjava.io.IOException;importjava.io.InputStream;importjava.util.UUID;importorg.springframework.stereotype.Service;importorg.springframework.util.StringUtils;importorg.springframework.web.multipart.MultipartFile;importsoftware.amazon.awssdk.core.sync.RequestBody;importsoftware.amazon.awssdk.services.s3.S3Client;importsoftware.amazon.awssdk.services.s3.model.DeleteObjectRequest;importsoftware.amazon.awssdk.services.s3.model.PutObjectRequest;@ServicepublicclassRustFsStorageService{privatefinalS3Clients3Client;privatefinalRustFsPropertiesproperties;publicRustFsStorageService(S3Clients3Client,RustFsPropertiesproperties){this.s3Client=s3Client;this.properties=properties;}publicStringupload(MultipartFilefile)throwsIOException{if(file.isEmpty()){thrownewIllegalArgumentException("上传文件不能为空");}Stringsuffix="";StringoriginalFilename=file.getOriginalFilename();if(StringUtils.hasText(originalFilename)){intindex=originalFilename.lastIndexOf('.');if(index>=0){suffix=originalFilename.substring(index);}}StringobjectKey=UUID.randomUUID()+suffix;PutObjectRequestrequest=PutObjectRequest.builder().bucket(properties.bucket()).key(objectKey).contentType(StringUtils.hasText(file.getContentType())?file.getContentType():"application/octet-stream").build();try(InputStreaminputStream=file.getInputStream()){s3Client.putObject(request,RequestBody.fromInputStream(inputStream,file.getSize()));}returnobjectKey;}publicvoiddelete(StringobjectKey){DeleteObjectRequestrequest=DeleteObjectRequest.builder().bucket(properties.bucket()).key(objectKey).build();s3Client.deleteObject(request);}}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/30 17:36:51

Node.js彻底卸载重装指南:覆盖Windows、macOS与Linux

如果你搜到这篇教程&#xff0c;那多半是 Node.js 环境已经把你折腾得够呛了。可能是npm动不动报错&#xff0c;可能是node -v显示的版本怎么看都不对劲&#xff0c;也可能是装了某个工具之后整个命令行都瘫了。我自己在过去几年里给不同系统重装过无数次 Node.js&#xff0c;踩…

作者头像 李华
网站建设 2026/9/30 17:32:07

轻量落地合规追溯:零系统改造实现药品批次全链路数据溯源

药品批次追溯是医药生产、流通、经营企业的核心合规底线&#xff0c;也是药监核查、质量召回、风险处置的核心依据。当前多数医药企业已部署MES、WMS、ERP、进销存、追溯码管理等多套业务系统&#xff0c;但各系统各司其职、数据独立存储、业务链路互不贯通&#xff1a;生产批次…

作者头像 李华
网站建设 2026/9/30 17:26:25

车贷违约预测实战:随机森林与AdaBoost的风控模型调优指南

简介&#xff1a;面向金融风控与机器学习初学者&#xff0c;这份车贷违约预测实战资源提供了基于Python的完整建模流程。针对约20万条、49个字段的车贷数据集&#xff0c;实现了从数据加载、清洗、特征与目标分离&#xff0c;到数据集划分、模型训练与保存的全链路代码&#xf…

作者头像 李华
网站建设 2026/9/30 17:15:46

素材用过几次怎么追踪?本地使用计数与状态流转的实现思路

素材用过几次怎么追踪&#xff1f;本地使用计数与状态流转的实现思路先说一个我自己遇到的事。上个月翻素材库找一个半月前下过的空镜&#xff0c;翻到第三页突然发现——这条素材我好像在两条片子里都用过&#xff0c;但当时完全没印象。更麻烦的是&#xff0c;另一条结构很像…

作者头像 李华
网站建设 2026/9/30 17:13:24

员工自助身份认证怎么落地:安当ASP统一门户与无感MFA实践

一、为什么要把"身份"交还给用户 传统企业身份管理里&#xff0c;员工忘记密码、丢了第二因素设备、换手机要解绑&#xff0c;往往要提工单、等运维、走审批。对管理员来说&#xff0c;这些高频低风险的琐事占掉了大量精力&#xff1b;对员工来说&#xff0c;等待意味…

作者头像 李华
网站建设 2026/9/30 17:12:20

微信小程序实战:checkbox 与 radio 组件的动态样式控制

在微信小程序开发中&#xff0c;表单组件是非常基础且高频使用的部分。本文将通过一个完整的实战案例&#xff0c;带大家掌握 checkbox&#xff08;复选框&#xff09;和 radio&#xff08;单选框&#xff09;组件的用法&#xff0c;并实现通过它们动态控制文本样式的效果。一、…

作者头像 李华