Using Puppeteer and AWS SDK
Puppeteer
Puppeteer is a perfect library to automate your actions within the browser.
Let’s install it first:
And then take a first screenshot:
In this example, we take a screenshot of the exemplary site and return the binary representation of the image. The last thing is to upload it to Amazon S3 or any S3-compatible storage.
Upload to S3
With aws-sdk
, it is fairly easy to do so. Let’s install it first:
And then we are ready to use it. Let’s for example, upload a screenshot to Cloudflare R2 instead of Amazon S3.
In this case, we used Cloudflare R2, but for Amazon S3, only the credentials and endpoint might be different.
After you upload a screenshot, you receive its URL in the result’s Location
attribute. You can return it in the response or do anything you need to do with it.
Using API
One more approach to uploading website screenshots to S3 is to save time and use the ready screenshot API.
ScreenshotOne API already has built-in functionality to upload screenshots to S3. It is scalable, reliable, fast, and uses CDN for storing screenshots.
You only need to configure your S3 access credentials in the dashboard and then specify a parameter in the request query.
Summary
Decide on which approach suits you best. If you already have all infrastructure ready, have plenty of time, and can afford to build your solution, I would make it. Otherwise, I would use a ready solution to save time and money.
You also might find useful on how to take screenshots with Puppeteer.
Have a nice day 👋