最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

amazon web services - AWS Version 4 signature snippet code with example in javascript - Stack Overflow

programmeradmin1浏览0评论

I have AWS Rest API and need to generate AWS Version 4 Signature.
I searching and get many links.
Successfully generate signature but getting error like "The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.".

I have AWS Rest API and need to generate AWS Version 4 Signature.
I searching and get many links.
Successfully generate signature but getting error like "The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.".

Share Improve this question asked Apr 30, 2018 at 7:23 Milan HirapraMilan Hirapra 311 gold badge1 silver badge6 bronze badges 2
  • Hi Milan, wele to stackoverflow. We'll need a bit more information, have you written your own code to pute the signature or are you using a library? If you're using a library, which one? – brabster Commented Apr 30, 2018 at 7:25
  • I get idea from gist.github./davidkelley/c1274cffdc0d9d782d7e – Milan Hirapra Commented Apr 30, 2018 at 7:34
Add a ment  | 

2 Answers 2

Reset to default 4

Generating an AWS v4 signature is plicated. If you're looking to learn about the process and implement it yourself, I remend the AWS documentation. There's a step-by-step walkthrough of the process, examples (including one in Javascript) and test suite. It's a good exercise to do if you're learning!

Trying to follow someone else's example without the information AWS provide is going to be tough and I don't think you'll understand as much.

If you just want to sign stuff, I'd suggest using a library rather than writing your own. AWS provide a library to use their services in the browser, and if you just want to sign requests, something like aws4 is a lightweight way to do that.

In 2024 this is now feasible using the aws-sigv4-fetch NPM package, a wrapper around the @smithy/signature-v4 package. See the snippet below. Refer to the aws-sigv4-fetch official docs for more info:

import { createSignedFetcher } from 'aws-sigv4-fetch';

const signedFetch = createSignedFetcher({
    service: 'lambda', // depends on what you want to access
    region: 'us-east-1',
    credentials: {
        accessKeyId: [YOUR_AWS_ACCESS_KEY],
        secretAccessKey: [YOUR_AWS_SECRET_KEY]
    }
});

const response = await signedFetch('https://your_aws_service/', {
    method: 'post',
    body: JSON.stringify(requestData),
    headers: { 'Content-Type': 'application/json' }
});

console.log(response.status);
console.log(await response.json());
console.log(await response.arrayBuffer());
发布评论

评论列表(0)

  1. 暂无评论