如何在 Java SDK 2.0 中构建用于文件上传的 Amazon S3 Presigned PostPolicy?

How to build Amazon S3 Presigned PostPolicy for file uploading in Java SDK 2.0?

我需要生成一个 AWS Signature v4 签名以上传到 s3,如下所示:https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-post-example.html.

试了很多例子,都报错

<Error>
    <Code>InvalidAccessKeyId</Code>
    <Message>The AWS Access Key Id you provided does not exist in our records.</Message>
    <AWSAccessKeyId>ASIA2AKMADUN</AWSAccessKeyId>
    <RequestId>E68a1B73B15</RequestId>
    <HostId>fIG19S=</HostId>
</Error>

我尝试构建签名,使用 minio-java,像这样 https://github.com/minio/minio-java/blob/master/examples/PresignedPostPolicy.java

此外,我尝试了这段代码 https://gist.github.com/phstudy/3523576726d74a0410f8

P.S。我的真正目标是从具有文件大小限制的客户端上传文件,例如 there, or there . I can create presignS3UploadLink,但无法设置最大大小。

因此,由于缺少 x-amz-security-token 参数,解决方案 https://github.com/minio/minio-java/blob/master/examples/PresignedPostPolicy.java 无效。

我们需要使用 session-token(我们从亚马逊获得)来创建 POST-Policy 和表单发布 - https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-HTTPPOSTConstructPolicy.html

minio 示例代码:

conditions.add(new String[]{"eq", "$x-amz-security-token", sessionToken});
formData.put("x-amz-security-token", sessionToken);

P.S。 x-amz-security-token 是必需的,因为使用了临时安全凭证 - https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html