node-webshot
node-webshot copied to clipboard
How to upload to s3 using stream
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.
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);
});
});
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.
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);
});
});
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,
});
});
...
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.
Lograste solucionar tu error
Me enfrento al siguiente error si alguien : spawn /var/task/node_modules/phantomjs-prebuilt/lib/phantom\bin\phantomjs.exe ENOENT