tpyo / amazon-s3-php-class

A standalone Amazon S3 (REST) client for PHP 5/CURL
1.03k stars 504 forks source link

SignatureDoesNotMatch Errors #12

Closed smartinec closed 12 years ago

smartinec commented 13 years ago

We are using S3 class version 0.4.0 and keep receiving the error below. We use a number of custom meta headers. The most likely culprit is our keyword header that is defined as:

'keyword' => mb_encode_mimeheader($keyword, 'UTF-8');

We seem to receive these errors when $keyword is Russian, Chinese, Japanese, etc.

User Warning: S3::putObject(): [SignatureDoesNotMatch] The request signature we calculated does not match the signature you provided. Check your key and signing method. in S3.php on line 358

tpyo commented 13 years ago

Are you only getting signature errors when using UTF-8 headers? Do UTF-8 object names still work for you?

smartinec commented 13 years ago

I'm not sure exactly what you mean by UTF-8 object names. The 'keyword' header is always encoded as UTF-8 as is the actual content stored in S3 (search engine result page). Here is an example page we are trying to store that had a failure:

http://www.google.com.tw/search?q=%E5%AC%8C%E7%94%9F%E6%95%A3%E5%85%89%E9%9A%B1%E5%BD%A2%E7%9C%BC%E9%8F%A1&gl=TW&hl=zh_TW

We set Content-Type to what is returned by the engine. In this case "text/html; charset=UTF-8". We also set Content-Encoding to "gzip" since we use "gzencode()" on the response.

Slach commented 12 years ago

code for reproduce

S3::setAuth('XXX', 'XXX');
S3::setExceptions(true);

$put = S3::putObject(
    S3::inputFile('1.txt'),
    'your_bucket',
    '1.txt',
    S3::ACL_PUBLIC_READ,
    array( // Custom meta headers
    ),
    array( // Custom $requestHeaders
      "Cache-Control" => "max-age=315360000",
      "Expires" => gmdate("D, d M Y H:i:s T", strtotime("+5 years")),
      "Content-type" => "text/plain",
    )
)
var_dump($put);

Expected result bool(true)

Actual result Fatal error: Uncaught exception 'S3Exception' with message 'S3::putObject(): [SignatureDoesNotMatch] The request signature we calculated does not match the signa ture you provided. Check your key and signing method.' in S3.class.php:554

HTML Request

PUT /uploads/1.txt HTTP/1.1 User-Agent: S3/php Accept: / x-amz-acl: public-read Host: lingualeo_cdn.s3.amazonaws.com Date: Sat, 16 Jun 2012 15:21:58 GMT Content-MD5: aVSG6u6vFHNzw7+fh5KVCA== Content-Type: text/plain Cache-Control: max-age=315360000 Expires: Fri, 16 Jun 2017 15:21:58 GMT Content-type: text/plain Authorization: AWS AKIAJQPD6HCEUK55UXDQ:iac8pbUCyvYD2XhrnBwPEsb9K+o= Content-Length: 8 Expect: 100-continue

HTML Response

HTTP/1.1 403 Forbidden x-amz-request-id: 12631B4F2C527789 x-amz-id-2: pWcELTdRMhMpwLmzJY6aWppXRnqOWVJUJxVWB4ewOI6lDdmdQ9Kit/RgYiD1hscG Content-Type: application/xml Transfer-Encoding: chunked Date: Sat, 16 Jun 2012 15:22:10 GMT nnCoection: close Server: AmazonS3

41b <?xml version="1.0" encoding="UTF-8"?>

SignatureDoesNotMatchThe request signature we calculated does not match the signature you provided. Check your key and signing method.50 55 54 0a 61 56 53 47 36 75 36 76 46 48 4e 7a 77 37 2b 66 68 35 4b 56 43 41 3d 3d 0a 74 65 78 74 2f 70 6c 61 69 6e 2c 74 65 78 74 2f 70 6c 61 69 6e 0a 53 61 74 2c 20 31 36 20 4a 75 6e 20 32 30 31 32 20 31 35 3a 32 31 3a 35 38 20 47 4d 54 0a 78 2d 61 6d 7a 2d 61 63 6c 3a 70 75 62 6c 69 63 2d 72 65 61 64 0a 2f 6c 69 6e 67 75 61 6c 65 6f 5f 63 64 6e 2f 75 70 6c 6f 61 64 73 2f 31 2e 74 78 7412631B4F2C527789pWcELTdRMhMpwLmzJY6aWppXRnqOWVJUJxVWB4ewOI6lDdmdQ9Kit/RgYiD1hscGiac8pbUCyvYD2XhrnBwPEsb9K+o=PUT aVSG6u6vFHNzw7+fh5KVCA== text/plain,text/plain Sat, 16 Jun 2012 15:21:58 GMT x-amz-acl:public-read /lingualeo_cdn/uploads/1.txtAKIAJQPD6HCEUK55UXDQ

0

tpyo commented 12 years ago

Would you be able to try this patch? It seems like it is likely related to the meta header order.

https://github.com/codemedic/amazon-s3-php-class/commit/5e2d6f2772cc07ab0942e608bbda1569a5f4ddec

Slach commented 12 years ago

thanks! patch work fine... may be you merged this patch to main project repository?

tpyo commented 12 years ago

The patch uses a lambda style function, it should ideally be a separate function for older PHP versions.

But I'm busy implementing it now, should push changes later today.