使用Spring集成对S3存储桶进行文件轮询

时间:2018-12-29 15:50:23

标签: java spring amazon-s3 spring-integration polling

我正在一个项目中,我需要轮询S3存储桶中的文件并在另一个S3存储桶中上载。作为实现它的第一步,我试图轮询S3存储桶以查找创建的新文件,并使用Spring Integration在我的本地目录中创建它们。为此,我使用以下对象轮询配置使用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;
    }
}*

但是,当我以Java应用程序形式启动应用程序并将文件上传到S3时,我看不到target2目录包含文件,也看不到任何与轮询执行相对应的日志。有人可以帮助我使其正常工作吗?

1 个答案:

答案 0 :(得分:1)

我认为您没有将OUTPUT_DIR2属性用于本地目录的问题。

您的本地目录代码如下:

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

这完全不是您要的内容。 尝试将其更改为

messageSource.setLocalDirectory(new File(OUTPUT_DIR2));