我需要使用S3从aws-sdk-js桶下载大量图像。
现在我打电话给s3.getObject(params, function (err, data)来做这件事。我的列表中的每一个文件都有循环吗?
在aws-sdk-js中,除了一个一个地下载文件外,还有其他更快、更有效的下载方法吗?
发布于 2016-07-22 12:38:28
基本上,您希望同步s3桶上的目录。
您可以使用aws的aws s3 sync或s3cmd CLI的s3cmd put --recursive。
如果您想要使用aws,那么API中没有同步方法,幸运的是,很多人已经实现了这个功能。
例如,节点-S3-客户机具有以下功能
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");
});发布于 2016-07-23 09:13:32
您可以使用Node s3fs模块,它提供与fs模块类似的功能。要下载多个文件,我建议您使用简单易用的s3fs模块。您可以使用s3桶使用s3fs将文件写入目录中,然后使用readdirp函数:
var fsImpl = new S3FS('test-bucket', options);
fsImpl.readdirp('test-folder').then(function(files) {
// Files contains a list of all of the files similar to [`fs.readdir(path, callback)`](http://nodejs.org/api/fs.html#fs_fs_readdir_path_callback) but with recursive contents
}, function(reason) {
// Something went wrong
});来源:https://www.npmjs.com/package/s3fs
要设置s3fs,请查看下面的链接,它非常容易使用:
https://stackoverflow.com/questions/38524108
复制相似问题