2.1.5 • Published 2 years ago

s3-spa-upload v2.1.5

Weekly downloads
583
License
ISC
Repository
github
Last release
2 years ago

S3 SPA Upload

Upload a Single Page Application (React, Angular, Vue, ...) to S3 with the right content-type and cache-control meta-data.

This module uploads the local SPA's build directory to S3, overwriting what's currently on S3.

index.html files are uploaded last, referenced JS/CSS files are uploaded first: so, users will only ever download index.html files that work, even during deployments.

Note: There's no intelligence (yet) to only upload changed files. There's also no intelligence (yet) to split big files in chunks and do multipart upload.

Build Status

This module requires the following AWS S3 permissions (see sample CloudFormation policy template below):

  • s3:PutObject on objects in your bucket
  • s3:ListBucket on your bucket (only needed when using --delete option)
  • s3:DeleteObject on objects in your bucket (only needed when using --delete option)

Installation

To install globally (for CLI usage):

npm install -g s3-spa-upload

Command Line Usage

Basic usage:

s3-spa-upload dist-dir my-s3-bucket-name

Clean-up old files

To also clean up old files, use the --delete option. This will delete all files in the bucket that are not included in the current upload (limited to the supplied prefix, see below):

s3-spa-upload dist-dir my-s3-bucket-name --delete

Custom cache-control mapping

You can provide your desired cache-control mapping in a json file that contains a mapping from glob patterns to cache-control headers:

{
    "index.html": "no-cache",
    "*.js": "public,max-age=31536000,immutable"
}

Suppose your mapping file is called cache-control.json:

s3-spa-upload dist-dir my-s3-bucket-name --cache-control-mapping cache-control.json

If you don't provide a custom mapping, the default will be used, which should be okay for most SPA's, see below.

Upload to a prefix

By default the SPA will be uploaded to the root of your S3 bucket. If you don't want this, specify the prefix to use:

s3-spa-upload dist-dir my-s3-bucket-name --prefix mobile

Note that when used in conjunction with --delete, this means only old files matching that same prefix will be deleted.

Programmatic Usage

import s3SpaUpload from "s3-spa-upload";
// const s3SpaUpload = require('s3-spa-upload')

s3SpaUpload("dir", "bucket").catch(console.error);

// Can supply options:
const options = {
  delete: true,
  prefix: "mobile",
  cacheControlMapping: {
    "index.html": "no-cache",
    "*.js": "public,max-age=31536000,immutable",
  },
  concurrency: 100, // max nr of files to upload to S3 in parallel
  awsCredentials: {
    accessKeyId: "...",
    secretAccessKey: "...",
    sessionToken: "...",
  }, // Optional. If not provided explicitly, the AWS SDK will source credentials as usual
};
s3SpaUpload("dir", "bucket", options).catch(console.error);

Default Cache-Control settings

File/extCache settingDescription
index.htmlpublic,max-age=60,stale-while-revalidate=25920001 minute, but allow stale content for 30 days, provided a cache refresh request is made also
csspublic,max-age=31536000,immutableAs long as possible
jspublic,max-age=31536000,immutableAs long as possible
pngpublic,max-age=86400,stale-while-revalidate=2592000One day, but allow stale content for 30 days, provided a cache refresh request is made also
icopublic,max-age=86400,stale-while-revalidate=2592000One day, but allow stale content for 30 days, provided a cache refresh request is made also
txtpublic,max-age=86400,stale-while-revalidate=2592000One day, but allow stale content for 30 days, provided a cache refresh request is made also

Content-Type settings

Based on file extensions using https://www.npmjs.com/package/mime-types

AWS Policy Template

This CloudFormation IAM Policy template grants the needed permissions:

- Version: "2012-10-17"
    Statement:
      - Effect: Allow # This effect is only needed when using the --delete option
          Action: s3:ListBucket
          Resource: arn:aws:s3:::your-bucket-name
      - Effect: Allow
          Action:
            - s3:DeleteObject # This action is only needed when using the --delete option
            - s3:PutObject
          Resource: arn:aws:s3:::your-bucket-name/*
2.1.2

2 years ago

2.0.3

2 years ago

2.1.1

2 years ago

2.0.2

2 years ago

2.1.4

2 years ago

2.1.3

2 years ago

2.0.4

2 years ago

2.1.5

2 years ago

2.1.0

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.2.0

2 years ago

1.2.1

2 years ago

1.1.7

3 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.2.7

6 years ago

0.2.6

6 years ago

0.2.5

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago