javascript Amazon S3签名不匹配- AWS SDK Java

gj3fmq9x  于 2023-11-15  发布在  Java
关注(0)|答案(7)|浏览(314)

我有一个游戏应用程序,需要上传文件到S3。我们正在scala开发,并使用Java AWS SDK。
我在尝试上传文件时遇到问题,使用预签名的URL时,我一直收到403 SignatureDoesNotMatch。该URL是使用AWS Java SDK通过以下代码生成的:

def generatePresignedPutRequest(filename: String) = {
    val expiration = new java.util.Date();
    var msec = expiration.getTime() + 1000 * 60 * 60; // Add 1 hour.
    expiration.setTime(msec);

    s3 match {
      case Some(s3) => s3.generatePresignedUrl(bucketname, filename, expiration, HttpMethod.PUT).toString
      case None => {
        Logger.warn("S3 is not availiable. Cannot generate PUT request.")
        "URL not availiable"
      }
    }
  }

字符串
对于前端代码,我们遵循ioncannon article
上传文件的js函数(与文章中使用的相同)

function uploadToS3(file, url)
     {
       var xhr = createCORSRequest('PUT', url);
       if (!xhr) 
       {
         setProgress(0, 'CORS not supported');
       }
       else
       {
         xhr.onload = function() 
         {
           if(xhr.status == 200)
           {
             setProgress(100, 'Upload completed.');
           }
           else
           {
             setProgress(0, 'Upload error: ' + xhr.status);
           }
         };

         xhr.onerror = function() 
         {
           setProgress(0, 'XHR error.');
         };

         xhr.upload.onprogress = function(e) 
         {
           if (e.lengthComputable) 
           {
             var percentLoaded = Math.round((e.loaded / e.total) * 100);
             setProgress(percentLoaded, percentLoaded == 100 ? 'Finalizing.' : 'Uploading.');
           }
         };

         xhr.setRequestHeader('Content-Type', 'image/png');
         xhr.setRequestHeader('x-amz-acl', 'authenticated-read');

         xhr.send(file);
       }
     }


服务器的响应是

<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>SignatureDoesNotMatch</Code>
<Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>
<StringToSignBytes>50 55 bla bla bla...</StringToSignBytes>
<RequestId>F7A8F1659DE5909C</RequestId>
<HostId>q+r+2T5K6mWHLKTZw0R9/jm22LyIfZFBTY8GEDznfmJwRxvaVJwPiu/hzUfuJWbW</HostId>
<StringToSign>PUT

    image/png
    1387565829
    x-amz-acl:authenticated-read
    /mybucketname/icons/f5430c16-32da-4315-837f-39a6cf9f47a1</StringToSign>
<AWSAccessKeyId>myaccesskey</AWSAccessKeyId></Error>


我已经配置了CORS,仔细检查了AWS凭据,并尝试更改请求头。我总是得到相同的结果。为什么亚马逊告诉我签名不匹配?

vddsk6oq

vddsk6oq1#

怀疑OP仍然有这个问题,但对于任何遇到这个问题的人来说,这里是答案:
当向S3发出签名请求时,AWS会进行检查,以确保签名与浏览器发送的HTTP Header信息完全匹配。不幸的是,这需要阅读:http://s3.amazonaws.com/doc/s3-developer-guide/RESTAuthentication.html
然而,在上面的代码中,实际情况并非如此,JavaScript正在发送:

xhr.setRequestHeader('Content-Type', 'image/png');
xhr.setRequestHeader('x-amz-acl', 'authenticated-read');

字符串
但是在Java/Scala中,s3.generatePresignedUrl被调用时没有传入任何一个。所以结果签名实际上是告诉S3拒绝任何带有Content-Type或x-ams-acl头集的东西。哎呀(我也上当了)。
我见过浏览器自动发送Content-Type,所以即使它们没有显式地添加到头部,它们仍然可以进入S3。所以问题是,我们如何将Content-Type和x-amz-acl头部添加到签名中?
在AWS SDK中有几个重载的generatePresignedUrl函数,但其中只有一个允许我们传入除bucket-name、filename、recruitation-date和http-method之外的任何其他内容。
解决方案是:
1.创建一个GeneratePresignedUrlRequest对象,包含你的bucket和文件名。
1.调用setString、setContentType等来设置所有的头信息。
1.将其作为唯一参数传递给s3.generatePresignedUrl。
下面是要用途:使用的GeneratePresignedUrlRequest的正确函数定义:
http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/AmazonS3Client.html#generatePresignedUrl(com.amazonaws.services.s3.model.GeneratePresignedUrlRequest)
AWS GitHub存储库上的函数代码也有助于我了解如何编写解决方案。希望这对您有所帮助。

smdncfj3

smdncfj32#

