NodeJS - 在发送到AWS S3之前处理图像

时间:2015-02-03 18:19:01

标签: node.js image-processing amazon-web-services amazon-s3

好的,今天我有些困惑,我确信这是一个简单的解决方案。问题是在发送到S3之前的图像处理,这是我在保存之前和使用AWS之前操作图像的方式:

    // Form
    var form = new formidable.IncomingForm();
    form.parse(req, function(err, fields, files) {
        res.writeHead(200, {'content-type': 'text/plain'});
    });
    // Form On
    form.on('end', function(fields, files) {
        /* Temporary location of our uploaded file */
        var temp_path = this.openedFiles[0].path;
        /* The file name of the uploaded file */
        var file_name = this.openedFiles[0].name;
        /* Location where we want to copy the uploaded file */
        var new_location = 'public/assets/uploads/blog/';
        // Rename Image
        var t = path.extname(file_name);
        var n = Math.floor(new Date() / 1000);
        // Copy Image
        fse.copy(temp_path, new_location+n+t, function(err) {  
            if (err) {
                console.error(err);
            } else {

                //
                // Resize and Blur 
                //

                require('lwip').open(new_location+n+t, function(err, image) {
                    image.batch()
                        //.scale(0.75)          // scale to 75%
                        //.rotate(45, 'white')  // rotate 45degs clockwise (white fill)
                        //.crop(200, 200)       // crop a 200X200 square from center
                        .blur(7)                // Gaussian blur with SD=5
                        .writeFile('public/assets/uploads/blog/blur/'+n+t, function(err){
                        // check err...
                        if(err) {
                            console.log(err);
                        }
                        // done.
                        console.log('Success');
                        // Send Data
                        res.write(n+t);
                        res.end();
                    });
                });
            }
        });
  });

非常简单的东西吧。我正在做的就是使用强大的方法处理传入的表单,然后使用'lwip'调整大小(如果需要)并模糊图像的副本(放在新目录中)

那么在将数据发送到AWS S3之前,我怎样才能在调整大小和模糊时使用'lwip'?

1 个答案:

答案 0 :(得分:2)

好的,我正在回答我自己的问题,这很酷。这就是我提出的在本地和Heroku上工作的内容。我使用了模块s3-uploader。

var client = s3.createClient({
    maxAsyncS3: 20,     // this is the default 
    s3RetryCount: 0,    // this is the default 
    s3RetryDelay: 1000, // this is the default 
    multipartUploadThreshold: 20971520, 
    multipartUploadSize: 15728640, 
    s3Options: {
        accessKeyId: "key",
        secretAccessKey: "secret",
    },
});
exports.s3 = function(req, res, next) {

    // Formidable
    var form = new formidable.IncomingForm();
    form.parse(req, function(err, fields, files) {
       res.writeHead(200, {'content-type': 'text/plain'});
    });


    form.on('end', function(fields, files) {
       /* Temporary location of our uploaded file */
       var temp_path = this.openedFiles[0].path;
       /* The file name of the uploaded file */
       var file_name = this.openedFiles[0].name;
       /* Location where we want to copy the uploaded file */
       var new_location = 'public/assets/uploads/s3/';
       // Rename Image
       var e = path.extname(file_name);
       var n = Math.floor(new Date() / 1000);

    // Copy Image 

    fse.copy(temp_path, new_location+n+e, function(err) {  

        // AWS Params
        var params = {
          localFile:  new_location+n+e,
          s3Params: {
            Bucket: "hirelee-uploads",
            Key: "blog/"+n+e,
          },
        };
        // AWS Upload
        var uploader = client.uploadFile(params);
        uploader.on('error', function(err) {
          console.error("unable to upload:", err.stack);
        });
        uploader.on('end', function() {

            // Blur Copied Image

            require('lwip').open(params.localFile, function(err, image) {
                image.batch()
                    .blur(7)                
                    .writeFile("public/assets/uploads/s3/blur-"+n+e, function(err){
                    // check err...
                    if(err) {
                        console.log(err);
                    } else { 

                        // AWS Upload Blur

                        var params = {
                            localFile: "public/assets/uploads/s3/blur-"+n+e,

                            s3Params: {
                                Bucket: "hirelee-uploads",
                                Key: "blog/blur/"+n+e,
                            },
                        };

                        var uploader = client.uploadFile(params);

                        uploader.on('error', function(err) {
                            console.error("unable to upload:", err.stack);
                        });

                        // Finished AWS upload
                        uploader.on('end', function() {
                            console.log("done uploading");

                        // Delete Copied Images on Disk

                 fs.unlinkSync("public/assets/uploads/s3/"+n+e)
                 fs.unlinkSync("public/assets/uploads/s3/blur-"+n+e)

                            res.end();
                        });
                    }
                });
            });

        });

    });

如果我错过了任何可能会更好的内容,或者可以使上述内容更有效,那么任何人都可以给我一个喊叫。