RustFS 是一款基于 Rust 语言开发的高性能存储服务,完全兼容 S3 协议,适用于 AI/ML、大数据、互联网、工业存储等多种场景。本文将详细介绍如何部署 RustFS 服务,并演示如何在 Spring Boot 应用中集成 RustFS。
RustFS 的主要特点:
确保你的系统已安装 Docker 环境。
创建一个名为 rustfs.sh
的部署脚本:
bash#!/bin/bash
curr_dir=$(pwd)
mkdir -p "$curr_dir"/data
chmod -R 755 "$curr_dir"
# 停止旧容器运行
docker stop rustfs
# 删除旧容器
docker rm rustfs
#docker rmi rustfs/rustfs
docker run -itd \
--name rustfs \
-e RUSTFS_SERVER_DOMAINS=tcuwzw.com \
-e RUSTFS_ACCESS_KEY=your_access_key \
-e RUSTFS_SECRET_KEY=your_secret_key \
-e RUSTFS_CONSOLE_ENABLE=true \
-e RUSTFS_TLS_PATH=/certs \
-p 9000:9000 \
-v "$curr_dir"/data:/data \
-v /data/nginx/ssl:/certs \
rustfs/rustfs:latest
exit 0
提示
物理主机需要开放防火墙9000端口
访问ip:9000即可看到如下页面,账号:your_access_key,密钥:your_secret_key
RUSTFS_ACCESS_KEY
和 RUSTFS_SECRET_KEY
: 设置访问凭证RUSTFS_CONSOLE_ENABLE
: 启用控制台RUSTFS_TLS_PATH
: TLS 证书路径-p 9000:9000
: 将容器 9000 端口映射到主机-v "$curr_dir"/data:/data
: 挂载数据目录bashchmod +x rustfs.sh
./rustfs.sh
在 pom.xml
中添加以下依赖:
xml<dependency>
<groupId>org.dromara.x-file-storage</groupId>
<artifactId>x-file-storage-spring</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
<version>2.29.29</version>
</dependency>
yamldromara:
x-file-storage: #文件存储配置
default-platform: amazon-s3-v2-1 #默认使用的存储平台
thumbnail-suffix: ".min.jpg" #缩略图后缀,例如【.min.jpg】【.png】
access-key: your_access_key
secret-key: your_secret_key
end-point: http://your_server_ip:9000/ # RustFS 服务地址
#对应平台的配置写在这里,注意缩进要对齐
amazon-s3-v2:
- platform: amazon-s3-v2-1 # 存储平台标识
enable-storage: true # 启用存储
access-key: ${dromara.x-file-storage.access-key}
secret-key: ${dromara.x-file-storage.secret-key}
region: ap-east-1 # 必填
end-point: ${dromara.x-file-storage.end-point}
bucket-name: ${spring.application.name}
domain: http://your_server_ip:9000/file/download # 资源访问地址
base-path: / # 文件服务器的存储路径路径
javaimport software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.net.URI;
@Configuration
public class S3Config {
@Value("${dromara.x-file-storage.end-point}")
private String endPoint;
@Value("${dromara.x-file-storage.access-key}")
private String accessKey;
@Value("${dromara.x-file-storage.secret-key}")
private String secretKey;
@Bean
public S3Client s3Client() {
return S3Client.builder()
.endpointOverride(URI.create(endPoint)) // RustFS 地址
.region(Region.US_EAST_1) // 可写死,RustFS 不校验 region
.credentialsProvider(
StaticCredentialsProvider.create(
AwsBasicCredentials.create(accessKey, secretKey)
)
)
.forcePathStyle(true) // 关键配置!RustFS 需启用 Path-Style
.build();
}
}
javaimport cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import software.amazon.awssdk.core.ResponseInputStream;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@RestController
@RequestMapping("/file")
public class FileController {
@Value("${spring.application.name}")
private String bucketName;
private final S3Client s3Client;
private final FileStorageService fileStorageService;
private final HttpServletResponse response;
/**
* 上传文件
*/
@PostMapping("/upload")
public Object upload(@RequestPart("file") MultipartFile file) {
FileInfo fileInfo = fileStorageService.of(file).upload();
// 返回文件信息
return fileInfo;
}
/**
* 下载文件
*/
@GetMapping("/download/{fileName}")
public void download(@PathVariable String fileName) throws IOException {
ResponseInputStream<GetObjectResponse> objectStream = s3Client.getObject(
GetObjectRequest
.builder()
.bucket(bucketName)
.key(fileName)
.build());
IoUtil.write(response.getOutputStream(), true, objectStream.readAllBytes());
}
}
使用 Postman 或 curl 测试文件上传:
bashcurl -X POST -F "file=@/path/to/your/file.jpg" http://localhost:8080/file/upload
通过浏览器或 curl 访问下载接口:
bashcurl -o downloaded_file.jpg http://localhost:8080/file/download/your_file_name.jpg
RustFS 提供了一种高性能、兼容 S3 协议的存储解决方案,特别适合需要处理海量数据的应用场景。通过本文的介绍,你应该已经掌握了 RustFS 的基本部署和使用方法。如需了解更多高级功能,请参考官方文档。
如果在使用过程中遇到任何问题,欢迎在评论区留言讨论。