Skip to content

Instantly share code, notes, and snippets.

@homaily
Last active December 16, 2022 13:13
Show Gist options
  • Select an option

  • Save homaily/a432af95e5c3e72d3a584d435544bee0 to your computer and use it in GitHub Desktop.

Select an option

Save homaily/a432af95e5c3e72d3a584d435544bee0 to your computer and use it in GitHub Desktop.
Lambda download and cache gzipped file from s3
'use strict';
console.log('// loading function');
const aws = require('aws-sdk');
const s3 = new aws.S3({apiVersion: '2006-03-01'});
const gzip = require('zlib').createGunzip();
const fs = require('fs');
let isInitialised = false;
exports.handler = function(event, context, callback) {
if (!isInitialised)
return exports.init(event, context, callback);
// Do stuff here
}
exports.init = function(event, context, callback) {
const localFileName = '/tmp/chrome';
const localFile = fs.createWriteStream(localFileName);
// first check if the file have been downloaded before
if (fs.existsSync(localFileName)) {
isInitialised = true;
return exports.handler(event, context, callback); // call handler function to resume process
}
s3.getObject({
Bucket: process.env.BUCKET_NAME,
Key: process.env.FILE_NAME,
})
.createReadStream()
.pipe(gzip) // decompress compress file on the fly. No need for this line if file already decompressed
.pipe(localFile) // write to local /tmp directory
.on('end', function() {
isInitialised = true;
exports.handler(event, context, callback); // call handler function to resume process
})
.on('err', function(err) {
callback(err);
})
}
@homaily
Copy link
Copy Markdown
Author

homaily commented Sep 1, 2017

You need to define those environmental variables in your lambda function

  • BUCKET_NAME: the name of the bucket were the file is hosted (example: puppeteer_chrome_test)
  • FILE_NAME: the path to the file within the bucket (example: chrome_executable/file_name)

In the sample above the file will be downloaded to /tmp/chrome

@homaily
Copy link
Copy Markdown
Author

homaily commented Sep 1, 2017

Once the function run for the first time it will execute exports.init. Afterwards isInitialised will be cached in memory as true.

@metrue
Copy link
Copy Markdown

metrue commented Aug 28, 2018

how does puppeteer know your chrome is in /tmp/chrome?

@ahmic
Copy link
Copy Markdown

ahmic commented Sep 5, 2019

how does puppeteer know your chrome is in /tmp/chrome?

puppeteer.launch({executablePath: '/tmp/chrome'})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment