2014-12-27 4 views
6

ich derzeit einzelne Objekte S3 laden wie so mit:Hochladen von ganzen Verzeichnisbaum bis S3 AWS SDK in Knoten js

var options = { 
     Bucket: bucket, 
     Key: s3Path, 
     Body: body, 
     ACL: s3FilePermissions 
}; 

S3.putObject(options, 
function (err, data) { 
    //console.log(data); 
}); 

Aber wenn ich einen großen Ressourcenordner zum Beispiel haben, verwende ich das AWS CLI Werkzeug.
Ich frage mich, gibt es eine native Möglichkeit, das gleiche mit der AWS SDK (Upload von ganzen Ordnern zu s3) zu tun?

Antwort

3

Ich betrachtete gerade dieses Problem neulich, und dachte, so etwas wie dieses:

...  
var async = require('async'), 
    fs = require('fs'), 
    path = require("path"); 

var directoryName = './test', 
    directoryPath = path.resolve(directoryName); 

var files = fs.readdirSync(directoryPath); 
async.map(files, function (f, cb) { 
    var filePath = path.join(directoryPath, f); 

    var options = { 
     Bucket: bucket, 
     Key: s3Path, 
     Body: fs.readFileSync(filePath), 
     ACL: s3FilePermissions 
    }; 

    S3.putObject(options, cb); 

}, function (err, results) { 
    if (err) console.error(err); 
    console.log(results); 
}); 
+2

'fs.readFileSync (filePath)' Diese Zeile gibt mir 'Fehler erhalten helfen: EISDIR: auf einem Verzeichnis illegalen Betrieb, read' wie hier: http : //stackoverflow.com/questions/25883775/node-js-readfilesync-function –

0

Sie könnten die node-s3-client versuchen.

UPDATE: Verfügbar auf npm here

Vom sync ein Verzeichnis docs bis s3:

UPDATE: Hinzugefügt Client inialization Code.

var client = s3.createClient({ 
    maxAsyncS3: 20,  // this is the default 
    s3RetryCount: 3, // this is the default 
    s3RetryDelay: 1000, // this is the default 
    multipartUploadThreshold: 20971520, // this is the default (20 MB) 
    multipartUploadSize: 15728640, // this is the default (15 MB) 
    s3Options: { 
     accessKeyId: "YOUR ACCESS KEY", 
     secretAccessKey: "YOUR SECRET ACCESS KEY" 
    } 
    }); 

var params = { 
    localDir: "some/local/dir", 
    deleteRemoved: true, // default false, whether to remove s3 objects 
         // that have no corresponding local file. 

    s3Params: { 
    Bucket: "s3 bucket name", 
    Prefix: "some/remote/dir/", 
    // other options supported by putObject, except Body and ContentLength. 
    // See: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property 
    }, 
}; 
var uploader = client.uploadDir(params); 
uploader.on('error', function(err) { 
    console.error("unable to sync:", err.stack); 
}); 
uploader.on('progress', function() { 
    console.log("progress", uploader.progressAmount, uploader.progressTotal); 
}); 
uploader.on('end', function() { 
    console.log("done uploading"); 
}); 
+3

Dies funktioniert nicht. Es hängt nur vom Fortschritt ab. Kein Fehler, kein Fortschritt. –

+0

Sind Sie sicher, dass der Client ordnungsgemäß initialisiert wurde? Ich habe die Antwort bearbeitet, um die Client-Initialisierung einzuschließen. – unboundev

+1

Ja. Andernfalls wird ein Fehler angezeigt. SO kein Fehler, der Client wird ordnungsgemäß initialisiert, aber die Synchronisierung wird nicht ausgeführt. –

3

Old-School-rekursive Art, die ich in Eile aufpeitschte. Verwendet nur Kernknotenmodule und Standard-AWS-SDK.

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

const uploadDir = function(s3Path, bucketName) { 

    let s3 = new AWS.S3(); 

    function walkSync(currentDirPath, callback) { 
     fs.readdirSync(currentDirPath).forEach(function (name) { 
      var filePath = path.join(currentDirPath, name); 
      var stat = fs.statSync(filePath); 
      if (stat.isFile()) { 
       callback(filePath, stat); 
      } else if (stat.isDirectory()) { 
       walkSync(filePath, callback); 
      } 
     }); 
    } 

    walkSync(s3Path, function(filePath, stat) { 
     let bucketPath = filePath.substring(s3Path.length+1); 
     let params = {Bucket: bucketName, Key: bucketPath, Body: fs.readFileSync(filePath) }; 
     s3.putObject(params, function(err, data) { 
      if (err) { 
       console.log(err) 
      } else { 
       console.log('Successfully uploaded '+ bucketPath +' to ' + bucketName); 
      } 
     }); 

    }); 
}; 

uploadDir("path to your folder", "your bucket name"); 

Besonderer Dank geht an Ali von this post mit den Dateinamen