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

v-upload-aws-s3

v0.1.0

Published

Pure JavaScript uploading to AWS S3

Downloads

4

Readme

Note on S3 user permissions

The user associated with the accessKey and secretKey you use must have the appropriate permissions assigned to them. My user's IAM policy looks like:

{
    "Version": "2020-03-21",
    "Statement": [
        {
            "Sid": "stmt20200321",
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:PutObjectVersionAcl"
            ],
            "Resource": [
                "arn:aws:s3:::my-bucket/uploads/*"
            ]
        }
    ]
}

Example



async function uploadToS3(){
  const file = {
    // `uri` can also be a file system path (i.e. file://)
    uri: "assets-library://asset/asset.PNG?id=655DBE66-8008-459C-9358-914E1FB532DD&ext=PNG",
    name: "image.png",
    type: "image/png"
  }

  const options = {
    keyPrefix: "uploads/",
    bucket: "your-bucket",
    region: "us-east-1",
    accessKey: "your-access-key",
    secretKey: "your-secret-key",
    successActionStatus: 201
  }

  try{
    const response = await RNS3.put(file, options)
    if (response.status === 201){
      console.log("Success: ", response.body)
      /**
       * {
       *   postResponse: {
       *     bucket: "your-bucket",
       *     etag : "9f620878e06d28774406017480a59fd4",
       *     key: "uploads/image.png",
       *     location: "https://your-bucket.s3.amazonaws.com/uploads%2Fimage.png"
       *   }
       * }
       */
    } else {
      console.log("Failed to upload image to S3: ", response)
    }
  } catch(error){
    console.log(error)
  }
}

Usage

put(file, options)

Upload a file to S3.

Arguments:

  1. file
  • uri required - File system URI, can be assets library path or file:// path
  • name required - The name of the file, will be stored as such in S3
  • type required - The mime type, also used for Content-Type parameter in the S3 post policy
  1. options
  • acl - The Access Control List of this object. Defaults to public-read
  • keyPrefix - Prefix, or path to the file on S3, i.e. uploads/ (note the trailing slash)
  • bucket required - Your S3 bucket
  • region required - The region of your S3 bucket
  • accessKey required - Your S3 AWSAccessKeyId
  • secretKey required - Your S3 AWSSecretKey
  • successActionStatus - HTTP response status if successful, defaults to 201
  • awsUrl - AWS S3 url. Defaults to s3.amazonaws.com
  • timeDelta - Devices time offset from world clock in milliseconds, defaults to 0

Returns an object that wraps an XMLHttpRequest instance and behaves like a promise, with the following additional methods:

  • progress - accepts a callback which will be called with an event representing the progress of the upload. Event object is of shape
    • loaded - amount uploaded
    • total - total amount to upload
    • percent - number between 0 and 1 representing the percent completed
  • abort - aborts the xhr instance

Examples:

RNS3.put(file, options)
  .progress((e) => console.log(e.loaded / e.total)); // or console.log(e.percent)

RNS3.put(file, option)
  .abort();