Created
April 5, 2020 12:01
-
-
Save valstu/92b2268c5ff968b6979a3f146d4a8634 to your computer and use it in GitHub Desktop.
Deploy script to publish react app to s3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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