读取多个文件并将上传到AWS S3

发布于 2025-02-10 14:46:13 字数 1675 浏览 1 评论 0原文

要求:

我在Express Server上的文件夹中有多个文件。我将这些文件名作为API调用传递,后端函数需要读取所有这些文件,将其上传到AWS S3,然后返回一系列公共URL。

const s3 = new aws.S3();
const fs = require("fs");


module.exports = {
  upload: async function (req, res, next) {
    console.log("Inside upload controller");
    let publicUrls = [];

    const result = await Promise.all(
      req.body.files.map(async (uploadFile) => {
        fs.promises.readFile(uploadFile).then((file) => {
          let body = fs.createReadStream(uploadFile);
          const s3PutParams = {
            Bucket: process.env.S3_BUCKET_NAME,
            Key: uploadFile.substr(15),
            Body: body,
            ACL: "public-read",
          };

          s3.upload(s3PutParams)
            .promise()
            .then((response) => {
              console.log(response.Location);
              publicUrls.push(response.Location);
            });
        });
      })
    );
    if (result) {
      console.log("Result", result);
      res.json(publicUrls);
    }
  },
  
};

观察到的输出:

Inside upload controller
Result [ undefined, undefined, undefined, undefined ]
https://xxx.s3.amazonaws.com/2_30062022.pdf
https://xxx.s3.amazonaws.com/1_30062022.pdf
https://xxx.s3.amazonaws.com/1_30062022.pdf
https://xxx.s3.amazonaws.com/2_30062022.pdf

我正在传递一个4个文件名的数组,因此在记录“结果”时4个“未定义”

“ 问题: 该代码不在等待承诺。 它立即返回JSON响应,这是一个空数阵列。

如何解决?

Requirement:

I have multiple files in a folder on my express server. I pass these file names as an API call, the backend function needs to read all these files, upload them to AWS S3 and then return an array of public URLs.

const s3 = new aws.S3();
const fs = require("fs");


module.exports = {
  upload: async function (req, res, next) {
    console.log("Inside upload controller");
    let publicUrls = [];

    const result = await Promise.all(
      req.body.files.map(async (uploadFile) => {
        fs.promises.readFile(uploadFile).then((file) => {
          let body = fs.createReadStream(uploadFile);
          const s3PutParams = {
            Bucket: process.env.S3_BUCKET_NAME,
            Key: uploadFile.substr(15),
            Body: body,
            ACL: "public-read",
          };

          s3.upload(s3PutParams)
            .promise()
            .then((response) => {
              console.log(response.Location);
              publicUrls.push(response.Location);
            });
        });
      })
    );
    if (result) {
      console.log("Result", result);
      res.json(publicUrls);
    }
  },
  
};

Observed Output:

Inside upload controller
Result [ undefined, undefined, undefined, undefined ]
https://xxx.s3.amazonaws.com/2_30062022.pdf
https://xxx.s3.amazonaws.com/1_30062022.pdf
https://xxx.s3.amazonaws.com/1_30062022.pdf
https://xxx.s3.amazonaws.com/2_30062022.pdf

I am passing an array of 4 file names, hence 4 "undefined" while logging "result"

enter image description here
Issue:
The code is not awaiting for the Promise.all to be completed.
It right away returns the json response, which is an empty array at that point.

How can this be resolved?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文