SeaweedFS S3 Spring Boot Starter

SeaweedFS S3 Spring Boot Starter

一个用于Spring Boot项目集成SeaweedFS S3协议的通用库,提供文件上传、下载、删除等操作。

源码

复制代码
1、Spring Boot Starter  实现源码工程
https://github.com/ShouZhiDuan/easydo/tree/main/s3-seaweedfs
2、Spring Boot Starter Client Test 集成测试Example工程
https://github.com/ShouZhiDuan/easydo/tree/main/s3-seaweedfs/s3-seaweedfs-test

特性

  • 🚀 开箱即用的Spring Boot自动配置
  • 📁 完整的S3协议支持
  • 🛠️ 便捷的工具类和服务类
  • 🔧 灵活的配置选项
  • 📝 完善的单元测试
  • 🎯 高性能文件操作

环境要求

  • Java 17+
  • Maven 3.6+
  • Spring Boot 2.7+

快速开始

1. 添加依赖

在你的Spring Boot项目中添加以下依赖:

xml 复制代码
<dependency>
    <groupId>com.techzhi.common</groupId>
    <artifactId>s3-seaweedfs-spring-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>

2. 配置文件

application.yml 中添加SeaweedFS配置:

yaml 复制代码
seaweedfs:
  s3:
    enabled: true
    access-key: nvx1
    secret-key: nvx1
    bucket-name: gongan
    endpoint: http://192.168.60.70:38333
    region: us-east-1
    path-style-access-enabled: true
    connection-timeout: 10000
    request-timeout: 30000
    max-connections: 50

3. 使用方式

方式一:注入服务类
java 复制代码
@Service
public class FileService {
    
    @Autowired
    private SeaweedFsS3Service seaweedFsS3Service;
    
    public void uploadFile(String key, byte[] data) {
        seaweedFsS3Service.uploadFile(key, data, "application/octet-stream");
    }
    
    public byte[] downloadFile(String key) {
        return seaweedFsS3Service.getFileBytes(key);
    }
}
方式二:使用工具类
java 复制代码
@RestController
public class FileController {
    
    @PostMapping("/upload")
    public String uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        String key = "uploads/" + file.getOriginalFilename();
        SeaweedFsS3Util.upload(key, file.getBytes(), file.getContentType());
        return "File uploaded successfully: " + key;
    }
    
    @GetMapping("/download/{key}")
    public ResponseEntity<byte[]> downloadFile(@PathVariable String key) {
        if (!SeaweedFsS3Util.exists(key)) {
            return ResponseEntity.notFound().build();
        }
        
        byte[] data = SeaweedFsS3Util.getBytes(key);
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + key + "\"")
                .body(data);
    }
}

API 文档

SeaweedFsS3Service 主要方法

方法 描述
uploadFile(key, inputStream, contentLength, contentType) 上传文件流
uploadFile(key, data, contentType) 上传字节数组
downloadFile(key) 下载文件对象
getFileInputStream(key) 获取文件输入流
getFileBytes(key) 获取文件字节数组
deleteFile(key) 删除文件
doesFileExist(key) 检查文件是否存在
getFileMetadata(key) 获取文件元数据
listFiles(prefix) 列出指定前缀的文件
listAllFiles() 列出所有文件
generatePresignedUrl(key, expiration) 生成预签名URL
copyFile(sourceKey, destinationKey) 复制文件

SeaweedFsS3Util 工具类方法

方法 描述
upload(key, data, contentType) 上传文件
download(key) 下载文件
getBytes(key) 获取文件字节数组
delete(key) 删除文件
exists(key) 检查文件是否存在
list(prefix) 列出文件
generatePresignedUrl(key) 生成预签名URL(1小时过期)
copy(sourceKey, destinationKey) 复制文件

配置参数

参数 默认值 描述
seaweedfs.s3.enabled true 是否启用SeaweedFS S3功能
seaweedfs.s3.access-key nvx1 S3访问密钥
seaweedfs.s3.secret-key nvx1 S3秘密密钥
seaweedfs.s3.bucket-name gongan 存储桶名称
seaweedfs.s3.endpoint http://192.168.60.70:38333 SeaweedFS S3端点
seaweedfs.s3.region us-east-1 AWS区域
seaweedfs.s3.path-style-access-enabled true 是否启用路径样式访问
seaweedfs.s3.connection-timeout 10000 连接超时时间(毫秒)
seaweedfs.s3.request-timeout 30000 请求超时时间(毫秒)
seaweedfs.s3.max-connections 50 最大连接数

运行测试

确保SeaweedFS服务正在运行,然后执行:

bash 复制代码
mvn test

构建项目

bash 复制代码
mvn clean install

注意事项

  1. 确保SeaweedFS服务已正确配置并启动S3网关
  2. 根据实际环境修改配置文件中的连接信息
  3. 在生产环境中,建议将敏感信息(如访问密钥)配置在环境变量中
  4. 大文件上传时,建议使用分片上传功能

集成应用

项目中如果需要用到以上的功能,可以快速集成。

复制代码
https://github.com/ShouZhiDuan/easydo/blob/main/s3-seaweedfs/s3-seaweedfs-test/README.md

🔔:记得修改自己实际的相关服务配置。

更多项目

🔔:https://github.com/ShouZhiDuan/easydo/blob/main/README.md

相关推荐
涡能增压发动积21 小时前
同样的代码循环 10次正常 循环 100次就抛异常?自定义 Comparator 的 bug 让我丢尽颜面
后端
云烟成雨TD21 小时前
Spring AI Alibaba 1.x 系列【6】ReactAgent 同步执行 & 流式执行
java·人工智能·spring
Wenweno0o21 小时前
0基础Go语言Eino框架智能体实战-chatModel
开发语言·后端·golang
于慨21 小时前
Lambda 表达式、方法引用(Method Reference)语法
java·前端·servlet
swg32132121 小时前
Spring Boot 3.X Oauth2 认证服务与资源服务
java·spring boot·后端
tyung1 天前
一个 main.go 搞定协作白板:你画一笔,全世界都看见
后端·go
gelald1 天前
SpringBoot - 自动配置原理
java·spring boot·后端
@yanyu6661 天前
07-引入element布局及spring boot完善后端
javascript·vue.js·spring boot
殷紫川1 天前
深入理解 AQS:从架构到实现,解锁 Java 并发编程的核心密钥
java
一轮弯弯的明月1 天前
贝尔数求集合划分方案总数
java·笔记·蓝桥杯·学习心得