使用 FlatFileItemReader 打开 AWS 签名 URL

问题描述 投票:0回答:1

在 Spring Batch 中,我尝试从 AWS S3 读取带有签名 URL 的文件

此 URL 具有以下参数:查询字符串中的

X-Amz-Algorithm=...&X-Amz-Date=...X-Amz-SignedHeaders=...&X-Amz-Expires=...&X-Amz-Credential=...&X-Amz-Signature=...

打开此文件时,

FlatFileItemReader
调用资源的
exists()
方法,从而操作HEAD方法来检查响应HTTP代码是否为200。

相反,它会收到 403,因为签名 URL 只允许用于 GET 方法:/

我在这里询问是否有人已经遇到过这个问题以及您是否有任何开箱即用的解决方案。 我发现我唯一的解决方案是编写自己的阅读器,但是

FlatFileItemReader
做得很好,除了这种情况:D

注意:

  • 在签名 URL 上使用 GET 方法效果很好
  • 我已经测试过将
    FlatFileItemReader
    严格参数设置为
    false

问候

amazon-s3 spring-batch
1个回答
0
投票

我找到了一种解决方法,通过重写 URLResource 并始终为

.exists()
.isReadable()
方法返回 true

/**
 * Automatically return true on <code>exists()</code> and <code>isReadable()</code> methods. This is made for signed <b>URL</b> that admit
 * only <b>GET</b> method while <code>exists()</code> and <code>isReadable()</code> use <b>HEAD</b> method and thus return a 403 instead of
 * expected 200 <b>HTTP</b> code. Used for instance in {@link FlatFileItemReader} which calls <code>exists()</code> and
 * <code>isReadable()</code> in <code>doOpen()</code> method.
 * 
 * @author pilak
 * @see FlatFileItemReader
 */
public class SignedURLResource extends UrlResource {

    public SignedURLResource(String path) throws MalformedURLException {
        super(path);
    }

    @Override
    public boolean exists() {
        return true;
    }

    @Override
    public boolean isReadable() {
        return true;
    }
}

这样做读者永远无法验证底层文件是否存在。但至少读者已经打开了。

我认为最好让签名的 URL 也允许 HEAD 方法,但如果这是不可能的......

© www.soinside.com 2019 - 2024. All rights reserved.