Skip to content

Instantly share code, notes, and snippets.

@Sathiyapramod
Forked from SalvoCozzubo/getObject.js
Created May 8, 2024 06:52
Show Gist options
  • Save Sathiyapramod/f8d8a37cbbdfe84e43765ffc5f9422e0 to your computer and use it in GitHub Desktop.
Save Sathiyapramod/f8d8a37cbbdfe84e43765ffc5f9422e0 to your computer and use it in GitHub Desktop.
GetObject Example
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