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

lazy-s3

v1.0.2

Published

a high-level abstraction of s3 functions for lazy programmers

Downloads

8

Readme

Lazy S3

Build Status Coverage Status

Do you hate writing the same S3 operations multiple times like me? Well, let me save you about 15 minutes and give you some high level abstraction of common S3 operations.

  • Get an object from S3
  • Download an object from S3
  • Get a pre-signed URL from S3
  • Delete an object for S3.
  • Generate a policy document for browser uploads.

Advantages

  • AWS Documentation can take forever to come through and figure out the arguments.
  • Sets sensisble defaults.
  • Makes you set a content-type.
  • Error catching for missed params.
  • Returns promises

Usage

First, you must make sure the following environment variables are set.

  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY
  • AWS_BUCKET

Setting a bucket is optional. If you do set a bucket either through the options object or set it as an environment variable, it will act as the default bucket for all methods. This means that you don't need to pass a bucket argument to any of the methods when set. However, if you DO pass in a bucket as a parameter to a method, the argument will be used over the default bucket.

Lazy S3 exposes a class, S3, that takes a optional object of options. You can use Foreman to load in a .env file containing the necessary options if you wish to not bother with setting the options in a object.


// constructor
let s3 = new S3('my-default-bucket'); // or process.env.AWS_BUCKET.   Setting either of these is essentially setting a default bucket.

// read a file and upload the contents to s3.
fs.readFile('input.png', (err, data) => {
   if (err) {
      return console.error(err);
   }
   // File as buffer, key, content-type, bucket.  Bucket is optional if default bucket has been specified.
   // Returns promise.
   s3.upload(data, 'content/input.png', 'image/png', 'my-bucket')
    .then((res) => console.log(res)) // res is the returned metadata from s3 detailing the created object.
    .catch((err) => console.log(err));
});

// download data from S3 as a buffer and write to file file.
s3.download('content/output.png', 'my-bucket') // Key and Bucket.  Like the above, bucket is optional.
    .then((buffer) => {
        fs.writefile('output.png', buffer, (err) => {
        if(err) {
        // handle error...
        }
        });
    });

    // Get a presigned url that lasts for x amount of minutes.   Use this method to let users dowload private documents from your buckets.   Defaults to 20 minutes if the 2nd argument is omitted or null.
    s3.getSignedUrl('mygif.gif', 20, 'my-bucket')
        .then((url) => console.log(url.url))
        .catch((err) => console.log(err));


        // Create a s3 polixy to be used with browser uploads
    s3.createS3Policy('application/pdf', 'private', 'my-bucket')
        .then((policy) => console.log(policy))  // returns object of policy information.
        .catch((err) => console.log(err));

Documentation