Question

I am having problems trying to do a CORS request from superagent to Amazon S3 for upload a file. First, I am asking to a node.js server for the policy. I return a JSON object like this:

{
    s3PolicyBase64: '',
    s3Signature: '',
    s3Key: '',
    s3ObjectKey: 'ftriana3185/inputData/input_fdc2f7f4b050c5884e5ac60a43bfc0d8ff26d549.csv' }

Then I try from superagent use the policy returned by node to upload a local file. My code looks like this:

it('GET /inputFiles/s3Credential', function(done) {
    var csvPath = './files/inputFileResource/countrylist.csv';
    var request = {};
    request.ext = 'csv';

    clientAgent.get(localPath + '/inputFiles/s3Credential').send(request).end(function(response) {
        var s3PolicyBase64 = response.body.s3PolicyBase64;
        var s3Signature = response.body.s3Signature;
        var s3Key = response.body.s3Key;
        var s3ObjectKey = response.body.s3ObjectKey;

        var request = clientAgent.post('bucket-name.s3.amazonaws.com')
            .type('form')
            .field('key', s3ObjectKey)
            .field('AWSAccessKeyId', s3Key)
            .field('acl', 'public-read')
            .field('policy', s3PolicyBase64)
            .field('signature', s3Signature)
            .attach('mycsv', csvPath).end(function(response){
                console.log(response);
            });
    });
});

I am sure that the problem is in the form that i am doing the request from superagent because i also have a html form that works fine. So, what is the correct form to use superagent for this purpose?

Était-ce utile?

La solution

I tried to do exactly that today, and found out that it fails with HTTP 400. I guess that superagent does not respect the precise form layout described in http://aws.amazon.com/articles/1434.

I recommend that you use the "form-data" module instead (https://github.com/felixge/node-form-data).

This worked for me:

var FormData = require('form-data');
var fs = require('fs');

...

it('should upload to S3 with a multipart form', function (done) {
    var policy = {/* your S3 policy */};
    var form = new FormData();
    form.append('AWSAccessKeyId', policy.AWSAccessKeyId);
    form.append('key', policy.key);
    form.append('policy', policy.policy);
    form.append('signature', policy.signature);
    form.append('file', fs.createReadStream('path/to/file'));
    form.submit('https://YOUR_BUCKET.s3.amazonaws.com/', function (err, res) {
        if (err) return done(err);
        res.statusCode.should.be.exactly(204);
        done();
    });
});
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top