@pivotpath/aws-utilities
v1.0.9
Published
Utilities to connect with AWS services from frontend with ease.
Readme
AWS Utility Client
Overview
This is a Node.js package designed to simplify interactions with AWS S3 for uploading and downloading files using pre-signed URLs, as well as sending emails via a backend API. It wraps common AWS S3 operations and mailing functionality into a convenient utility class.
Installation
To install the package in your project, run:
npm install @nevitonnpm/aws-utilitiesAs this is a private package you need to configure .npmrc file on your local system for having this package while doing development.
.npmrc should contain following line
//registry.npmjs.org/:_authToken=<auth-token-to-access-private-packages-from-neviton>Here you will find more details on configuring .npmrc file on local. Link - Official documentation for .npmrc
To use this package in CI/CD workflow, Please follow This link of official documentation.
For Auth Token please sendout a mail on [email protected]. You will recieve a mail with auth token to replace it in your .npmrc file shortly.
Please don't ever use auth token directly in .npmrc files in your projects. either you can set it as env veriable or create global .npmrc on system to manage it.
Usage
1. Import the AWSUtilities class
import AWSUtilities from "@nevitonnpm/aws-utilities";2. Initialize the AWSUtilities class
To interact with the API, initialize the class with your AWS Utilities APP Config added in AWS Utilities repository & create a AWSUtilities client using which you will be able to connect with service by calling required methods easily.
const appName = "xyz";
const appId = "xyz_id";
const appSecret = "xyz_secrete";
const appSecreteKey = "xyz_secrete_key";
const env = "DEV";
const appUrl = 'https://example.com/'
const awsUtilities = new AWSUtilities({
appName,
appId,
appSecrete,
appSecretKey,
env,
appUrl
});
default export awsUtilities;3. List all files in S3
You can list all files present in your AWS S3 bucket:
awsUtilities
.listAllFiles()
.then((files) => console.log(files))
.catch((err) => console.error(err));4. Get a pre-signed URL for downloading an S3 object
To get a pre-signed URL for downloading a specific object from S3:
const keys = ["file1.jpg", "file2.png"];
const expiresIn = 3600;
awsUtilities
.getS3FileUrl(keys, expiresIn)
.then((urls) => console.log(urls))
.catch((err) => console.error(err));5. Upload a file to S3
To upload a file to S3 using a pre-signed URL:
const fileData = /* some binary file data */;
const fileKey = 'uploads/myFile.jpg';
const expiresIn = 3600;
const fileType = 'application/pdf';
awsUtilities.uploadFileToS3(fileData, fileKey, expiresIn, fileType)
.then(response => console.log('File uploaded successfully'))
.catch(err => console.error('Upload failed', err));6. Send an email
To send an email through the API:
const emailData = {
to: "[email protected]",
subject: "Hello!",
html: "<h1>Test Email</h1>",
attachments: [],
cc: "[email protected]",
};
awsUtilities
.sendMailToUser(emailData)
.then((response) => console.log("Email sent successfully"))
.catch((err) => console.error("Failed to send email", err));API Methods
listAllFiles(): Lists all the objects in the S3 bucket.getS3FileUrl(keys, expiresIn): Returns pre-signed URLs for downloading S3 objects. Optionally, you can provideexpiresIn(in seconds).uploadFileToS3(fileData, key, expiresIn, fileType): Uploads a file to S3. You can specify the fileData, key (filename) and optionally provideexpiresInandfileType.sendMailToUser(data): Sends an email via the API, including attachments.
Error Handling
Each method returns a Promise. You can handle success and failure using .then() and .catch() or You can also use try & catch block.
License
This project is unlicensed & for internal purpose only.