我也遇到过类似的问题,在我的情况下,设置配置signatureVersion: 'v4'帮助解决了这个问题-
在JavaScript中:

var s3 = new AWS.S3({
  signatureVersion: 'v4'
});

字符串
改编自https://github.com/aws/aws-sdk-js/issues/902#issuecomment-184872976

goucqfw6

goucqfw63#

我刚刚在使用NodeJs AWS SDK时遇到了这个问题。这是由于使用了有效的凭据,* 但没有足够的权限。* 更改为我的管理员密钥解决了这个问题,而没有更改代码!编辑:管理员密钥是一个极端的测试示例;在真实的生活中,当然要遵循密钥管理和最小权限的最佳实践。

mftmpeh8

mftmpeh84#

我也遇到了同样的问题,但删除内容类型工作正常。特此分享完整的代码。

public class GeneratePresignedUrlAndUploadObject {
    private static final String BUCKET_NAME = "<YOUR_AWS_BUCKET_NAME>"; 
    private static final String OBJECT_KEY  = "<YOUR_AWS_KEY>";
    private static final String AWS_ACCESS_KEY = "<YOUR_AWS_ACCESS_KEY>";
    private static final String AWS_SECRET_KEY = "<YOUR_AWS_SECRET_KEY>";

    public static void main(String[] args) throws IOException {
        BasicAWSCredentials awsCreds = new BasicAWSCredentials(AWS_ACCESS_KEY, AWS_SECRET_KEY);

        AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion(Regions.US_EAST_1)
                .withCredentials(new AWSStaticCredentialsProvider(awsCreds)).build();

        try {
            System.out.println("Generating pre-signed URL.");
            java.util.Date expiration = new java.util.Date();
            long milliSeconds = expiration.getTime();
            milliSeconds += 1000 * 60 * 60;
            expiration.setTime(milliSeconds);

            GeneratePresignedUrlRequest generatePresignedUrlRequest = 
                    new GeneratePresignedUrlRequest(BUCKET_NAME, OBJECT_KEY);
            generatePresignedUrlRequest.setMethod(HttpMethod.PUT); 
            generatePresignedUrlRequest.setExpiration(expiration);
            URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest); 

            UploadObject(url);

            System.out.println("Pre-Signed URL = " + url.toString());
        } catch (AmazonServiceException exception) {
            System.out.println("Caught an AmazonServiceException, " +
                    "which means your request made it " +
                    "to Amazon S3, but was rejected with an error response " +
            "for some reason.");
            System.out.println("Error Message: " + exception.getMessage());
            System.out.println("HTTP  Code: "    + exception.getStatusCode());
            System.out.println("AWS Error Code:" + exception.getErrorCode());
            System.out.println("Error Type:    " + exception.getErrorType());
            System.out.println("Request ID:    " + exception.getRequestId());
        } catch (AmazonClientException ace) {
            System.out.println("Caught an AmazonClientException, " +
                    "which means the client encountered " +
                    "an internal error while trying to communicate" +
                    " with S3, " +
            "such as not being able to access the network.");
            System.out.println("Error Message: " + ace.getMessage());
        }
    }

    public static void UploadObject(URL url) throws IOException
    {
        HttpURLConnection connection=(HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("PUT");
        OutputStreamWriter out = new OutputStreamWriter(
                connection.getOutputStream());
        out.write("This text uploaded as object.");
        out.close();
        int responseCode = connection.getResponseCode();
        System.out.println("Service returned response code " + responseCode);

    }
}

字符串

m0rkklqb

m0rkklqb5#

有一个问题,windows上的mime类型将fileType设置为空字符串,它不起作用。只需处理空字符串并添加一些文件类型。

atmip9wb

atmip9wb6#

我在使用Java AWS SDK时遇到了SignatureDoesNotMatch错误。在我的例子中,在升级maven依赖项而不更改代码后发生了SignatureDoesNotMatch错误。(因此凭据是正确的,没有被更改)。将依赖项org.apache.httpcomponents:httpclient从版本4.5.6升级到4.5.7后(实际上是Spring Boot2.1.2升级到2.1.3bom已经指定了httpclient版本),代码在执行一些AWS SDK S3请求(如AmazonS3.getObject)时成为抛出异常。
在深入挖掘根本原因后,我发现httpclient库使用规范化URI进行了破坏性更改,影响了Java AWS SDK S3。请查看打开的GitHub ticket org.apache.httpcomponents:httpclient:4.5.7 breaks fetching S3 objects以了解更多详细信息。

piwo6bdm

piwo6bdm7#

如果您的访问密钥和密钥都是正确的,但它显示“SignatureDoesNotmatch”,请检查您的密钥,它可能具有某些特殊字符,例如+/ - / *
转到aws并生成另一个访问密钥,其中密钥没有这些。然后再试一次:)

相关问题