Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dist-to-s3

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dist-to-s3

upload static assets to aws s3

  • 0.0.6
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
increased by100%
Maintainers
1
Weekly downloads
 
Created
Source

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

FAQs

Package last updated on 09 May 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc