Download file from url and upload it to AWS S3 without saving - node.js

前端 未结 3 1752
天命终不由人
天命终不由人 2020-12-23 20:52

I\'m writing an application which downloads images from a url and then uploads it to an S3 bucket using the aws-sdk.

Perviously I was just downloading images and sav

3条回答
  •  天涯浪人
    2020-12-23 21:20

    Here's some javascript that does this nicely:

        var options = {
            uri: uri,
            encoding: null
        };
        request(options, function(error, response, body) {
            if (error || response.statusCode !== 200) { 
                console.log("failed to get image");
                console.log(error);
            } else {
                s3.putObject({
                    Body: body,
                    Key: path,
                    Bucket: 'bucket_name'
                }, function(error, data) { 
                    if (error) {
                        console.log("error downloading image to s3");
                    } else {
                        console.log("success uploading to s3");
                    }
                }); 
            }   
        });
    

提交回复
热议问题