Maaz Zindani

Loading the index…

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

S3 returns 403 when you upload with a presigned URL

Error403 (Forbidden)

Short answer

A 403 from a presigned URL means S3 refused the signature, the permissions behind it, or the credentials' lifetime. Send the request with curl and read the error code in the XML body: AccessDenied points to permissions or a bucket policy, ExpiredToken to credentials, SignatureDoesNotMatch to a request that differs from the one signed.

  • Applies to Amazon S3 SigV4, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Take axios and the browser out of the picture and upload with curl using the exact URL. S3 sends an XML body whose Code line names the cause.

    curl -i -X PUT --upload-file ./test.txt "PASTE_PRESIGNED_URL"
  2. 2Match what you see, then jump to its fix

    1. 1<Code>AccessDenied</Code>The identity that signed the URL cannot do the operationFix
    2. 2<Code>ExpiredToken</Code>, or a URL that works for a short time and then stops long before its set expiryThe URL outlived the credentials that signed itFix
    3. 3<Code>SignatureDoesNotMatch</Code>The request does not match what was signedFix

What does the error look like?

  • 403 (Forbidden)
  • AccessDenied
  • SignatureDoesNotMatch
  • ExpiredToken

Cause 1 of 3

The identity that signed the URL cannot do the operation

1Check

A presigned URL carries the permissions of whoever created it, so that IAM user or role needs the action (s3:PutObject for an upload), and the bucket policy must not explicitly deny it.

aws sts get-caller-identity
aws s3api get-bucket-policy --bucket my-bucket

2Fix

Allow the action on the object keys for the identity that signs, and remove or narrow any explicit deny.

{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Action": "s3:PutObject",
    "Resource": "arn:aws:s3:::my-bucket/uploads/*"
  }]
}

3Verify

Generate a new URL and repeat the curl upload; S3 answers 200.

curl -i -X PUT --upload-file ./test.txt "PASTE_NEW_URL"

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 2 of 3

The URL outlived the credentials that signed it

1Check

When temporary credentials sign the URL, it expires with them even if you asked for more. An assumed role session lasts one hour by default, and EC2 instance credentials rotate after about six hours at most.

aws sts get-caller-identity

2Fix

Create the URL just before you hand it out, with fresh credentials, and keep the lifetime shorter than the credentials' remaining time. For a long-lived URL, sign it with an IAM user's keys (valid up to 7 days with Signature Version 4).

aws s3 presign s3://my-bucket/uploads/report.pdf --expires-in 900

3Verify

Use the new URL within its lifetime and check the upload returns 200.

curl -i -X PUT --upload-file ./test.txt "PASTE_NEW_URL"

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 3 of 3

The request does not match what was signed

1Check

S3 compares the HTTP method, headers and query string with the ones used when the URL was created. A GET URL used with PUT, a changed header, a proxy that rewrites headers, or a drifting system clock all break the signature.

date -u
curl -i -X PUT --upload-file ./test.txt "PASTE_PRESIGNED_URL"

2Fix

Send the same method and the same headers that you signed, add nothing extra, and sync the clock with NTP. If a corporate proxy sits in between, test without it.

# signed for PUT: upload with PUT and no extra headers
curl -X PUT --upload-file ./test.txt "PASTE_PRESIGNED_URL"

3Verify

Run the curl upload again; S3 answers 200 with an empty body.

curl -i -X PUT --upload-file ./test.txt "PASTE_PRESIGNED_URL"

Confirmed by docs.aws.amazon.com

Did this fix it?

How do I stop it coming back?

  • Generate presigned URLs on demand, close to use, with a lifetime of minutes.
  • Give the signing role only the actions and key prefix it needs, and test uploads with curl before wiring the browser.
  • In the client, set only the headers that were part of the signed request.

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

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 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.