如何将多个文件(50K+)/文件夹上传到AWS S3 Node.js

发布于 2025-02-14 01:13:41 字数 793 浏览 0 评论 0原文

我有一个在Windows计算机上运行的节点JS API,该节点会生成一些XML文件,这些文件后来将其上传到S3桶。文件数量超过50k,有时甚至更多。

在我当前的方法中,我正在使用 aws-sdk 用于上传的软件包。基本上,我循环浏览需要上传的文件夹,读取每个文件并上传。

const files = fs.readdirSync(dirPath, {
            withFileTypes: true
});
for (const file of files) {
      const fileContent = fs.readFileSync(path.join(dirPath, file.name));
      const params = {
          Bucket: BUCKET_NAME,
          Key: `${folderPath}/${file.name}`,
          Body: fileContent
      };
      try {
          await s3.upload(params).promise()
      } catch (err) {
          //error handling
          return;
      }
}

这大约需要3-4个小时才能上传。有什么更好的方法可以批量上传文件吗?还是有什么方法可以上传整个文件夹?

提前致谢

I've a node js API running on a windows machine which generates some XML files which are later uploaded to S3 bucket. The number of files exceed 50k and sometimes even more.

In my current approach, I'm using aws-sdk package for uploading. Basically I loop through the folder that needs to be uploaded, read every file and upload it.

const files = fs.readdirSync(dirPath, {
            withFileTypes: true
});
for (const file of files) {
      const fileContent = fs.readFileSync(path.join(dirPath, file.name));
      const params = {
          Bucket: BUCKET_NAME,
          Key: `${folderPath}/${file.name}`,
          Body: fileContent
      };
      try {
          await s3.upload(params).promise()
      } catch (err) {
          //error handling
          return;
      }
}

This takes around 3-4 hours to upload. Is there any better way to bulk upload files? Or if any way to upload entire folder?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不喜欢何必死缠烂打 2025-02-21 01:13:41

我建议您首先将文件夹汇总,然后将Zipped文件夹上传到S3。在BASH脚本中,您可以这样做:

zip -r data.zip ./target_folder/

然后您可以将S3上传到:

aws s3 cp ./data.zip s3://bucket_name/

希望有帮助!

I would recommend you zip your folder first, and then upload the zipped folder to the S3. In bash script you can do that as:

zip -r data.zip ./target_folder/

And then you can upload to S3 as:

aws s3 cp ./data.zip s3://bucket_name/

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文