使用nodejs将文件下载到AWS s3

时间:2015-07-01 18:34:34

标签: javascript node.js amazon-web-services amazon-s3

我正在尝试使用npm file download将我从网站上抓取的图像下载到我的aws s3服务器上。我一直得到以下内容:

  

if(!self.body)抛出新错误('params.Body is required');                             ^   错误:params.Body是必需的

我想知道是否有人能指出我正确修复我的代码的方向。以下是完整的代码:

//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 = 'https://medium.com/@benjaminhardy/8-things-every-person-should-do-before-8-a-m-cc0233e15c8d'; //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);
  });
};

0 个答案:

没有答案