Search code examples
node.jsfile-uploadamazon-s3knox-amazon-s3-client

NodeJS: Uploading PDF to S3 via Knox; putFile returns 505


I'm trying to upload a PDF to an S3 bucket using the Knox library, but I keep getting 505 errors and the PDFs won't save. My code:

// all of this works well
var knox = require('knox');
var client = knox.createClient(require('../path/to/config.js').knox);

client.putFile('tmp/file', '/prefix/key',
  function(err, res) {
    if (err) {
      console.log("Error PUTing file in S3:", err);
    }

    console.log("S3 RESPONSE:", res.statusCode); // returns 505
  }
);

Anyone have any insight into what I'm doing wrong? I've also tried setting my own headers using client.put(..), but I got the same 505 response.


Solution

  • This isn't an answer per se, and I'm still unsure about the 505 response above, but the AWS SDK that Amazon puts out works great if anyone is having similar issues with Knox. The above just becomes:

    var aws = require('aws-sdk');
    aws.config.loadFromPath('./path/to/config.json');
    var s3 = new aws.S3();
    
    var params = { Bucket: 'your-bucket', 
                   Key: 'your-key', 
                   Body: fs.readFileSync('/path/to/file.pdf') };
    
    s3.putObject(params, function(err, data) {
      if (err) {
        console.log("Error PUTing file:", err);
      }
      console.log("S3 RESPONSE:", data);
    });