import aws    from 'aws-sdk';
import fs     from 'fs';
import files  from '../files';
import config from '../../config';
import async  from 'async';

aws.config.loadFromPath('./aws-config.json');

let s3 = new aws.S3({httpOptions: {timeout: 5 * 60 * 1000}});

let concurrency = 3;

let s3lib = {

    queue: async.queue((req, cb) => {
        s3lib.uploadFile(req.filePath, req.remotePath, () => {
            cb();
            req.cb();
        });
    }, concurrency),

    createBucket(bucketName, callback) {
        s3.createBucket({Bucket: bucketName}, function(err, res) {
            callback(err, res);
        });
    },

    uploadFile(filePath, remotePath, callback) {
        fs.readFile(filePath, (err, data) => {
            let contentType = files.getContentType(filePath);

            let params = {
                ACL: 'private',
                Bucket: 'openneuro.snapshots',
                Key: remotePath,
                Body: data,
                ContentType: contentType
            };

            let upload = new aws.S3.ManagedUpload({params});
            upload.send(function(err) {
                if (err) {
                    // console.log(err);
                }
                callback();
            });
        });
    },

    uploadDirectory(dirPath, callback) {
        files.getFiles(dirPath, (files) => {
            async.each(files, (filePath, cb) => {
                let remotePath = filePath.slice((config.location + '/persistent/datasets/').length);
                this.queue.push({
                    filePath,
                    remotePath,
                    cb
                });
            }, callback);
        });
    }

};

export default s3lib;
@@ -290,16 +290,30 @@
 ib = %7B%0A%0A
+    api: s3,%0A%0A
     queu
@@ -631,32 +631,33 @@
     %7D);%0A    %7D,%0A%0A
+%0A
     uploadFile(f
@@ -1665,24 +1665,603 @@
      %7D, 
-callback
+()  =%3E %7B%0A                s3.putObjectTagging(%7B%0A                    Bucket: 'openneuro.snapshots',%0A                    Key: dirPath.slice((config.location + '/persistent/datasets/').length) + '/dataset_description.json',%0A                    Tagging: %7B%0A                        TagSet: %5B%0A                            %7B%0A                                Key: 'datasetComplete',%0A                                Value: 'true'%0A                            %7D%0A                        %5D%0A                    %7D%0A                %7D, () =%3E %7B%0A                    callback();%0A                %7D);%0A            %7D
 );%0A     

Add tagging strategy on successful transfer