Skip to content

Instantly share code, notes, and snippets.

@karolba
Last active May 4, 2024 19:52
Show Gist options
  • Save karolba/c8c2c20c9e5558a459b75c3594d8e16b to your computer and use it in GitHub Desktop.
Save karolba/c8c2c20c9e5558a459b75c3594d8e16b to your computer and use it in GitHub Desktop.
Scan a dynamodb table with aws4-tiny
const credentials = {
accessKeyId: "AKIA...",
secretAccessKey: "...",
}
const region = 'eu-west-1'
async function dynamoDB(target, body) {
const response = await aws4.fetch({
service: 'dynamodb',
region: region,
path: '/',
headers: {
'X-Amz-Target': target,
'Content-Type': 'application/x-amz-json-1.0'
},
body: JSON.stringify(body)
}, credentials)
if (!response.ok) {
throw response
}
return await response.json()
}
// TODO: Use parallel scans if need to handle multi-MB responses
async function scanDynamoDB(scanOptions) {
let items = [];
let response = {};
do {
response = await dynamoDB('DynamoDB_20120810.Scan', {
...scanOptions,
...(response.LastEvaluatedKey && { ExclusiveStartKey: response.LastEvaluatedKey })
});
items.push(...response.Items);
} while (response.LastEvaluatedKey);
return items;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment