UNPKG

nodebasecli

Version:

Cli to create modules for nodebase

91 lines (81 loc) 3.24 kB
const fileData = `/** * @author "Abdul Quadir Dewaswala" * @license MIT * @version 1.0 * This module provides functions to interact with AWS S3. * It uses the AWS SDK for JavaScript in Node.js to interact with S3. * The functions are: * uploadFileToS3: uploads a file to an S3 bucket. * downloadFileFromS3: downloads a file from an S3 bucket. * deleteFileFromS3: deletes a file from an S3 bucket. * The functions are asynchronous and return promises. * If an error occurs, the promise is rejected with the error. */ import S3 from 'aws-sdk/clients/s3'; // Create an S3 client with the region and credentials // specified in the environment variables. const s3Client = new S3({ region: process.env.AWS_REGION, // AWS region // Optionally, you can specify accessKeyId and secretAccessKey // credentials: { // accessKeyId: process.env.AWS_ACCESS_KEY_ID, // secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY // }, apiVersion: '2006-03-01', // S3 API version }); /** * Uploads a file to an S3 bucket. * @param {Object} file - The file object containing Key and Body. * @param {string} bucketName - The name of the S3 bucket. * @return {Promise} - A promise that resolves if the file is uploaded successfully. */ const uploadFileToS3 = async (file, bucketName) => { const { Key, Body } = file; // File parameters const params = { Bucket: bucketName, // S3 bucket name Key, // File key (name) Body // File body (content) }; // Use the S3 client to upload the file // The promise is returned, so the caller can handle the result return s3Client.putObject(params).promise().catch(err => console.log(err)); }; /** * Downloads a file from an S3 bucket. * @param {Object} file - The file object containing Key. * @param {string} bucketName - The name of the S3 bucket. * @return {Promise} - A promise that resolves if the file is downloaded successfully. */ const downloadFileFromS3 = async (file, bucketName) => { const { Key } = file; // File key const params = { Bucket: bucketName, // S3 bucket name Key // File key (name) }; // Use the S3 client to download the file // The promise is returned, so the caller can handle the result return s3Client.getObject(params).promise().catch(err => console.log(err)); }; /** * Deletes a file from an S3 bucket. * @param {Object} file - The file object containing Key. * @param {string} bucketName - The name of the S3 bucket. * @return {Promise} - A promise that resolves if the file is deleted successfully. */ const deleteFileFromS3 = async (file, bucketName) => { const { Key } = file; // File key const params = { Bucket: bucketName, // S3 bucket name Key // File key (name) }; // Use the S3 client to delete the file // The promise is returned, so the caller can handle the result return s3Client.deleteObject(params).promise().catch(err => console.log(err)); }; // Export the functions for external use export { uploadFileToS3, downloadFileFromS3, deleteFileFromS3 };` module.exports = fileData