Last active
December 15, 2022 21:24
-
-
Save gbvanrenswoude/ddb11c3e89ca8fc731f651f192ec52b6 to your computer and use it in GitHub Desktop.
Make plain https requests to an OpenSearch domain using AWS Sigv4 signing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Sha256 } from '@aws-crypto/sha256-browser'; | |
import { defaultProvider } from '@aws-sdk/credential-provider-node'; | |
import { NodeHttpHandler } from '@aws-sdk/node-http-handler'; | |
import { HttpRequest } from '@aws-sdk/protocol-http'; | |
import { SignatureV4 } from '@aws-sdk/signature-v4'; | |
// import { region } from './http-agent'; use an aws region string | |
// import { getEnv } from './utils'; just calls process.env or returns default | |
export async function indexDocumentonAwsES(body, index, type, id, domain) { | |
const request = new HttpRequest({ | |
body: JSON.stringify(body), | |
headers: { | |
'Content-Type': 'application/json', | |
host: domain | |
}, | |
hostname: domain, | |
method: 'PUT', | |
path: `${index}/${type}/${id}` | |
}); | |
const signer = new SignatureV4({ | |
credentials: defaultProvider(), | |
region, | |
service: 'es', | |
sha256: Sha256 | |
}); | |
const signedRequest = await signer.sign(request); | |
const client = new NodeHttpHandler(); | |
// @ts-ignore | |
const { response } = await client.handle(signedRequest); | |
console.log(`${response.statusCode} ${response.body.statusMessage}`); | |
let responseBody = ''; | |
await new Promise(() => { | |
response.body.on('data', chunk => { | |
responseBody += chunk; | |
}); | |
response.body.on('end', () => { | |
console.log('Response body: ' + responseBody); | |
}); | |
}).catch(error => { | |
console.log(`Error: ${error}`); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment