简体   繁体   English

如何使用Express JS 3.X和Angle JS将图像流式传输到Amazon S3?

[英]How to stream image to amazon s3 using express js 3.X and angular js?

I have used the connect-busboy to get the uploaded file but my script doesn't emits the file event. 我已经使用connect-busboy来获取上载的文件,但是我的脚本没有发出文件事件。 I am using angular on the client side. 我在客户端使用角度。

I am using following code: 我正在使用以下代码:

req.busboy.on('error', function (fieldname, file, filename, encoding, mimetype) { console.log(fieldname); }); req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) { //set aws header var awsHeader = { Bucket: 'Mybucket', Key: 'MyImage', ContentType: mimetype, ACL: 'public-read' }; file.uploadFile(file, awsHeader, function (err, res) { console.log(err); console.log(res); }); }); req.busboy.on('finish', function () { res.send({status: true, message: "File uploaded succesfully."}); }); req.busboy.on('field', function (key, value, keyTruncated, valueTruncated) { console.log(key); }); req.pipe(req.busboy);

The finish event gets fired every time I try to upload file. 每次我尝试上传文件时,都会触发finish事件。

Looks like you aren't building your file from the multipart form. 看起来您不是从多部分表单中构建文件。 Before the code below I use node aws-sdk to build an s3Bucket that I can PUT to. 在下面的代码之前,我使用节点aws-sdk构建可以放入的s3Bucket。

req.busboy.on ( 'file', function ( fieldname, file, filename, encoding, mimetype ) {
                if ( !filename ) {
                    // If filename is not truthy it means there's no file
                    return res.status ( 400 ).send ( {error: 'no file'} );
                }

                // Create the initial array containing the stream's chunks
                file.fileRead = [];

                file.on ( 'data', function ( chunk ) {
                    // Push chunks into the fileRead array

                    this.fileRead.push ( chunk );
                    //you can use this if you want to limit file size on ingest                        
                   /*if(this.fileRead.length > 5500000){
                        return res.status(500 ).send({error:"file too large - 5MB max"})
                    }*/
                } );

                file.on ( 'error', function ( err ) {
                    console.log ( 'Error while buffering the stream: ', err );
                } );

                file.on ( 'end', function () {
                    // Concat the chunks into a Buffer
                    var finalBuffer = Buffer.concat ( this.fileRead );

                    req.files[fieldname] = {
                        buffer  : finalBuffer,
                        size    : finalBuffer.length,
                        filename: filename,
                        mimetype: mimetype
                    }


                    var data = {Key: "users/" + req.body.id + '/image/' + req.body.id, Body: req.files[fieldname].buffer, ACL: 'public-read'};
                    //im not sure how you're uploading to s3 but I use this
                     s3Bucket.putObject ( data, function ( err, data ) {
                        if ( err ) {
                            console.log ( err )
                            return res.status ( 400 ).send ( 'error during upload' )
                        } else { //success 
                        }
                    } )
                } );
            } );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM