[英]AWS S3 Node.js SDK: Need to Download all files from one bucket at once
Currently I can iterate over all files in bucket and download one by one, using node.js sdk.目前我可以遍历bucket中的所有文件并使用node.js sdk一一下载。 I need to download all files from S3 bucket at once.
我需要一次从 S3 存储桶下载所有文件。 then remove it from bucket.
然后将其从桶中取出。
This higher-level Node S3 library has a downloadDir
function which syncs a remote S3 bucket with a local directory.这个更高级别的节点 S3 库有一个
downloadDir
函数,该函数将远程 S3 存储桶与本地目录同步。
https://github.com/andrewrk/node-s3-client#clientdownloaddirparams https://github.com/andrewrk/node-s3-client#clientdownloaddirparams
You can use the aws cli
using the command below:您可以使用以下命令使用
aws cli
:
aws s3 sync s3://yourbucket .
If you have to use node
then you can use exec
or spwan
.如果必须使用
node
则可以使用exec
或spwan
。
const exec = require('child_process').exec;
exec('aws s3 sync s3://yourbucket .', (err, stdout, stderr) => {});
Let's say you have a function listFiles
that returns an array of the Keys in the bucket you want to download and a function downloadFile
that accepts a file Key (name) and downloads it and then deletes it.假设您有一个函数
listFiles
返回要下载的存储桶中的键数组,还有一个函数downloadFile
接受文件键(名称)并下载它,然后将其删除。
You want to call downloadFile
simultaneously on all files returned from listFiles
right?您想对从
listFiles
返回的所有文件同时调用downloadFile
对吗? Use the async
library.使用
async
库。
function listFiles(cb) {
s3.stuff(params, cb);
}
function downlaodFile(key, cb) {
s3.stuff(key, cb);
}
listFiles(function (err, fileKeys) {
if (err) {
throw err;//don't really but this is just an example
}
async.each(fileKeys, downloadFile, function done(err) {
if (err) {
throw err;
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.