Skip to content

Instantly share code, notes, and snippets.

@minhoyooDEV
Created May 22, 2018 15:54
Show Gist options
  • Save minhoyooDEV/222ea05b55c4966a4fd7a4136742253c to your computer and use it in GitHub Desktop.
Save minhoyooDEV/222ea05b55c4966a4fd7a4136742253c to your computer and use it in GitHub Desktop.

Revisions

  1. minhoyooDEV created this gist May 22, 2018.
    53 changes: 53 additions & 0 deletions AWS_S3_UPLOAD_LOCAL_FILES.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    //https://gist.github.com/kethinov/6658166

    var AWS = require('aws-sdk'),
    fs = require('fs'),
    path = require('path');


    // For dev purposes only
    AWS.config.update({accessKeyId: '.', secretAccessKey: '..'});

    // Read in the file, convert it to base64, store to S3
    //https://gist.github.com/kethinov/6658166
    console.log('====read directory===');
    var walkSync = function (dir, filelist) {
    var fs = fs || require('fs'),
    files = fs.readdirSync(dir);
    filelist = filelist || [];
    files.forEach(function (file) {
    if (file !== 'node_modules' && file !== '.idea') {

    if (fs.statSync(dir + '/' + file).isDirectory()) {
    filelist = walkSync(dir + '/' + file, filelist);
    }
    else {
    filelist.push(`${dir}/${file}`);
    }
    }
    });
    return filelist;
    };
    var fileList = []
    walkSync(process.argv[2], fileList)
    console.log(fileList)

    fileList.forEach((file) => {
    fs.readFile(file, (err, data) => {
    if (err) {
    throw err;
    }
    var base64data = new Buffer(data, 'binary');
    console.log(file, base64data)
    var s3 = new AWS.S3();
    s3.putObject({
    Bucket: 'minho-bucket',
    Key: file.slice(2),
    Body: base64data,
    ACL: 'public-read'
    },function (resp) {
    console.log(arguments);
    console.log('Successfully uploaded package.');
    });
    })
    });