0

I have html file and it stored in AWS S3. I already read that html content file with NodeJS AWS-SDK function(getObject) and it works very well then give me a data. The question is, how to get "src" url from that html data? and how to replace it with new url?

this my example code, I run it in cmd windows :

var AWS = require('aws-sdk');

AWS.config.loadFromPath('./config.json');
var s3 = new AWS.S3();
var params = {Bucket: 'myStoreName/content', Key: 'index.html'};

s3.getObject(params, function(err, data) {
  if (err) {
    console.log(err, err.stack);
  }
  else {
    var html = data.Body.toString();
    console.log(html);
  }    
});

The result from code above is :

<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        <h1>Hello World</h1>
        <p>This is sample of test</p>
        <img src="./myimage.jpg" />
    </body>
</html>

All I want just replce src url to be src="cid:unique@kreata.ee". Is there anyone know how to solve it? is there other way? thankyou for any help

angvee
  • 257
  • 2
  • 16

1 Answers1

2

You might want to use some parser for this.

Cheerio is my choice.

var AWS = require('aws-sdk');
var cheerio = require('cheerio');

AWS.config.loadFromPath('./config.json');
var s3 = new AWS.S3();
var params = {Bucket: 'myStoreName/content', Key: 'index.html'};

s3.getObject(params, function(err, data) {
  if (err) {
    console.log(err, err.stack);
  }
  else {
    var $ = cheerio.load( data.Body.toString() );
    $('body').find('img').attr('src', 'SRC_VALUE_TO_SET');
        console.log( $.html() );
  }    
});

Hope that helps.

Arjun S Kumar
  • 376
  • 3
  • 12
  • thanksss! it works. But one question again, how if I have more ? and it must have different src. – angvee Sep 07 '16 at 07:58
  • If you've more img elements you will have to use a parent element to select them separately if you've access to the HTML you pass. FYI: Cheerio have selectors similar to jQuery. – Arjun S Kumar Sep 07 '16 at 08:27
  • Many thanks for your help. I have figure out and I use each(function(index, element)) to loop all img. – angvee Sep 07 '16 at 09:07