美好的一天
如何将所有内容从S3存储桶下载到Local temp文件夹以进行进一步处理?
谢谢!
答案 0 :(得分:0)
下面的代码将在您的本地临时文件夹中下载Bucket结构...
var AWS = require('aws-sdk');
var fs = require('fs');
var s3 = new AWS.S3
({
accessKeyId: <AccessKeyID>, //REQUIRED
secretAccessKey:<SecretAccessKey> //REQUIRED
});
var params =
{
Bucket: <SourceBucket> //REQUIRED
}
// CREATE A BUCKET SUBFOLDER
fs.mkdirSync(params.Bucket);
fs.mkdirSync(params.Bucket+ "/" + <FolderName>);
s3.listObjects({ Bucket: params.Bucket }, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else {
data.Contents.forEach(function(currentValue, index, array){
// CHECK IF THE FILE ALREADY EXISTS
fs.exists(params.Bucket + "/" + currentValue.Key, function(exists){
if (exists)
{
console.log("Skipping: " + currentValue.Key);
}
else
{
console.log("Retrieving the file : " + currentValue.Key);
s3.getObject({ Bucket: params.Bucket, Key: currentValue.Key }, function(err, data) {
if (err) console.log(err, err.stack);
else {
fs.writeFile(params.Bucket + "/" + currentValue.Key, data.Body, function(){
console.log("Finished downloading the file: " + currentValue.Key);
});
}
});
}
});
});
}
});
希望这很有用。 请给出意见 谢谢。