Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do pass-thru uploads to s3 on Heroku with node.js?

Here's my current code. I'm using express and knox, and I don't think I'm doing anything unusual, but s3.putFile is responding 400 status code but null error, and the file does not get uploaded.

var express = require('express');
var knox = require('knox');

var app = express();
app.use(express.bodyParser());

var s3 = knox.createClient({
    key: process.env.AWS_ACCESS_KEY_ID,
    secret: process.env.AWS_SECRET_ACCESS_KEY,
    bucket: process.env.S3_BUCKET_NAME
});

app.post('/upload', function(req, res, next) {
    var photo = req.files.photo;
    var s3Headers = {
      'Content-Type': photo.type,
      'x-amz-acl': 'public-read'
    };

    s3.putFile(photo.path, photo.name, s3Headers, function(err, s3response){
      //handle, respond
    });
});

This same code works fine even from the cloud9 online editor/debugger, just not from Heroku. I'm guessing it has something to do with the "ephemeral file system", but that's just a guess. However I was able to get s3 pass-thru uploads to work on Heroku in Clojure using noir and weavejester's aws sdk, so it must be possible in node as well.

like image 843
Dax Fohl Avatar asked Nov 03 '22 13:11

Dax Fohl


1 Answers

I stumbled across a fix—once I added the following to package.json, it started working. Heroku defaults to 0.4 otherwise.

"engines": {
  "node": "0.8.x"
}
like image 104
Dax Fohl Avatar answered Nov 09 '22 09:11

Dax Fohl