2014-12-27 17 views
6

Attualmente caricare singoli oggetti a S3 utilizza in questo modo:Carica intero albero di directory per S3 utilizzando AWS sdk nel nodo js

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

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

Ma quando ho una cartella di grandi risorse, ad esempio, utilizzare lo strumento AWS CLI.
Mi stavo chiedendo, esiste un modo nativo per fare la stessa cosa con aws sdk (caricare intere cartelle su s3)?

risposta

3

stavo solo contemplando questo problema, l'altro giorno, e stava pensando qualcosa di simile:

...  
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)' questa linea mi restituisce 'Errore: EISDIR: un'operazione non valida su una directory, read' come qui: http : //stackoverflow.com/questions/25883775/node-js-readfilesync-function –

0

Si potrebbe provare il node-s3-client.

UPDATE: Disponibile su NPM here

Dalla sincronizzazione una directory a S3 docs:

UPDATE: Aggiunto codice client inialization.

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

Questo non funziona. Si blocca solo sui progressi. Nessun errore, nessun progresso. –

+0

Sei sicuro che il client sia inizializzato correttamente? Ho modificato la risposta per includere l'inizializzazione del client. – unboundev

+1

Sì. Altrimenti mostrerà l'errore. Quindi nessun errore, il client è inizializzato correttamente ma la sincronizzazione non avverrà. –

3

vecchio modo ricorsivo della scuola mi sono sbrigato in fretta. Utilizza solo i moduli nodo principale e lo sdk AWS standard.

grazie
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"); 

speciale a Ali da this post con aiutando ottenere i nomi dei file

Problemi correlati