springboot+camel对接minio上传下载

简介: springboot+camel对接minio上传下载

不要相信任何人,凡事都要自己用心,即使是有意让人恭维,也是可怕的。——爱·杨格

昨天我们已经实现了 minio上传下载

今天我们集成camel方式,相应的参数文档:

Minio :: Apache Camel

首先是依赖:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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>3.1.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ruben</groupId>
    <artifactId>simple-camel</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>simple-camel</name>
    <description>simple-camel</description>
    <properties>
        <java.version>17</java.version>
        <camel.version>4.0.1</camel.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-spring-boot-starter</artifactId>
            <version>${camel.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-minio-starter</artifactId>
            <version>${camel.version}</version>
        </dependency>
        <!-- we use the endpoint-dsl -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-endpointdsl</artifactId>
            <version>${camel.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-direct</artifactId>
            <version>${camel.version}</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

然后是配置文件application.yml

camel:
  component:
    minio:
      access-key: minioadmin
      secret-key: minioadmin
      endpoint: http://localhost:9000
      bucket: testbucket

之后是代码配置:

package com.ruben.simplecamel;
import org.apache.camel.builder.endpoint.EndpointRouteBuilder;
import org.springframework.stereotype.Component;
@Component
public class MinioRouteBuilder extends EndpointRouteBuilder {
    @Override
    public void configure() {
        // For the upload route
        from(direct("upload"))
                .to(file("sourceFolder?noop=true"))
                .to(minio("{{camel.component.minio.bucket}}"));
        // For the download route
        from(direct("download"))
                .to(file("targetFolder"))
                .to(minio("{{camel.component.minio.bucket}}").operation("getObject"));
    }
}

最后是使用:

package com.ruben.simplecamel;
import io.minio.GetObjectResponse;
import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.minio.MinioConstants;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.StreamUtils;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
@SpringBootTest
class MinioCamelTest {
    @Autowired
    private CamelContext camelContext;
    @Test
    void testUploadAndDownload() throws IOException {
        var content = "Hello, Minio!";
        ProducerTemplate producerTemplate = camelContext.createProducerTemplate();
        producerTemplate.sendBodyAndHeader("direct:upload", content, MinioConstants.OBJECT_NAME, "test.txt");
        GetObjectResponse response = (GetObjectResponse) producerTemplate.requestBodyAndHeader("direct:download", "", MinioConstants.OBJECT_NAME, "test.txt");
        Assertions.assertEquals(content, StreamUtils.copyToString(response, StandardCharsets.UTF_8));
    }
}

目录
打赏
0
0
0
0
29
分享
相关文章
🗄️Spring Boot 3 整合 MinIO 实现分布式文件存储
本文介绍了如何基于Spring Boot 3和MinIO实现分布式文件存储。随着应用规模扩大,传统的单机文件存储方案难以应对大规模数据和高并发访问,分布式文件存储系统成为更好的选择。文章详细讲解了MinIO的安装、配置及与Spring Boot的整合步骤,包括Docker部署、MinIO控制台操作、Spring Boot项目中的依赖引入、配置类编写及工具类封装等内容。最后通过一个上传头像的接口示例展示了具体的开发和测试过程,强调了将API操作封装成通用工具类以提高代码复用性和可维护性的重要性。
45 6
🗄️Spring Boot 3 整合 MinIO 实现分布式文件存储
Spring Boot 3 整合 Minio 实现文件存储
本文介绍了如何使用 Spring Boot 3 整合 MinIO 实现文件存储服务。MinIO 是一款高性能的对象存储服务器,适合大规模数据存储与分析,支持多种部署环境且文档完备、开源免费。从 MinIO 的快速安装、配置文件公开访问,到 Spring Boot 中集成 MinIO 客户端的步骤,包括创建用户访问密钥、引入依赖包、添加配置信息、编写 MinIO 客户端配置类及上传和预览文件的服务代码。最后通过 Apifox 进行文件上传测试,并验证文件是否成功存储及预览功能是否正常。关注公众号“Harry技术”,回复 minio 获取源码地址。
234 76
|
5月前
|
SpringBoot 整合 Minio
本文介绍了如何在服务器上安装并配置Minio服务,包括Minio的依赖、配置类以及基本操作。首先,通过Maven添加Minio依赖;接着,在`yml`文件中配置Minio的连接信息;然后,创建`MinIoClientConfig`类将MinioClient注入到Spring容器中;最后,定义`OSSFileService`接口及其实现类`OssFileServiceImpl`,实现文件上传、获取文件URL、临时访问URL和删除文件等操作。
133 2
|
3月前
|
Spring Boot集成MinIO
本文介绍了如何在Spring Boot项目中集成MinIO,一个高性能的分布式对象存储服务。主要步骤包括:引入MinIO依赖、配置MinIO属性、创建MinIO配置类和服务类、使用服务类实现文件上传和下载功能,以及运行应用进行测试。通过这些步骤,可以轻松地在项目中使用MinIO的对象存储功能。
195 5
springboot + minio + kkfile实现文件预览
本文介绍了如何在容器中安装和启动kkfileviewer,并通过Spring Boot集成MinIO实现文件上传与预览功能。首先,通过下载kkfileviewer源码并构建Docker镜像来部署文件预览服务。接着,在Spring Boot项目中添加MinIO依赖,配置MinIO客户端,并实现文件上传与获取预览链接的接口。最后,通过测试验证文件上传和预览功能的正确性。
322 4
springboot + minio + kkfile实现文件预览
SpringBoot操作Excel实现单文件上传、多文件上传、下载、读取内容等功能
SpringBoot操作Excel实现单文件上传、多文件上传、下载、读取内容等功能
255 8
springboot整合最新版minio和minio的安装(完整教程,新人必看)
本文详细介绍了如何使用Docker安装配置最新版的MinIO,并展示了如何在Spring Boot应用中整合MinIO以及如何通过前端进行文件上传测试。
542 3
springboot整合最新版minio和minio的安装(完整教程,新人必看)
SpringBoot中大量数据导出方案:使用EasyExcel并行导出多个excel文件并压缩zip后下载
在SpringBoot环境中,为了优化大量数据的Excel导出体验,可采用异步方式处理。具体做法是将数据拆分后利用`CompletableFuture`与`ThreadPoolTaskExecutor`并行导出,并使用EasyExcel生成多个Excel文件,最终将其压缩成ZIP文件供下载。此方案提升了导出效率,改善了用户体验。代码示例展示了如何实现这一过程,包括多线程处理、模板导出及资源清理等关键步骤。
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等