bornytm
bornytm

Reputation: 813

Moving to s3 storage with node.js

I'm trying to get ready to move a node.js application I've been working on into a production environment (I'm using Heroku). Users add images to the site via url. Right now they are just saved on the server- I'd like to move the storage to s3 but am having some difficulties.

The idea is to save the image to disk first and then upload it, but I'm struggling to find a way to be notified when the file has finished writing to the disk. It seems like it doesn't use the typical node callback style.

Here is the code as it is now. I'm using the request node module which may be complicating things rather than simplifying them:

    requestModule(request.payload.url).pipe(fs.createWriteStream("./public/img/submittedContent/" + name));

                // what do I do here?

                fs.readFile("./public/img/submittedContent/" + name, function(err, data){
                if (err) { console.warn(err); }
                else {
                    s3.putObject({
                        Bucket: "submitted_images",
                        Key: name,
                        Body: data
                     }).done(function(s3response){
                        console.log("success!");
                        reply({message:'success'});
                    }).fail(function(s3response){
                        console.log("failure");
                        reply({message:'failure'});
                    });
                }
            });

Any advice would be appreciated. Thanks!

Upvotes: 0

Views: 372

Answers (2)

Jon Mountjoy
Jon Mountjoy

Reputation: 4526

Unless you're modifying the image, you shouldn't upload to Heroku - but rather directly to S3.

See Direct to S3 File Uploads in Node.js

Upvotes: 0

levi
levi

Reputation: 25091

Try listening for the finish event on the writable stream:

requestModule(request.payload.url).pipe(fs.createWriteStream("./public/img/submittedContent/" + name)).on('finish', function(){
    // do stuff with saved file
});

Upvotes: 2

Related Questions