【问题标题】:Streaming objects from S3 Object using Spring Aws Integration使用 Spring Aws 集成从 S3 对象流式传输对象
【发布时间】:2019-01-04 18:34:52
【问题描述】:

我正在研究一个我应该轮询 S3 的用例 -> 读取内容的流 -> 进行一些处理并将其上传到另一个存储桶,而不是在我的服务器中写入文件。

我知道我可以在 Spring aws 集成中使用 S3StreamingMessageSource 来实现它,但我面临的问题是我不知道如何处理通过轮询接收到的消息流

public class S3PollerConfigurationUsingStreaming {
    @Value("${amazonProperties.bucketName}")
    private String bucketName;

    @Value("${amazonProperties.newBucket}")
    private String newBucket;

    @Autowired
    private AmazonClientService amazonClient;

    @Bean
    @InboundChannelAdapter(value = "s3Channel", poller = @Poller(fixedDelay = "100"))
    public MessageSource<InputStream> s3InboundStreamingMessageSource() {    
        S3StreamingMessageSource messageSource = new S3StreamingMessageSource(template());
        messageSource.setRemoteDirectory(bucketName);
        messageSource.setFilter(new S3PersistentAcceptOnceFileListFilter(new SimpleMetadataStore(),
                "streaming"));      
        return messageSource;
    }

    @Bean
    @Transformer(inputChannel = "s3Channel", outputChannel = "data")
    public org.springframework.integration.transformer.Transformer transformer() {
        return new StreamTransformer();
    }

    @Bean
    public S3RemoteFileTemplate template() {
        return new S3RemoteFileTemplate(new S3SessionFactory(amazonClient.getS3Client()));
    }

    @Bean
    public PollableChannel s3Channel() {
        return new QueueChannel();
    }

    @Bean
    IntegrationFlow fileStreamingFlow() {
        return IntegrationFlows
                .from(s3InboundStreamingMessageSource(),
                        e -> e.poller(p -> p.fixedDelay(30, TimeUnit.SECONDS)))
                .handle(streamFile())
                .get();
    }

}

有人可以帮我处理流的代码吗?

【问题讨论】:

    标签: amazon-s3 streaming spring-integration spring-integration-aws


    【解决方案1】:

    不确定您的问题是什么,但我看到您有多种顾虑。如果您使用消息注释(请参阅配置中的@InboundChannelAdapter),那么在IntegrationFlow 定义中使用相同的s3InboundStreamingMessageSource 有什么意义?

    无论如何,您似乎已经为自己探索了StreamTransformer。这个有一个charset 属性,可以将您的InputStream从远程S3 资源转换为String。否则返回byte[]。其他一切都取决于您如何处理转换后的内容以及如何处理。

    另外,我认为没有理由将s3Channel 用作QueueChannel,因为@InboundChannelAdapter 无论如何都可以轮询您的流程的开始。

    我会说我们有更多的问题要问你,反之亦然......

    更新

    不清楚您对InputStream 处理的想法是什么,但事实上,在S3StreamingMessageSource 之后,您将在下一个处理程序中将InputStream 作为有效负载。

    也不确定您的streamFile() 是什么,但它必须确实期望InputStream 作为请求消息有效负载的输入。 你也可以使用上面提到的StreamTransformer:

    @Bean
    IntegrationFlow fileStreamingFlow() {
        return IntegrationFlows
                .from(s3InboundStreamingMessageSource(),
                        e -> e.poller(p -> p.fixedDelay(30, TimeUnit.SECONDS)))
                .transform(Transformers.fromStream("UTF-8"))
                .get();
    }
    

    下一个.handle() 将准备好将String 作为有效负载。

    【讨论】:

    • 非常感谢您指出与代码有关的问题。我的确切问题是如何在 streamFile() 方法中从远程 S3 资源获取输入流以便我可以处理它?我是 spring-integration 的新手,这就是为什么我没有掌握它的窍门
    • 我知道我必须从有效负载中获取文件,如下所示 MessageSource msgSource; msgSource.receive().getPayload() 。我的疑问是我从哪里得到 msgSource ?我知道这听起来可能很幼稚,但我对 spring-integration 很陌生
    • 我不知道如何解释你的简单方法,但你不会自己打电话给msgSource.receive()。带有轮询器的@InboundChannelAdapter 或ntegrationFlows.from() 可以轮询MessageSource 并为配置的通道生成消息,其中InputStream 作为每个远程文件的有效负载。
    • 在我的回答中查看更新。
    • 首先注释中的代码不可读。其次,如果您恰好需要InputStream,则根本不需要该转换器,但只需在processFile() 的参数中期望InputStream。在s3InboundStreamingMessageSource 的情况下没有File。请在参考手册中阅读更多内容:docs.spring.io/spring-integration/reference/html/…。您需要了解s3InboundStreamingMessageSource 生成的消息与InputStream 有效负载完全一致,而这正是您在handle() 的POJO 方法中所需要的。
    猜你喜欢
    • 1970-01-01
    • 2015-10-19
    • 2018-12-19
    • 1970-01-01
    • 2017-06-27
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多