node.jsamazon-s3pkgcloud

What "streams and pipe-capable" means in pkgcloud in NodeJS


My issue is to get image uploading to amazon working. I was looking for a solution that doesnt save the file on the server and then upload it to Amazon.

Googling I found pkgcloud and on the README.md it says:

Special attention has been paid so that methods are streams and pipe-capable.

Can someone explain what that means and if it is what I am looking for?


Solution

  • Yupp, that means you've found the right kind of s3 library.

    What it means is that this library exposes "streams". Here is the API that defines a stream: http://nodejs.org/api/stream.html

    Using node's stream interface, you can pipe any readable stream (in this case the POST's body) to any writable stream (in this case the S3 upload).

    Here is an example of how to pipe a file upload directly to another kind of library that supports streams: How to handle POSTed files in Express.js without doing a disk write

    EDIT: Here is an example

    var pkgcloud = require('pkgcloud'),
          fs = require('fs');
    
    var s3client = pkgcloud.storage.createClient({ /* ... */ });
    
    app.post('/upload', function(req, res) {
      var s3upload = s3client.upload({
        container: 'a-container',
        remote: 'remote-file-name.txt'
      })
    
      // pipe the image data directly to S3
      req.pipe(s3upload);      
    });
    

    EDIT: To finish answering the questions that came up in the chat:

    req.end() will automatically call s3upload.end() thanks to stream magic. If the OP wants to do anything else on req's end, he can do so easily: req.on('end', res.send("done!"))