Last active
May 4, 2024 19:52
-
-
Save karolba/c8c2c20c9e5558a459b75c3594d8e16b to your computer and use it in GitHub Desktop.
Scan a dynamodb table with aws4-tiny
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
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