Search code examples
androidnode.jsamazon-s3knox-amazon-s3-client

Keep getting function error not sure why


Not sure what I am doing incorrectly here... I keep getting this error when trying to upload a video (multipart) from android to AmazonS3 using knox library on nodejs... I am using the async library as well to wait for the video to finish uploading before i call the callback (cb) the next function Here is my error:

2015-06-10T08:32:27.877193+00:00 app[web.1]:   stream.on('error', fn); 
2015-06-10T08:32:27.877195+00:00 app[web.1]:          ^ 
2015-06-10T08:32:27.877196+00:00 app[web.1]: TypeError: undefined is not a function
2015-06-10T08:32:27.877198+00:00 app[web.1]:     at Client.putStream (/app/node_modules/knox/lib/client.js:448:10)
2015-06-10T08:32:27.877200+00:00 app[web.1]:     at setupAndCreateImage (/app/routes/challenge.js:397:11)
2015-06-10T08:32:27.877201+00:00 app[web.1]:     at fn (/app/node_modules/async/lib/async.js:582:34)
2015-06-10T08:32:27.877202+00:00 app[web.1]:     at Immediate._onImmediate (/app/node_modules/async/lib/async.js:498:34)
2015-06-10T08:32:27.877204+00:00 app[web.1]:     at processImmediate [as _immediateCallback] (timers.js:358:17)

Here is the code:

function setupAndCreateImage ( cb) {
            if(req.files && req.files.mediaFile) {
                console.log("Received file:\n" + JSON.stringify(req.files));

                    //add p for pics will do v for vids
                    videoID = "v" + path.basename(req.files.mediaFile.path);
                    tmp_path = req.files.mediaFile.path;
                    //targetPathSmall = './public/img/challenges/' + videoID;
                    //videoConvert = req.files.mediaFile.name;


                    var video = req.files.mediaFile;
                    var s3Headers = {
                        'Content-Type': video.type,
                        'Content-Length': video.size,
                        'x-amz-acl': 'public-read'
                    };
                            //console.log('stdout:', stdout, targetPathSmall)
                            s3.putStream(tmp_path, videoID, s3Headers, function(err, s3response){
                                //handle, respond
                                if(err) { 
                                    console.log(err);
                                } else {
                                    s3response.resume();
                                    console.log("saved to S3");
                                    //console.log(s3response);
                                    cb(null, videoID);
                                }
                            });
                        } else {
                            videoID = "";
                            cb(null, videoID);
                        }

                    },

Solution

  • You're passing a String as the first argument to s3.putStream where a Stream is expected.

    See https://github.com/Automattic/knox/blob/master/lib/client.js#L398

    You should be using it like so:

    s3.putStream(inputStream, videoID, s3Headers, function(err, s3response) {
    

    You may be able to populate inputStream by reading the content of req.mediaFile as a stream, if possible. It's not easy to tell from the code.

    Good luck!