Skip to content

Instantly share code, notes, and snippets.

@kyohei8
Last active August 12, 2018 10:19
Show Gist options
  • Save kyohei8/885478002df6082d690aa2deed150a0b to your computer and use it in GitHub Desktop.
Save kyohei8/885478002df6082d690aa2deed150a0b to your computer and use it in GitHub Desktop.
upload from local to AWS_S3 (with gzip!)
{
"accessKeyId": "YOUR_AWS_ACCESSKEYID",
"secretAccessKey": "YOUR_AWS_SECRETACCESSKEY"
}
const fs = require('fs');
const path = require('path');
const async = require('async');
const zlib = require('zlib');
const mime = require('mime-types');
const AWS = require('aws-sdk');
const { accessKeyId, secretAccessKey } = require('./AWS_KEYS.json');
const bucketName = 'YOUR_BUCKET_NAME';
const dirName = path.resolve(__dirname, '../dist');
AWS.config.update({ accessKeyId, secretAccessKey });
const s3 = new AWS.S3();
const files = fs.readdirSync(dirName);
async.map(files, (file, cb) => {
const filePath = path.join(dirName, file);
const _body = fs.readFileSync(filePath);
const ext = path.extname(filePath);
const options = {
Bucket: bucketName,
Key: file,
Body: fs.readFileSync(filePath),
ContentType: mime.lookup(filePath),
ContentLength: Buffer.byteLength(_body),
ACL: 'public-read'
};
// gzip化
options.ContentEncoding = 'gzip';
zlib.gzip(_body, (err, binary) => {
options.Body = binary;
options.ContentLength = Buffer.byteLength(binary);
s3.putObject(options, cb);
});
}, (err, res) => {
if (err) console.error('err', err);
console.log('Successfully uploaded package.');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment