Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Amazon S3: The request signature we calculated does not match the signature you provided

ErrorThe request signature we calculated does not match the signature you provided. Check your key and signing method.

Short answer

S3 computed a different signature from the one in your request. The usual causes are a wrong or mis-pasted secret access key, a server clock that is off, a proxy that edits headers or the query string, or presigned URL parameters that differ from the request. Test the same call from the AWS CLI to separate them.

  • Applies to PHP 8.3, aws-sdk-php 3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run the same operation with the AWS CLI on the same server. If the CLI works, your PHP code or its credentials are wrong; if it fails too, look at the clock, key and network path.

    date -u
    aws s3 ls s3://my-bucket --debug 2>&1 | tail -n 20
  2. 2Match what you see, then jump to its fix

    1. 1SignatureDoesNotMatch for every request, even a simple listingThe secret access key is wrong or was pasted with extra charactersFix
    2. 2Starts suddenly on one server, or after a VM resume, while keys have not changedThe server clock is not synchronisedFix
    3. 3Works from a laptop or the CLI on the server but fails through a corporate proxy, load balancer or CDNA proxy or CDN changes the headers or query stringFix
    4. 4The URL was generated fine but the browser or client request fails with 403A presigned URL is used with a different method or headers than it was signed forFix

What does the error look like?

  • The request signature we calculated does not match the signature you provided. Check your key and signing method.
  • Error SignatureDoesNotMatch, Status 403

Cause 1 of 4

The secret access key is wrong or was pasted with extra characters

1Check

AWS documentation for a related service describes this error as typically a copy and paste mistake: the secret key is wrong, or it does not belong to the access key ID. A trailing space or line break from a .env file is the usual form.

printf '[%s]\n' "$AWS_SECRET_ACCESS_KEY"
printf '%s' "$AWS_SECRET_ACCESS_KEY" | wc -c

2Fix

Re-copy the pair from IAM, or create a new access key. Trim whitespace when you load it, and keep key and ID together.

<?php
$s3 = new Aws\S3\S3Client([
    'region'  => 'eu-west-1',
    'version' => 'latest',
    'credentials' => [
        'key'    => trim(getenv('AWS_ACCESS_KEY_ID')),
        'secret' => trim(getenv('AWS_SECRET_ACCESS_KEY')),
    ],
]);

3Verify

A simple call succeeds.

aws s3 ls s3://my-bucket

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 2 of 4

The server clock is not synchronised

1Check

AWS lists an unsynchronised system clock first among the causes of SignatureDoesNotMatch, since even small drifts can invalidate signatures. Compare the server time with a trusted source.

timedatectl status

2Fix

Turn on NTP time synchronisation.

sudo timedatectl set-ntp true

3Verify

System clock synchronized: yes, then retry the request.

timedatectl status | grep -i synchronized

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 3 of 4

A proxy or CDN changes the headers or query string

1Check

AWS notes that some corporate proxies modify headers or query strings, which breaks the signature. Test without the proxy first.

curl -sI -o /dev/null -w '%{http_code}
' --noproxy '*' 'PRESIGNED_URL'

2Fix

Send requests to S3 directly, or configure the proxy to pass the signed headers and the query string through unchanged.

# environment of the PHP process: bypass the proxy for AWS hosts
NO_PROXY=.amazonaws.com

3Verify

The same request now returns 200.

curl -sI --noproxy '*' 'PRESIGNED_URL' | head -n 1

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 4 of 4

A presigned URL is used with a different method or headers than it was signed for

1Check

The HTTP method, headers and query string must match exactly between generating the URL and using it. A URL signed for GET will not work for PUT.

curl -i -X PUT --upload-file ./test.txt 'PRESIGNED_PUT_URL'

2Fix

Generate the URL for the exact command you will send (GetObject or PutObject), and send the same headers the signer included, such as Content-Type, if you signed them.

<?php
$cmd = $s3->getCommand('PutObject', ['Bucket' => 'my-bucket', 'Key' => 'a.txt']);
$req = $s3->createPresignedRequest($cmd, '+15 minutes');
echo (string) $req->getUri();

3Verify

Upload with the matching method succeeds.

curl -i -X PUT --upload-file ./test.txt 'PRESIGNED_PUT_URL' | head -n 1

Confirmed by docs.aws.amazon.com

Did this fix it?

How do I stop it coming back?

  • Use IAM roles on EC2, ECS or Lambda, so no long-lived secret has to be pasted anywhere.
  • Keep NTP enabled on every server.
  • Store keys in a secret store and load them without surrounding whitespace.
  • Let the SDK create presigned URLs instead of building signatures by hand.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.