如何从jQuery Ajax获取Amazon S3预先签名的URL对象

时间:2015-05-04 21:30:39

标签: ajax amazon-s3 amazon

我在Google上找不到任何内容。

我正在正确创建预先指定的网址,因为如果我使用浏览器加载请求,它们就可以正常工作。

然而,当使用来自jQuery ajax调用的相同url时,它失败并告诉我请求签名是错误的。

PS:这是标题。第一部分是成功的浏览器调用,第二部分是我的ajax失败。

HTTP/1.1 200 OK
x-amz-id-2: clb7J//+XLYa+XS4HJthLdDO0KxBJU02fyBt29Kr8A2TXRJXM189tGgy7bWgmoYkDzXWUhg3R5g=
x-amz-request-id: F3A8C4ED98E5443E
Date: Mon, 04 May 2015 21:53:24 GMT
Access-Control-Allow-Origin: <any valid value>
Access-Control-Allow-Methods: GET, DELETE, HEAD
Access-Control-Allow-Headers: accept, content-type
Access-Control-Max-Age: 3000
Access-Control-Allow-Credentials: true
Vary: Origin, Access-Control-Request-Headers, Access-Control-Request-Method
Content-Length: 0
Server: AmazonS3


BAD AJAX:

HTTP/1.1 403 Forbidden
Access-Control-Allow-Origin: <any valid value>
Access-Control-Allow-Methods: GET, DELETE, HEAD
Access-Control-Max-Age: 3000
Access-Control-Allow-Credentials: true
Vary: Origin, Access-Control-Request-Headers, Access-Control-Request-Method
x-amz-request-id: 05071412E03C4541
x-amz-id-2: xw5uMr2N/alPOR7MFMbX6fVkVEf1p30VhQKyP3yUqxYXxDq+vb5hzlsyShHwY4XhgAfLd3BCjG0=
Content-Type: application/xml
Transfer-Encoding: chunked
Date: Mon, 04 May 2015 21:53:24 GMT
Server: AmazonS3


Here is my Ajax call:

    $.ajax({
                type: "GET",
                url: presignedurl,
                contentType: 'application/json; charset=utf-8',
                cache: false,
                async: true,
                dataType: "text",
                error: function (xhr, status, error) {
                    CentralScrutinizer("FetchDetails Exception: " + error);
                },
                success: function (payload) {
                    try {
                        $('#newTaskDetails').val(payload);

                    } catch (e) {

                    }

                }
            });

The specific error is:

<?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>

Again, the closest I could find was a similar SO post for a put but they merely suggested adding the following headers:

//headers: { 'Content-Type': 'application/json; charset=utf-8' },

I tried that but it made no difference.

I also used fiddler to compare the two GETs (browser versus ajax) and the only thing different I see is the jsonp callback parameter.

That shouldn't affect the signature, should it?

Here are the headers

Browser Success:

    HTTP/1.1 200 OK
    x-amz-id-2: clb7J//+XLYa+XS4HJthLdDO0KxBJU02fyBt29Kr8A2TXRJXM189tGgy7bWgmoYkDzXWUhg3R5g=
    x-amz-request-id: F3A8C4ED98E5443E
    Date: Mon, 04 May 2015 21:53:24 GMT
    Access-Control-Allow-Origin: <any valid origin>
    Access-Control-Allow-Methods: GET, DELETE, HEAD
    Access-Control-Allow-Headers: accept, content-type
    Access-Control-Max-Age: 3000
    Access-Control-Allow-Credentials: true
    Vary: Origin, Access-Control-Request-Headers, Access-Control-Request-Method
    Content-Length: 0
    Server: AmazonS3

BAD AJAX:

    HTTP/1.1 403 Forbidden
    Access-Control-Allow-Origin:: <any valid origin>
    Access-Control-Allow-Methods: GET, DELETE, HEAD
    Access-Control-Max-Age: 3000
    Access-Control-Allow-Credentials: true
    Vary: Origin, Access-Control-Request-Headers, Access-Control-Request-Method
    x-amz-request-id: 05071412E03C4541
    x-amz-id-2: xw5uMr2N/alPOR7MFMbX6fVkVEf1p30VhQKyP3yUqxYXxDq+vb5hzlsyShHwY4XhgAfLd3BCjG0=
    Content-Type: application/xml
    Transfer-Encoding: chunked
    Date: Mon, 04 May 2015 21:53:24 GMT
    Server: AmazonS3

谢谢!

1 个答案:

答案 0 :(得分:1)

我找到了答案。

我从Ajax请求中删除了以下内容:

contentType:&#39; application / json;字符集= UTF-8&#39;,

它运作得很好。

我通过比较响应头并减去两者之间的共同点来解决这个问题。

一旦我得到了相似之处,唯一的区别就是内容长度和内容类型。

因此,作为一项实验,我只是注释掉了contentType属性并且它有效。

s3签名和ajax不会经常发生这种情况。我觉得自己很幸运,希望这对某人有所帮助。