繁体   English   中英

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

[英]Polling S3 Bucket for file using Spring integration

我正在开发一个项目,我需要轮询 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 目录,也没有获取与轮询执行相对应的任何日志。 有人可以帮我让它工作吗?

我认为您不使用OUTPUT_DIR2属性来推动本地目录的问题。

你的本地目录代码是这样的:

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

这完全不是你要找的。 尝试将其更改为

messageSource.setLocalDirectory(new File(OUTPUT_DIR2));

暂无
暂无

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

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