S3 Api SignatureDoesNotMatch

Writing a script that needs to upload some files to an R2 bucket at build time (so really this is just running as normal nodejs). Trying to use the S3 API. Code is really simple, basically straight from the examples:
import {
S3Client,
ListBucketsCommand,
ListObjectsV2Command,
GetObjectCommand,
PutObjectCommand,
} from "@aws-sdk/client-s3";
import { createHash } from "node:crypto";

const hashedSecretKey = createHash('sha256').update(R2_S3_SECRET).digest('hex');

const S3 = new S3Client({
region: "auto",
endpoint: R2_S3_ENDPOINT,
credentials: {
accessKeyId: R2_S3_CLIENT,
secretAccessKey: hashedSecretKey,
},
});

console.log(await S3.send(new ListObjectsV2Command ({
Bucket: R2_BUCKET_NAME,
})));
import {
S3Client,
ListBucketsCommand,
ListObjectsV2Command,
GetObjectCommand,
PutObjectCommand,
} from "@aws-sdk/client-s3";
import { createHash } from "node:crypto";

const hashedSecretKey = createHash('sha256').update(R2_S3_SECRET).digest('hex');

const S3 = new S3Client({
region: "auto",
endpoint: R2_S3_ENDPOINT,
credentials: {
accessKeyId: R2_S3_CLIENT,
secretAccessKey: hashedSecretKey,
},
});

console.log(await S3.send(new ListObjectsV2Command ({
Bucket: R2_BUCKET_NAME,
})));
Getting the error:
SignatureDoesNotMatch: The request signature we calculated does not match the signature you provided. Check your secret access key and signing method.
SignatureDoesNotMatch: The request signature we calculated does not match the signature you provided. Check your secret access key and signing method.
Running this from my local machine. I've tried using a custom public domain. CORS is set up, but this is not a CORS issue. Public Access is enabled FWIW.
1 Reply
CleverPatrick
CleverPatrickOP3mo ago
Solution was to not hash the secret!

Did you find this page helpful?