Skip to content

The request signature we calculated does not match the signature you provided. Check your key and signing method. I get this error only when i use lambda function. Their is no issue in local server

0

const { S3Client, DeleteObjectCommand, PutObjectCommand } = require("@aws-sdk/client-s3"); const dotenv = require('dotenv');

dotenv.config();

const bucketName = process.env.AWS_BUCKET_BLOG; const region = process.env.AWS_BUCKET_REGION; const accessKeyId = process.env.AWS_ACCESS_KEY; const secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY; const domain = process.env.AWS_CD_URL_BLOG;

const s3Client = new S3Client({ region, credentials: { accessKeyId, secretAccessKey }, signatureVersion: 'v4', });

function uploadPost(fileBuffer, fileName , mimetype) { const uploadParams = { Bucket: bucketName, Body: fileBuffer, Key: fileName, ContentType: mimetype, };

return s3Client.send(new PutObjectCommand(uploadParams));

}

function deletePost(fileName) { const deleteParams = { Bucket: bucketName, Key: fileName, };

return s3Client.send(new DeleteObjectCommand(deleteParams));

}

async function getObjectSignedUrlPost(key) { const url = domain + key;

return url;

}

module.exports = { uploadPost, deletePost, getObjectSignedUrlPost, };

asked 2 years ago4K views
1 Answer
3
Accepted Answer

Hi,

Using AWS_ACCESS_KEY and AWS_SECRET_ACCESS_KEY within a Lambda is not the right way to go: you will get credential mismatches like the one you get because the request that you make is done under the Lambda execution role.

So, the right way is to use only the Lambda execution role after granting it the right credentials for the services that you need.

See https://docs.aws.amazon.com/lambda/latest/dg/lambda-intro-execution-role.html

You provide an execution role when you create a function. When you invoke your function, 
Lambda automatically provides your function with temporary credentials by assuming this role. 
You don't have to call sts:AssumeRole in your function code.

Best,

Didier

EXPERT
answered 2 years ago
EXPERT
reviewed 2 years ago
EXPERT
reviewed 2 years ago
EXPERT
reviewed 2 years ago
  • It worked thank you

  • For deleting Object it works but when I upload Object it add broken image and also get the same.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.