S3 returns 403 when you upload with a presigned URL
403 (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.
Fix it fast
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"2Match what you see, then jump to its fix
- 1
<Code>AccessDenied</Code>The identity that signed the URL cannot do the operationFix - 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
<Code>SignatureDoesNotMatch</Code>The request does not match what was signedFix
- 1
What does the error look like?
403 (Forbidden)AccessDeniedSignatureDoesNotMatchExpiredToken
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-bucket2Fix
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
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-identity2Fix
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 9003Verify
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
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
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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack 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 fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix