[英]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.