Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – aws lambda如何在S3中存储通过https检索的图像大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试编写一个lambda脚本,可以从站点提取图像并将其存储在S3中.我遇到的问题是什么样的对象作为Body属性传递到S3.putObject方法.在文档 here中,它说它应该是新的Buffer(‘…’)|| ‘StriNG_VALUE’|| streamObject,但我不确定如何将https响应转换为其中之一.这是我尝试过的:

var AWS = require('aws-sdk');
var https = require('https');
var Readable = require('stream').Readable;
var s3 = new AWs.S3();
var fs = require('fs');

var url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/1d/AmazonWebservices_logo.svg/500px-AmazonWebservices_logo.svg.png';

exports.handler = function(event,context) {
  https.get(url,function(responsE) {
    var params = {
     Bucket: 'example',Key: 'aws-logo.png',Body: response  // fs.createReadStream(responsE); doesn't work,arg should be a path to a file...
                     // just putTing response errors out with "CAnnot determine length of [object Object]"
    };

    s3.putObject(params,function(err,data) {
      if (err) {
        console.error(err,err.stack);
      } else {
        console.log(data);
      }
    });
  });
};@H_197_8@

解决方法

评论中所示,Lambda允许将文件保存在/ tmp中.但你真的不需要它……

响应不包含文件内容,而是包含http响应(包含其状态代码和标头).

你可以尝试这样的事情:

var AWS = require('aws-sdk');
var https = require('https');
var s3 = new AWs.S3();

var url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/1d/AmazonWebservices_logo.svg/500px-AmazonWebservices_logo.svg.png';

exports.handler = function(event,function(res) {
    var body = '';
    res.on('data',function(chunk) {
      // Agregates chunks
      body += chunk;
    });
    res.on('end',function() {
      // Once you received all chunks,send to S3
      var params = {
        Bucket: 'example',Body: body
      };
      s3.putObject(params,data) {
        if (err) {
          console.error(err,err.stack);
        } else {
          console.log(data);
        }
      });
    });
  });
};@H_197_8@

大佬总结

以上是大佬教程为你收集整理的node.js – aws lambda如何在S3中存储通过https检索的图像全部内容,希望文章能够帮你解决node.js – aws lambda如何在S3中存储通过https检索的图像所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。