【问题标题】:How do I return the data from S3 bucket in node? [duplicate]如何从节点中的 S3 存储桶返回数据? [复制]
【发布时间】:2022-01-06 10:03:19
【问题描述】:

我希望 main() 返回数据,而不仅仅是控制台记录数据,如下所示。我该怎么做?

async function main() {
  // Load the AWS SDK for Node.js
  var AWS = require("aws-sdk");
  // Set the region
  AWS.config.update({ region: "REGION" });

  // Create S3 service object
  s3 = new AWS.S3({ apiVersion: "2006-03-01" });

  // Call S3 to list the buckets
  s3.listBuckets(function (err, data) {
    if (err) {
      console.log("Error", err);
    } else {
      console.log(data);
    }
  });
}
main();

【问题讨论】:

    标签: javascript node.js amazon-s3


    【解决方案1】:

    尝试等待listBuckets 函数。

    改变

    s3.listBuckets(function (err, data) {
      if (err) {
        console.log("Error", err);
      } else {
        console.log(data);
      }
    });
    

    到

    return await s3.listBuckets();
    

    如果这不起作用,您可以随时构建一个手册 Promise 并使用数据解决它。

    async function main() {
      // Load the AWS SDK for Node.js
      var AWS = require("aws-sdk");
      // Set the region
      AWS.config.update({ region: "REGION" });
    
      // Create S3 service object
      s3 = new AWS.S3({ apiVersion: "2006-03-01" });
    
      return await new Promise((resolve, reject) => {
        // Call S3 to list the buckets
        s3.listBuckets(function (err, data) {
          if (err) {
            reject(err);
          } else {
            resolve(data);
          }
        });
      });
    }
    main();
    

    【讨论】:

    • 谢谢。我已经尝试过了,它返回了目前没有用的属性。我有兴趣在回调函数中返回数据。
    • @Gerald 查看更新的答案
    猜你喜欢
    • 2019-04-02
    • 1970-01-01
    • 1970-01-01
    • 2014-09-04
    • 2019-05-14
    • 2017-11-23
    • 2018-06-18
    • 2022-01-07
    • 2022-01-11
    相关资源
    最近更新 更多