Skip to content

Instantly share code, notes, and snippets.

@valstu
Created April 5, 2020 12:01
Show Gist options
  • Save valstu/92b2268c5ff968b6979a3f146d4a8634 to your computer and use it in GitHub Desktop.
Save valstu/92b2268c5ff968b6979a3f146d4a8634 to your computer and use it in GitHub Desktop.
Deploy script to publish react app to s3
const AWS = require('aws-sdk');
const fs = require('fs');
const glob = require('glob');
const mimeTypes = require('mime-types');
require('dotenv').config();
AWS.config = new AWS.Config({
credentials: new AWS.Credentials({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
}),
region: process.env.AWS_DEFAULT_REGION
});
const s3 = new AWS.S3();
glob('./build/**/*', {}, (err, files) => {
files.forEach(file => {
if (!fs.lstatSync(file).isDirectory()) {
const fileContents = fs.readFileSync(`./${file}`);
const fileMime = mimeTypes.lookup(file);
s3.upload(
{
Bucket: 'cra-s3-sample',
Key: file.replace('./build/', ''),
Body: fileContents,
ContentType: fileMime
},
{partSize: 10 * 1024 * 1024, queueSize: 1},
(err, data) => {
if (err) {
console.log(err)
throw new Error(err.message);
}
}
);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment