Last active
August 2, 2022 20:35
-
-
Save emtudo/f0e0771a4b07ce65dd9cffc5938b568c to your computer and use it in GitHub Desktop.
read sqs and save to file
This file contains 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 { SQSClient, ReceiveMessageCommand } from "@aws-sdk/client-sqs"; | |
import { join } from 'node:path' | |
import { fileURLToPath } from 'node:url' | |
import { writeFile } from 'node:fs/promises' | |
const QueueUrl = "" | |
const client = new SQSClient({ region: "sa-east-1" }); | |
const TARGET_DIR = join(fileURLToPath(new URL('.', import.meta.url)), 'data') | |
const command = new ReceiveMessageCommand({ | |
QueueUrl, | |
MaxNumberOfMessages: 10, | |
WaitTimeSeconds: 5, | |
}); | |
while (true) { | |
const { Messages } = await client.send(command); | |
if (!Messages || Messages.length == 0) { | |
console.warn('no more messages') | |
break | |
} | |
console.log(`load new ${Messages.length} messages`) | |
const promises = Messages.map(msg => { | |
return writeFile(join(TARGET_DIR, `${msg.MessageId}.json`), JSON.stringify(msg, null, 2)) | |
}) | |
await Promise.allSettled(promises) | |
.then(results => { | |
const errors = results.filter(row => row.status === 'rejected') | |
if (errors.length > 0) { | |
errors.forEach(err => { | |
console.error(err) | |
}) | |
} | |
}) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment