mercredi 1 juillet 2015

Downloading file to AWS s3 using nodejs

I'm trying to use npm file download to download an image I scraped off a site to my aws s3 server. I keep getting the following:

if (!self.body) throw new Error('params.Body is required'); ^ Error: params.Body is required

I was wondering if anyone could point me in the right direction of fixing my code. Below is the code in entirety:

//required node modules
var request = require('request');
var cheerio = require('cheerio');
var fs      = require('fs');
var s3      = require('s3');
var AWS     = require('aws-sdk');

var url = 'http://ift.tt/1Gg65Lq'; //url of site

AWS.config.loadFromPath('./config.json');
var s3bucket = new AWS.S3({params: {Bucket: 'crawlerdemo'}});

request(url, function(err, resp, body){
  $ = cheerio.load(body);
  links = $('img'); //get the image tags
  $(links).each(function(i, link){ //go through every link on page
    image_path = $(link).attr('src');
    var tmp = image_path.lastIndexOf('/');
    var filename = image_path.substring(tmp+1, image_path.length); //set file name
    var download_tmp = download(url, filename, function(){});
    var params = {Key: filename,Body: download_tmp};
    s3bucket.upload(params, function(err, data){
      if(err){
        console.log("Error: ",err);
      }
    });
  });
});

//download method
var download = function(uri, filename, callback){
  request.head(uri, function(err, res, body){
    request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
  });
};



Aucun commentaire:

Enregistrer un commentaire