【发布时间】:2017-04-17 21:41:07
【问题描述】:
我正在使用 spring 集成 aws 来轮询 S3 资源并从 S3 存储桶中获取文件并使用 spring 集成处理它们。 以下是我所拥有的:
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。我将此追溯到AbstractInboundFileSynchronizer.java 文件中的以下代码:
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\receipts\originalReceipts\inbound\receipt1.dat,它没有找到并给出 FileNotFoundExceptionerror。相反,它应该只是复制到本地文件夹 C:\SpringAws\S3inbound\receipt1.dat
-
拉动 S3 对象时,我注意到它拉动了
shipmentReceipts/receipts下的所有对象,而不是shipmentReceipts/receipts/originalReceipts/inbound在进一步调试时,我发现S3Session.java中的以下代码 sn-p 负责它:@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 {......
它将前缀设置为它遇到的第一个正斜杠/ 之后的所有内容。
我如何减轻这些?谢谢!
【问题讨论】:
标签: amazon-s3 spring-integration