简体   繁体   English

使用 Spring 集成为文件轮询 S3 Bucket

[英]Polling S3 Bucket for file using Spring integration

I am working on a project where I am required to poll S3 bucket for files and upload in a different S3 bucket.我正在开发一个项目,我需要轮询 S3 存储桶中的文件并上传到不同的 S3 存储桶中。 As a first step to implementing it, I am trying to poll S3 bucket for new files created and create them in my local directory using Spring Integration.作为实现它的第一步,我尝试轮询 S3 存储桶以查找创建的新文件,并使用 Spring Integration 在我的本地目录中创建它们。 To achieve that I have created a simple spring-boot application with maven with the below object polling configuration while handles the fileReading IntegrationFlow为了实现这一点,我使用 maven 创建了一个简单的 spring-boot 应用程序,并在处理 fileReading IntegrationFlow 时使用以下对象轮询配置

@Configuration
@EnableIntegration
@IntegrationComponentScan
@EnableAsync
public class ObjectPollerConfiguration {
    @Value("${amazonProperties.bucketName}")
    private String bucketName;
    public static final String OUTPUT_DIR2 = "target2";
    @Autowired
    private AmazonClient amazonClient;
    @Bean
    public S3InboundFileSynchronizer s3InboundFileSynchronizer() {
        S3InboundFileSynchronizer synchronizer = new S3InboundFileSynchronizer(amazonClient.getS3Client());
        synchronizer.setDeleteRemoteFiles(true);
        synchronizer.setPreserveTimestamp(true);
        synchronizer.setRemoteDirectory(bucketName);            
        return synchronizer;
    }
    @Bean
    @InboundChannelAdapter(value = "s3FilesChannel", poller = @Poller(fixedDelay = "30"))
    public S3InboundFileSynchronizingMessageSource s3InboundFileSynchronizingMessageSource() {
        S3InboundFileSynchronizingMessageSource messageSource =
                new S3InboundFileSynchronizingMessageSource(s3InboundFileSynchronizer());
        messageSource.setAutoCreateLocalDirectory(true);
        messageSource.setLocalDirectory(new File("."));
        messageSource.setLocalFilter(new AcceptOnceFileListFilter<File>());
        return messageSource;
    }
    @Bean
    public PollableChannel s3FilesChannel() {
        return new QueueChannel();
    }
    @Bean
    IntegrationFlow fileReadingFlow() {
        return IntegrationFlows
                .from(s3InboundFileSynchronizingMessageSource(),
                        e -> e.poller(p -> p.fixedDelay(30, TimeUnit.SECONDS)))
                .handle(fileProcessor())
                .get();
    }
    @Bean
    public MessageHandler fileProcessor() {
        FileWritingMessageHandler handler = new FileWritingMessageHandler(new File(OUTPUT_DIR2));
        handler.setExpectReply(false); // end of pipeline, reply not needed
        return handler;
    }
}*

But when I start my application as a java application and upload files to S3, I don't see the target2 directory with file nor getting any logs corresponding to polling execution.但是,当我将我的应用程序作为 Java 应用程序启动并将文件上传到 S3 时,我没有看到包含文件的 target2 目录,也没有获取与轮询执行相对应的任何日志。 Can someone help me to get it working ?有人可以帮我让它工作吗?

I think the problem that you don't use your OUTPUT_DIR2 property for local dir to push into.我认为您不使用OUTPUT_DIR2属性来推动本地目录的问题。

Your code for local dir is like this:你的本地目录代码是这样的:

messageSource.setLocalDirectory(new File("."));

This is fully not what you are looking for.这完全不是你要找的。 Try to change it into the尝试将其更改为

messageSource.setLocalDirectory(new File(OUTPUT_DIR2));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM