【问题标题】:node.js Async : crop image and upload to servernode.js 异步:裁剪图像并上传到服务器
【发布时间】:2017-09-19 08:36:10
【问题描述】:

我想将图像裁剪成各种尺寸并将它们上传到 AWS S3。裁剪尺寸存储在一个数组中。

我正在使用async 瀑布和series 方法来实现这一点。

async.each(crop_sizes,function (result,cb) {

            async.waterfall([
                /*
                 * Crop the images to specific size
                 */
                function (callback) {
                    gm(path)
                        .resize(result.width,result.height,'^')
                        .stream(function (err,buffer) {
                            console.log('cropped')
                            callback(null,{'buffer':buffer,'filename':filename,'s3':s3});
                        });
                },
                function (output,callback) {
                    var params ={
                        Bucket:'mybucket',
                        Key:'artwork-croppedimages/'+result.folder+output.filename,
                        Body:output.buffer
                    }

                    var options ={
                        partSize: 5242880, queueSize: 1
                    };

                    s3.upload(params,options,function(err, data) {
                        console.log("upload");

                        callback();
                    });
                }

            ],function (err,result) {
                console.log("one iteration completed");
                cb();
            });
        },function (err,result) {
            console.log("All upload complete");
        });

这就是代码的执行方式:

在async.each 中重复步骤 1 和 2。

  1. 裁剪图像。

  2. 将裁剪后的图像上传到 AWS S3。

但上传大尺寸图片时失败。这是我得到的输出:

cropped
cropped
cropped
cropped
cropped
cropped
cropped
cropped
cropped
cropped
cropped
cropped
upload
one iteration completed
upload
one iteration completed
upload
one iteration completed
upload
one iteration completed
upload
one iteration completed
upload
one iteration completed
upload
one iteration completed
upload
one iteration completed
upload
one iteration completed
upload
one iteration completed
upload
one iteration completed
upload
one iteration completed
All upload complete

预期输出:

 cropped
   upload
   one iteration completed
  cropped
   upload
   one iteration completed
   .....
  All upload complete

【问题讨论】:

  • 您正在使用 async.each 并行运行所有操作。所以 async.waterfall([]) 会为所有项目并行触发。您需要使用 async.eachSeries 来获得您期望的每次迭代的等待位置。
  • 请更新代码
  • 您可以尝试将 async.each 更改为 async.eachSeries 吗?让我知道结果?
  • 让我检查一下
  • 感谢它的工作

标签: javascript node.js express asynchronous gm


【解决方案1】:

你能试试async.eachOfSeries函数吗?我怀疑async.each 函数并行运行迭代器(如文档中所述),这一定是导致问题的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-13
    • 2015-08-10
    • 2021-06-28
    • 2016-12-15
    • 2018-05-16
    • 2017-08-05
    • 2017-08-13
    相关资源
    最近更新 更多