繁体   English   中英

s3.getObject 中的回调函数不起作用 - Node 12.x

[英]Callback function inside s3.getObject is not working - Node 12.x

我正在尝试从 S3 服务获取图像,并在 Lambda 中调整其大小。 我正在使用节点 12.x

    s3.getObject(params, function(err, data) {
      console.log("inside getobject")
      if (err) console.log(err, err.stack); // an error occurred
      else {
        console.log("let there be data")
        Sharp(data.Body)
        .resize({ width: 100 })
        .toFormat('png')
        .toBuffer()
      .then(data => 
        s3.putObject({
          Body: data,
          Bucket: bucket + '-dest',
          ContentType: 'image/png',
          Key: key + '-150X150',
        }).promise()
      )
      .then(() => callback(null, {
          statusCode: '301',
          headers: {'location': `${URL}/${key}`},
          body: '',
        })
      )
      .catch(err => callback(err))
      };           // successful response
    });

如您所见,我到处都有控制台日志,以查看该功能的哪一部分不起作用。 事实证明,回调函数本身没有被调用。

如需清晰图片,请参阅 CloudWatch 中的日志。 CloudWatch 日志中未显示代码中的任何控制台日志

2020-10-14T20:30:57.442+05:30   START RequestId: 06c20f38-444b-4932-9d2b-c650a4e12e57 Version: $LATEST

2020-10-14T20:30:57.459+05:30   2020-10-14T15:00:57.444Z 06c20f38-444b-4932-9d2b-c650a4e12e57 INFO Started

2020-10-14T20:30:57.459+05:30   2020-10-14T15:00:57.459Z 06c20f38-444b-4932-9d2b-c650a4e12e57 INFO hbjb.jpg

2020-10-14T20:30:57.460+05:30   2020-10-14T15:00:57.459Z 06c20f38-444b-4932-9d2b-c650a4e12e57 INFO serv-image-uploads

2020-10-14T20:30:57.499+05:30   2020-10-14T15:00:57.461Z 06c20f38-444b-4932-9d2b-c650a4e12e57 INFO { Bucket: 'serv-image-uploads', Key: 'hbjb.jpg' }

2020-10-14T20:30:58.180+05:30   END RequestId: 06c20f38-444b-4932-9d2b-c650a4e12e57

2020-10-14T20:30:58.180+05:30   REPORT RequestId: 06c20f38-444b-4932-9d2b-c650a4e12e57 Duration: 737.66 ms Billed Duration: 800 ms Memory Size: 128 MB Max Memory Used: 107 MB Init Duration: 584.66 ms

可能是什么原因?

您有语法错误...请告诉我是否解决了您的代码运行良好

s3.getObject(params, function(err, data) {
      console.log("inside getobject"); // line was changed
      if (err) { console.log(err, err.stack); // line was changed
      } else { // line was changed
        console.log("let there be data"); // line was changed
        Sharp(data.Body)
        .resize({ width: 100 })
        .toFormat('png')
        .toBuffer()
      .then(data => 
        s3.putObject({
          Body: data,
          Bucket: bucket + '-dest',
          ContentType: 'image/png',
          Key: key + '-150X150',
        }).promise()
      )
      .then(() => callback(null, {
          statusCode: '301',
          headers: {'location': `${URL}/${key}`},
          body: '',
        })
      )
      .catch(err => callback(err))
      };           // successful response
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM