大佬教程收集整理的这篇文章主要介绍了node.js – 在amazon lambda中,并行抛出多个缩略图大小异步抛出错误:Stream产生空缓冲区,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我的代码在几秒内在本地运行正常,但在lambda云中,它不会并行运行,在调整第一个缩略图大小后抛出错误..如果我将其切换为串行而不是并行,则需要大约60连续运行的秒数.
为什么在lambda中并行运行调整大小代码会导致流产生空缓冲区错误.如何提高性能,以便我可以在几秒钟内创建尺寸,但在处理器成本方面仍然可以从lambda中获得良好的价值和效率?
// dependencies var async = require('async'); var AWS = require('aws-sdk'); var gm = require('gm') .subClass({ imageMagick: true }); // Enable ImageMagick integration. var util = require('util'); // constants var SIZES = [100,320,640]; // get reference to S3 client var s3 = new AWs.S3(); exports.handler = function(event,context) { // Read options from the event. console.log("Reading options from event:\n",util.inspect(event,{depth: 5})); var srcBucket = event.Records[0].s3.bucket.name; var srcKey = event.Records[0].s3.object.key; var dstBucket = srcBucket + "-resized"; // Infer the image type. var typeMatch = srcKey.match(/\.([^.]*)$/); if (!typeMatch) { console.error('unable to infer image type for key ' + srcKey); return context.done(); } var imageType = typeMatch[1]; if (imageType != "jpg" && imageType != "png") { console.log('skipping non-image ' + srcKey); return context.done(); } // Sanity check: validate that source and desTination are different buckets. if (srcBucket == dstBucket) { console.error("DesTination bucket must not match source bucket."); return context.done(); } // Download the image from S3 s3.getObject({ Bucket: srcBucket,Key: srcKey },function(err,responsE){ if (err) return console.error('unable to download image ' + err); var contentType = response.ContentType; var original = gm(response.body); original.size(function(err,sizE){ if(err) return console.error(err); //transform,and upload to a different S3 bucket. async.each(SIZES,function (max_size,callBACk) { resize_photo(size,max_size,imageType,original,srcKey,dstBucket,contentType,callBACk); },function (err) { if (err) { console.error( 'Unable to resize ' + srcBucket + ' due to an error: ' + err ); } else { console.log( 'successfully resized ' + srcBucket ); } context.done(); }); }); }); }; //wrap up variables into an options object var resize_photo = function(size,donE) { var dstKey = max_size + "_" + srcKey; // transform,and upload to a different S3 bucket. async.waterfall([ function transform(next) { // Infer the scaling factor to avoid stretching the image unnaturally. var scalingFactor = Math.min( max_size / size.width,max_size / size.height ); var width = scalingFactor * size.width; var height = scalingFactor * size.height; // Transform the image buffer in memory. original.resize(width,height) .toBuffer(imageType,buffer) { if (err) { next(err); } else { next(null,buffer); } }); },function upload(data,next) { // Stream the transformed image to a different S3 bucket. s3.putObject({ Bucket: dstBucket,Key: dstKey,Body: data,ContentType: contentType },next); } ],function (err) { console.log('finished resizing ' + dstBucket + '/' + dstKey); if (err) { console.error(err) ; } else { console.log( 'successfully resized ' + dstKey ); } done(err); } ); };
以上是大佬教程为你收集整理的node.js – 在amazon lambda中,并行抛出多个缩略图大小异步抛出错误:Stream产生空缓冲区全部内容,希望文章能够帮你解决node.js – 在amazon lambda中,并行抛出多个缩略图大小异步抛出错误:Stream产生空缓冲区所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。