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

dist-to-s3

v0.0.7

Published

upload static assets to aws s3

Downloads

2

Readme

Dist to s3

NPM

npm package NPM downloads Average time to resolve an issue

Why

This is a simplified s3 client for uploading static assets, such as html, css, images, font file onto AWS s3 bucket. It's pretty easy to write the same script rely on the standard docs of AWS, but it's necessary for you to consider following items:

  1. How to list all files in the folder(usually 'dist' or 'build') using nodejs?
  2. How to decide the mime types of each file?
  3. What value is reasonable for header 'Cache Control'?
  4. How to make it work behind a proxy in your local network(Usually in office )?

I spent several hours to package all above into this package and expose some of custom configurations out. e.g.

s3

s3.config({
  proxy: 'http://proxy.com:8080',
  name: bucketName,
  region: bucketRegion,
  key: accessKeyId,
  secret: accessSecret
})

upload options:

s3
  .upload(path.join(__dirname, 'dist'), {basePath: path.join(__dirname, 'dist')})
  .catch(err => {
    console.error(err)
  })

basePath is the relative path to the file while caculating the s3 key of the file. e.g. when:

  • file: ./dist/stylesheets/app.css,
  • basePath: ./dist Then the key of the file (What) will be /stylesheets/app.js. By default the basePath is same with the folder(the first argument of the upload function).

Get started

npm install dist-to-s3
yarn add dist-to-s3

Make it as a task of gulp.

gulpfile.js

require('dotenv').config()

const path = require('path')
const s3 = require('dist-to-s3')

if (!(process.env.S3_BUCKET_NAME && process.env.S3_BUCKET_REGION && process.env.AWS_ACCESS_KEY && process.env.AWS_ACCESS_SECRET)) {
  console.error('Please make sure env variables [S3_BUCKET_NAME, S3_BUCKET_REGION, AWS_ACCESS_KEY, AWS_ACCESS_SECRET] are provided correctly and then try again')
  process.exit(1)
}

const bucketName = process.env.S3_BUCKET_NAME;
const bucketRegion = process.env.S3_BUCKET_REGION;
const accessKeyId = process.env.AWS_ACCESS_KEY;
const accessSecret = process.env.AWS_ACCESS_SECRET;

s3.config({
  proxy: 'http://proxy.com:8080',
  name: bucketName,
  region: bucketRegion,
  key: accessKeyId,
  secret: accessSecret
})

function deploy() {
  return s3
  .upload(path.join(__dirname, 'dist'))
  .catch(err => {
    console.error(err)
  })
}

exports.deploy = deploy

What is key of file

Let's make it clear with such a sample.

  1. The url you visited s3 files is https://myhost.com
  2. The file(app.css) you upload to s3 with key(/stylesheets/app.css)

Then you are able to visit app.css with opening url https://myhost.com/stylesheets/app.css in browser.

LICENSE

MIT