如何修复 aws s3 持续上传 SignatureDoesNotMatch?

时间:2021-03-04 09:42:45

标签: java amazon-web-services amazon-s3 pre-signed-url

我想获取 aws s3 的预先签名的 url 并放置图像。

但是,在收到预签名的 url 后,在请求带有该 url 的文件 PUT 时会出现错误。

请告诉我如何修复它。 T_T

获取signedURL 方法。

public String getSignedURL(String fileName){

    Date expiration=new Date();
    long expTimeMillis =expiration.getTime();
    expTimeMillis+=1000*60*1; //1Min
    expiration.setTime(expTimeMillis);

    //+uploadTime.format(DateTimeFormatter.ofPattern("yy.mm.dd HH:mm:ss z"))

    String objectKey="boardimages/"+fileName; //target.  buket/boardimages/(here my image)
    URL urls=null;
    try {
        GeneratePresignedUrlRequest url = new GeneratePresignedUrlRequest(bucketName, objectKey)
                .withMethod(HttpMethod.PUT)
                .withExpiration(expiration);

        url.addRequestParameter(Headers.S3_CANNED_ACL, CannedAccessControlList.PublicRead.toString());
        urls = s3Client.generatePresignedUrl(url);
    }catch(Exception e){
        e.printStackTrace();
    }

    System.out.println("pre-signed url : "+urls);
    return urls.toString();
}

上传html文件

<form role="form" action="#" th:action="${url}" method="post" enctype="multipart/form-data">
    <input type="hidden" name="_method" value="PUT"/>
<input type="file" name="file">
<input type="submit" value="click"></input>

错误信息

<Error>
<Code>SignatureDoesNotMatch</Code>
<Message>
The request signature we calculated does not match the signature you provided. Check your key and signing method.
</Message>

我的 s3 Cors。

[
{
    "AllowedHeaders": [
        "*"
    ],
    "AllowedMethods": [
        "HEAD",
        "GET",
        "PUT",
        "POST",
        "DELETE"
    ],
    "AllowedOrigins": [
        "*"
    ],
    "ExposeHeaders": []
}

我所有的公共访问权限都处于非活动状态。

如何解决??...

0 个答案:

没有答案