node-webshot icon indicating copy to clipboard operation
node-webshot copied to clipboard

How to upload to s3 using stream

Open solshark opened this issue 10 years ago • 7 comments

Hi,

Could you please advice how to use stream to upload screenshot to s3 using aws-sdk? Files from filesystem I uploaded in this way:

fileStream.on('open', function () {
  var s3 = new AWS.S3();
  s3.putObject({
    Bucket: 'mybucketname',
    Key: 'myarchive.tgz',
    Body: fileStream
  }, function (err) {
    if (err) { throw err; }
  });

Not very familiar with streams. Thx.

solshark avatar Dec 09 '14 14:12 solshark

Try this:

var Readable = require('stream').Readable;
var webshot = require('webshot');
var aws = require('aws-sdk');

webshot('google.com', function(err, stream) {
    if(err) return console.log(err);

    var s3 = new aws.S3({ params: { Bucket: 'my-bucket', Key: 'google.png' } });

    var readableStream = new Readable().wrap(stream);

    s3.upload({ Body: readableStream }, function(err, data) {
        if(err) return console.log(err);

        console.log(data);
    });
});

Leh2 avatar Feb 10 '15 20:02 Leh2

Hey @Leh2 - thank you for this hin. It helped me to create an image and directly uploaded to S3 - thanks! Is there a way you can modify your answer to show me how this would work with a PDF instead of an image ? The image is working fine but when changing the key to google.pdf the file is not able to get opened as a PDF.

TimoRuetten avatar May 23 '16 17:05 TimoRuetten

Heyy @Leh2 - Can you please help me in giving options while calling the webshot because I need to define the screenSize, shotSize as well as customCSS

I'm also a novice in stream related stuffs... Thank You in Advance

var cssCustom = "body{zoom:3}";
var mOptions = {
    siteType: 'html',
    screenSize: {
        width: 960
    },
    customCSS: cssCustom,
    shotSize: {
        width: 960,
        height: 'all'
    },
    userAgent: 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_2 like Mac OS X; en-us)' +
        ' AppleWebKit/531.21.20 (KHTML, like Gecko) Mobile/7B298g'
};

webshot(globalHTML, mOptions, function(err, stream) {
        if (err) return console.log(err);
        var s3 = new aws.S3({
            params: {
                Bucket: 'myBucket',
                Key: 'someFolder/google.png'
            }
        });
       var readableStream = new Readable().wrap(stream);

        s3.upload({
            Body: readableStream
        }, function(err, data) {
            if (err) return console.log(err);

            console.log(data);
        });
    });

priyesh2609 avatar Aug 04 '16 19:08 priyesh2609

For some reason my images end up being 0bytes.

...
webshot('http://google.com', (err, stream)=>{
      if (err) {
        console.log(err);
        reject(err);
      }

      let readableStream = new Readable().wrap(stream);
      s3Client.upload({
        Key: key,
        Body: readableStream,
        Bucket: process.env.S3_BUCKET,
      }, (error) => {
        if (error) {
          console.error(error);
           reject(error);
        }
         resolve({
          key,
        });
      });
...

zaklampert avatar Apr 24 '17 22:04 zaklampert

Hello , I am facing some issue while streaming the data from webshot. Actually when i try to read SVG content i am facing this issue. Kindly share if anybody face this

_stream_readable.js:992 stream.pause(); ^

TypeError: stream.pause is not a function at Stream.stream.on (_stream_readable.js:992:14) at Stream.emit (events.js:189:13) at Stream.EventEmitter.emit (domain.js:441:20) at Socket. (/app/node_modules/webshot/lib/webshot.js:259:9) at Socket.emit (events.js:189:13) at Socket.EventEmitter.emit (domain.js:441:20) at addChunk (_stream_readable.js:288:12) at readableAddChunk (_stream_readable.js:269:11) at Socket.Readable.push (_stream_readable.js:224:10) at Pipe.onStreamRead [as onread] (internal/stream_base_commons.js:145:17)

AshokRamaraja avatar Dec 20 '18 13:12 AshokRamaraja

Lograste solucionar tu error

axelgonzales avatar Apr 17 '19 21:04 axelgonzales

Me enfrento al siguiente error si alguien : spawn /var/task/node_modules/phantomjs-prebuilt/lib/phantom\bin\phantomjs.exe ENOENT

axelgonzales avatar Apr 17 '19 21:04 axelgonzales