我使用spring集成aws來輪詢S3資源並從S3存儲桶獲取文件並使用spring集成來處理它們。 下面是我有:實現spring-integration-aws的問題
AmazonS3 amazonS3 = new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey));
@Bean
IntegrationFlow fileReadingFlow() {
return IntegrationFlows
.from(s3InboundFileSynchronizingMessageSource(),
e -> e.poller(p -> p.fixedDelay(30, TimeUnit.SECONDS)))
.handle(receiptProcessor())
.get();
}
@Bean
public S3InboundFileSynchronizer s3InboundFileSynchronizer() {
S3InboundFileSynchronizer synchronizer = new S3InboundFileSynchronizer(amazonS3);
synchronizer.setDeleteRemoteFiles(false);
synchronizer.setPreserveTimestamp(true);
synchronizer.setRemoteDirectory(s3BucketName.concat("/").concat(s3InboundFolder));
synchronizer.setFilter(new S3RegexPatternFileListFilter(".*\\.dat\\.{0,1}\\d{0,2}"));
return synchronizer;
}
@Bean
public S3InboundFileSynchronizingMessageSource s3InboundFileSynchronizingMessageSource() {
S3InboundFileSynchronizingMessageSource messageSource =
new S3InboundFileSynchronizingMessageSource(s3InboundFileSynchronizer());
messageSource.setAutoCreateLocalDirectory(false);
messageSource.setLocalDirectory(new File(inboundDir));
messageSource.setLocalFilter(new AcceptOnceFileListFilter<File>());
return messageSource;
}
,我的S3存儲和重點是:
bucketName = shipmentReceipts
key = receipts/originalReceipts/inbound/receipt1.dat
,所以我面臨的2個問題與此實現:
1. inboundDir文件夾名稱是使用s3key將其重命名爲不同的路徑名稱,從而導致FileNotFoundException
。
protected void copyFileToLocalDirectory(String remoteDirectoryPath, F remoteFile, File localDirectory,
Session<F> session) throws IOException {
String remoteFileName = this.getFilename(remoteFile);
String localFileName = **this.generateLocalFileName(remoteFileName);**
String remoteFilePath = remoteDirectoryPath != null
? (remoteDirectoryPath + this.remoteFileSeparator + remoteFileName)
: remoteFileName;
if (!this.isFile(remoteFile)) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("cannot copy, not a file: " + remoteFilePath);
}
return;
}
**File localFile = new File(localDirectory, localFileName);**
if (!localFile.exists()) {........
所以它結束了尋找一個文件路徑C:\ SpringAws \ S3inbound \收據\ originalReceipts \入境\ receipt1.dat其中它不找到,並給出我AbstractInboundFileSynchronizer.java
文件追蹤這對下面的代碼那FileNotFoundException
錯誤。相反,它應該僅僅被複制到本地目錄C:\ SpringAws \ S3inbound \ receipt1.dat
同時拉動S3對象我注意到它被拉動所有對象
shipmentReceipts/receipts
下代替shipmentReceipts/receipts/originalReceipts/inbound
在進一步調試我發現,在S3Session.java
下面的代碼片段是負責IT:@Override public S3ObjectSummary[] list(String path) throws IOException { Assert.hasText(path, "'path' must not be empty String."); String[] bucketPrefix = path.split("/"); Assert.state(bucketPrefix.length > 0 && bucketPrefix[0].length() >= 3, "S3 bucket name must be at least 3 characters long."); String bucket = resolveBucket(bucketPrefix[0]); ListObjectsRequest listObjectsRequest = new ListObjectsRequest() .withBucketName(bucket); if (bucketPrefix.length > 1) { **listObjectsRequest.setPrefix(bucketPrefix[1]);** } /* For listing objects, Amazon S3 returns up to 1,000 keys in the response. If you have more than 1,000 keys in your bucket, the response will be truncated. You should always check for if the response is truncated. */ ObjectListing objectListing; List<S3ObjectSummary> objectSummaries = new ArrayList<>(); do {......
它集前綴一切後,第斜線/
遇到。 我如何減輕這些?謝謝!
謝謝阿爾喬姆!我確實使用了spring-integration-aws的1.1.0.M1版本,但仍然編寫了自己的類來解決上述問題。 – user5758361
我正在使用Spring集成5.0.0.M4和Spring集成AWS 1.1.0.M2,並且在使用像'abc/def /'這樣的存儲桶名稱時仍然存在相同的問題。有關解決方法,請參閱下面的答案。我是流媒體,所以沒有可以操縱的本地文件名。 –
您是否介意提出GH問題,並提供更多詳細信息以從我們這邊複製?謝謝 –