Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 承诺或与Node js异步_Node.js_Amazon S3_Aws Lambda - Fatal编程技术网

Node.js 承诺或与Node js异步

Node.js 承诺或与Node js异步,node.js,amazon-s3,aws-lambda,Node.js,Amazon S3,Aws Lambda,我有大量的代码,从S3存储桶中获取图像,将其保存到Lambda上的临时文件中,将其大小调整为4种不同的大小,根据大小将其保存到不同的文件夹中,然后他们将图像放回S3存储桶中,也放回不同的文件夹中 但是,在Lambda上运行时,我必须在整个过程结束时调用context.done(),否则该上下文将保持活动状态,直到Lambda超时 因此,当upload最后一次返回时,我需要调用context.done() 看看这两个选项,async和promissions,它们可能需要更少的代码重构才能工作 //

我有大量的代码,从S3存储桶中获取图像,将其保存到Lambda上的临时文件中,将其大小调整为4种不同的大小,根据大小将其保存到不同的文件夹中,然后他们将图像放回S3存储桶中,也放回不同的文件夹中

但是,在Lambda上运行时,我必须在整个过程结束时调用
context.done()
,否则该上下文将保持活动状态,直到Lambda超时

因此,当
upload
最后一次返回时,我需要调用
context.done()

看看这两个选项,
async
promissions
,它们可能需要更少的代码重构才能工作

// dependencies
var AWS = require('aws-sdk');
var gm = require('gm').subClass({ imageMagick: true });
var fs = require("fs");

// get reference to S3 client
var s3 = new AWS.S3();

var _800px = {
    width: 800,
    destinationPath: "large"
};

var _500px = {
    width: 500,
    destinationPath: "medium"
};

var _200px = {
    width: 200,
    destinationPath: "small"
};

var _45px = {
    width: 45,
    destinationPath: "thumbnail"
};

var _sizesArray = [_800px, _500px, _200px, _45px];

var len = _sizesArray.length;
 module to be exported when in production
ports.AwsHandler = function(event, context) {
  // Read options from the event.
  var srcBucket = event.Records[0].s3.bucket.name;
  var srcKey = event.Records[0].s3.object.key;
  var dstnFolder = "/tmp";
  // function to determine paths
  function _filePath (directory, i) {
      if ( directory === false ) {
          return "dst/" + _sizesArray[i].destinationPath + "/" + srcKey;
      } else if ( directory === true ) {
          return dstnFolder + "/" + _sizesArray[i].destinationPath + "/" + srcKey;
      }
  };
  for ( var i = 0; i<len; i++) {
      fs.mkdir("/tmp" + "/" + _sizesArray[i].destinationPath, function (err) {
          if (err) {
              console.log(err);
          }
      });
  };
  // Infer the image type.
  var typeMatch = srcKey.match(/\.([^.]*)$/);
  if (!typeMatch) {
      console.error('unable to infer image type for key ' + srcKey);
      return;
  };
  var imageType = typeMatch[1];
  if (imageType != "jpg" && imageType != "png") {
      console.log('skipping non-image ' + srcKey);
      return;
  };
  function download () {
      s3.getObject({
              Bucket: srcBucket,
              Key: srcKey
          },
          function (err, response) {
              if (err) {
                  console.error(err);
              }
              fs.writeFile("/tmp" + "/" + srcKey, response.Body, function (err) {
                  transform();
              })
          }
      );
  };
  function transform () {
      var _Key,
          _Size;
      for ( var i = 0; i<len; i++ ) {
          // define path for image write
          _Key = _filePath (true, i);
          // define sizes to resize to
          _Size = _sizesArray[i].width;
          // resize images
          gm("/tmp/" + srcKey)
              .resize(_Size)
              .write(_Key, function (err) {
                  if (err) {
                      return handle(err);
                  }
                  if (!err) {
                      // get the result of write
                      var readPath = this.outname;
                      var iniPath = this.outname.slice(4);
                      var writePath = "dst".concat(iniPath);
                      read(err, readPath, writePath, upload);
                  }
              });
      };
  };
  function read (err, readPath, writePath, callback) {
      // read file from temp directory
      fs.readFile(readPath, function (err, data) {
          if (err) {
              console.log("NO READY FILE FOR YOU!!!");
              console.error(err);
          }
          callback(data, writePath);
      });
  };
  function upload (data, path) {
      // upload images to s3 bucket
      s3.putObject({
              Bucket: srcBucket,
              Key: path,
              Body: data,
              ContentType: data.type
          },
          function (err) {
              if (err) {
                  console.error(err);
              }
              console.log("Uploaded with success!");
          });
  }
  download();
//依赖项
var AWS=要求('AWS-sdk');
var gm=require('gm')。子类({imageMagick:true});
var fs=要求(“fs”);
//获取对S3客户端的引用
var s3=新的AWS.s3();
var_800px={
宽度:800,
destinationPath:“大型”
};
变量_500px={
宽度:500,
目标路径:“中等”
};
var_200px={
宽度:200,
命运之路:“小”
};
变量_45px={
宽度:45,
destinationPath:“缩略图”
};
变量大小数组=[[u800px,[u500px,[u200px,[u45px];
var len=_sizesArray.length;
生产时要导出的模块
ports.AwsHandler=函数(事件、上下文){
//从事件中读取选项。
var srcBucket=event.Records[0].s3.bucket.name;
var srcKey=event.Records[0].s3.object.key;
var dstnFolder=“/tmp”;
//函数来确定路径
函数_文件路径(目录,i){
如果(目录===false){
返回“dst/”+_sizesArray[i]。destinationPath+“/”+srcKey;
}else if(目录===true){
返回dstnFolder++“/”++_size数组[i]。destinationPath++“/”+srcKey;
}
};

对于(var i=0;i,请看一下它们是如何在这方面使用的

您的代码最终将非常类似于

download()
.then(transform)
.then(read)
.then(upload)
.catch(function (error) {
  // Handle any error from all above steps
  console.error(error);
})
.done(function() {
  console.log('Finished processing image');
  context.done();
});
你也可以看看,并使用它,因为他们显示在这另一个