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

s3-files-handler

v0.0.5

Published

A simple DynamoDB handler for Node.js

Downloads

5

Readme

s3-files-handler

The s3-files-handler library is a TypeScript class that provides functionality to interact with Amazon S3 using the AWS SDK for JavaScript.

Methods

The library provides the following methods:

setClient(): S3Client

Sets up and returns an S3 client based on the configuration provided in the environment variables or using default configuration.

uploadFile(bucket: string, file: IFile): Promise

Uploads a file to the specified bucket in Amazon S3 using the PutObjectCommand. Returns a Promise that resolves to the upload response.

streamToString(stream: any): Promise

Converts a readable stream from an S3 object retrieval into a string. Returns a Promise that resolves to the string representation of the stream.

getFiles(bucket: string): Promise<IGetFiles[]>

Retrieves a list of files from the specified bucket in Amazon S3 using the ListObjectsCommand and GetObjectCommand. Returns a Promise that resolves to an array of files with their key and body contents.

getUrl(args: IGetUrl): Promise

Generates a pre-signed URL for accessing an object in the specified bucket with the provided route, bucket, and MIME type. Returns a Promise that resolves to the pre-signed URL.

signUrl(args: ISignUrl): Promise

Generates a pre-signed URL for accessing an object based on the provided expiration time and object information. Returns a Promise that resolves to the pre-signed URL.

listObjects(bucket: string, key: string): Promise

Lists objects in the specified bucket with the given key prefix using the ListObjectsV2Command. Returns a Promise that resolves to the output of the list objects operation.

Usage/Examples

import S3Provider from "s3-files-handler";

// Create an instance of the S3Provider class
const s3Provider = new S3Provider();

// Use async/await to interact with Amazon S3

async function example() {
  // Set the S3 client
  const client = await s3Provider.setClient();

  // Use the provided methods to interact with Amazon S3

  // Upload a file to Amazon S3
  const bucket = "YourBucketName";
  const file = {
    fileName: "example.txt",
    body: "This is the content of the file in base 64."
  };

  try {
    const response = await s3Provider.uploadFile(bucket, file);
    console.log("File uploaded:", response);
  } catch (error) {
    console.error("Error uploading file:", error);
  }

  // Get a list of files from Amazon S3
  try {
    const files = await s3Provider.getFiles(bucket);
    console.log("Files retrieved:", files);
  } catch (error) {
    console.error("Error retrieving files:", error);
  }

  // Generate a pre-signed URL for accessing an object
  const getUrlParams = {
    route: "example.txt",
    bucket: bucket,
    mimeType: "text/plain",
    expiration: 3600 // Optional, in seconds
  };

  try {
    const url = await s3Provider.getUrl(getUrlParams);
    console.log("Pre-signed URL:", url);
  } catch (error) {
    console.error("Error generating pre-signed URL:", error);
  }

  // Generate a pre-signed URL using object information
  const getObjectParams = {
    Key: "mydocuments/example.txt",
    Bucket: bucket,
  };

  const signUrlParams = {
    object: new GetObjectCommand(getObjectParams),
    expiration: 3600
  };

  try {
    const url = await s3Provider.signUrl(signUrlParams);
    console.log("Pre-signed URL:", url);
  } catch (error) {
    console.error("Error generating pre-signed URL:", error);
  }

  // List objects in a bucket with a specific key prefix
  const keyPrefix = "folder/subfolder/";

  try {
    const output = await s3Provider.listObjects(bucket, keyPrefix);
    console.log("List objects output:", output);
  } catch (error) {
    console.error("Error listing objects:", error);
  }
}

example();


Contribution

Contributions are welcome! If you want to improve this package or report any issues, please follow these steps:

1. Fork the repository.

2. Create a new branch

(git checkout -b feature/feature-name).

3. Make the necessary changes

and commit them (git commit -am 'Add new feature').

4. Push your changes

to the remote repository (git push origin feature/feature-name). Open a pull request on GitHub.

License

This project is licensed under the MIT License. See the LICENSE. file for more details.