-
-
Save Sathiyapramod/f8d8a37cbbdfe84e43765ffc5f9422e0 to your computer and use it in GitHub Desktop.
GetObject Example
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
const { S3Client, GetObjectCommand } = require('@aws-sdk/client-s3'); | |
const client = new S3Client({}); | |
const streamToString = (stream) => new Promise((resolve, reject) => { | |
const chunks = []; | |
stream.on('data', (chunk) => chunks.push(chunk)); | |
stream.on('error', reject); | |
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8'))); | |
}); | |
const readFile = async (bucket, key) => { | |
const params = { | |
Bucket: bucket, | |
Key: key, | |
}; | |
const command = new GetObjectCommand(params); | |
const response = await client.send(command); | |
const { Body } = response; | |
return streamToString(Body); | |
}; | |
module.exports.index = async (event) => { | |
const { BUCKET_NAME } = process.env; | |
const { file } = event; | |
return readFile(BUCKET_NAME, file); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment