npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@therocketcodemx/library-manager-documents

v1.1.30

Published

A library of common functions for managing documents with Amazon S3

Downloads

71

Readme

library-manager-documents

:rocket: Table of Contents :rocket:

Install

This is a Node.js module available through the npm registry.

Before installing, download and install Node.js. Node.js 0.6 or higher is required.

Installation is done using the npm install command:

$ npm install @therocketcodemx/library-manager-documents

Introduction

This library provides a simple way to manage documents, images, and videos stored in an Amazon Web Services (AWS) S3 bucket. It includes methods for uploading, downloading, deleting, and replacing files, as well as file type and size validation during upload.

Here is an example of how to initialize the module:

import { ManagerDocuments } from '@therocketcodemx/library-manager-documents';
// configuration object to connect to S3
const configS3 = {
  accessKeyId: "your-access-key-id",
  secretAccessKey: "your-secret-access-key",
  region: "your-region",
  bucket: "your-bucket",
  signatureVersion: "v4", // Optional
};
// The library uses the following default file types and sizes for validation during upload
const defaultFileTypesAndSizes = {
  document: {
    maxSize: 5000000, // in bytes
    allowedTypes: [".pdf", ".docx", ".xlsx", ".pptx", ".odt"],
  },
  image: {
    maxSize: 5000000, // in bytes
    allowedTypes: [".png", ".jpg", ".jpeg", ".gif"],
  },
  video: {
    maxSize: 20000000, // in bytes
    allowedTypes: [".mp4", ".avi", ".mov", ".wmv"],
  },
};
const manager = new ManagerDocuments(configS3, defaultFileTypesAndSizes);

...

You can provide an optional customFileTypesAndSizes object to the ManagerDocuments constructor to override the default file types and sizes used for validation during upload. The customFileTypesAndSizes object should have the same structure as the IFileTypeAndSize interface defined in the library.

Interfaces used

// The configuration object for AWS S3
interface IConfigS3 {
  accessKeyId: string; // Your AWS access key ID
  secretAccessKey: string; // Your AWS secret access key
  region: string; // The AWS region where your bucket is hosted
  bucket: string; // The name of your AWS S3 bucket
  signatureVersion?: string; // (optional) The AWS signature version you want to use. If not provided, the library will use 'v4' by default.
}
  // Enum defining the file types
enum FileType {
  DOCUMENT = "document",
  IMAGE = "image",
  VIDEO = "video",
  }
// Object containing max file sizes and allowed file types for each file type
interface IFileTypeAndSize {
  [FileType.DOCUMENT]?: {
    maxSize: number; // The maximum size, in bytes, of a document file that can be uploaded
    allowedTypes: string[]; // The file extensions that are accepted for document files
  };
  [FileType.IMAGE]?: {
    maxSize: number; // The maximum size, in bytes, of an image file that can be uploaded
    allowedTypes: string[]; // The file extensions that are accepted for image files
  };
  [FileType.VIDEO]?: {
    maxSize: number; // The maximum size, in bytes, of a video file that can be uploaded
    allowedTypes: string[]; // The file extensions that are accepted for video files
  };
}

Using methods

For use the methods the instance object is used of ManagerDocuments, for example:

uploadDocument

This method uploads a file to the S3 bucket. It takes a file buffer and a text string with the file name as parameters. If the file is invalid (for example, if it is too large or if its file type is not allowed), the method will throw an error.

// Example usage
const fileBuffer = Buffer.from('This is a file');
const fileName = 'location/for/the/file/my-file.pdf'; // This will be taken as the Key for the document

try {
  // The method returns a promise that resolves to the URL of the uploaded file
  const result = await manager.uploadDocument(fileBuffer, fileName);
  console.log(result); // "https://your-bucket.s3.your-region.amazonaws.com/location/of/the/file/my-file.pdf"
} catch (error) {
  console.error(error); // Handle the error
}

getSignedDownloadUrl

This method generates a presigned URL that allows access to a private S3 object. It takes a text string with the object key and an optional number specifying the time in seconds until the URL expires (default is 86400 seconds or 24 hours).

// Example usage
const key = 'location/of/the/file/my-file.pdf';
const expiresIn = 3600; // URL expires in 1 hour

const url = manager.getSignedDownloadUrl(key, expiresIn);
console.log(url); // "https://your-bucket.s3.your-region.amazonaws.com/location/of/the/file/my-file.pdf?token"

replaceDocument

This method is used to replace an existing file in the S3 bucket. It takes a buffer of the new file and a text string with the name of the file to be replaced as parameters. If the new file is invalid (for example, if it is too large or if its file type is not allowed), the method will throw an error.

// Example usage
const fileBuffer = Buffer.from('This is a new file');
const oldFileName = 'my-file.pdf'; // This will be taken as the Key for the document

try {
  // The method returns a promise that resolves to the URL of the replaced file
  const result = await manager.replaceDocument(fileBuffer, fileName);
  console.log(result);
} catch (error) {
  console.error(error);
}

deleteDocument

This method is used to delete a file by receiving as parameters: the name of the file to be deleted.

/ Example usage
const fileNameToDelete = 'location/for/the/file/my-file.pdf'; // This will be taken as the Key for the document

try {
  const result = await manager.deleteDocument(fileNameToDelete);
  console.log(result);
} catch (error) {
  console.error(error);
}