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

相关推荐
IT_陈寒7 分钟前
《Redis性能翻倍的7个冷门技巧,90%开发者都不知道!》
前端·人工智能·后端
一线大码9 分钟前
SpringBoot 优雅实现接口的多实现类方式
java·spring boot·后端
花伤情犹在14 分钟前
Java Stream 高级应用:优雅地扁平化(FlatMap)递归树形结构数据
java·stream·function·flatmap
yaoxin52112326 分钟前
212. Java 函数式编程风格 - Java 编程风格转换:命令式 vs 函数式(以循环为例)
java·开发语言
Q_Q196328847526 分钟前
python+uniapp基于微信小程序的助眠小程序
spring boot·python·小程序·django·flask·uni-app·node.js
摇滚侠37 分钟前
Spring Boot 3零基础教程,WEB 开发 Thymeleaf 属性优先级 行内写法 变量选择 笔记42
java·spring boot·笔记
滑水滑成滑头38 分钟前
**发散创新:多智能体系统的探索与实践**随着人工智能技术的飞速发展,多智能体系统作为当今研究的热点领域,正受到越来越多关注
java·网络·人工智能·python
摇滚侠41 分钟前
Spring Boot 3零基础教程,WEB 开发 Thymeleaf 总结 热部署 常用配置 笔记44
java·spring boot·笔记
十年小站41 分钟前
一、新建一个SpringBoot3项目
java·spring boot
2401_8414956444 分钟前
【数据结构】最长的最短路径的求解
java·数据结构·c++·python·算法·最短路径·图搜索