Search code examples
node.jsamazon-s3uploadknox-amazon-s3-client

Is there a way to upload to S3 from a url using node.js?


I found this question, but it doesn't seem to answer my question as I think it's still talking about local files.

I want to take, say, and imgur.com link and upload it to S3 using node. Is knox capable of this or do I need to use something else?

Not sure where to get started.


Solution

  • I’m not using knox but the official AWS SDK for JavaScript in Node.js. I issue a request to a URL with {encoding: null} in order to retrieve the data in buffer which can be passed directly to the Body parameter for s3.putObject(). Here below is an example of putting a remote image in a bucket with aws-sdk and request.

    var AWS = require('aws-sdk');
    var request = require('request');
    
    AWS.config.loadFromPath('./config.json');
    var s3 = new AWS.S3();
    
    function put_from_url(url, bucket, key, callback) {
        request({
            url: url,
            encoding: null
        }, function(err, res, body) {
            if (err)
                return callback(err, res);
    
            s3.putObject({
                Bucket: bucket,
                Key: key,
                ContentType: res.headers['content-type'],
                ContentLength: res.headers['content-length'],
                Body: body // buffer
            }, callback);
        })
    }
    
    put_from_url('http://a0.awsstatic.com/main/images/logos/aws_logo.png', 'your_bucket', 'media/aws_logo.png', function(err, res) {
        if (err)
            throw err;
    
        console.log('Uploaded data successfully!');
    });