I'm using sharp to convert images with a cloud function. Visual studio code, typescript.
I import it as:
import * as sharp from 'sharp';
I use it like this:
export const generateThumbs = functions.storage
.object()
.onFinalize(async object => {
const bucket = gcs.bucket(object.bucket);
const filePath = object.name;
const fileName = filePath.split('/').pop();
const bucketDir = dirname(filePath);
const workingDir = join(tmpdir(), 'thumbs');
const tmpFilePath = join(workingDir, 'source.png');
if (fileName.includes('thumb@') || !object.contentType.includes('image')) {
console.log('exiting function');
return false;
}
// 1. Ensure thumbnail dir exists
await fs.ensureDir(workingDir);
// 2. Download Source File
await bucket.file(filePath).download({
destination: tmpFilePath
});
// 3. Resize the images and define an array of upload promises
const sizes = [64, 128, 256];
const uploadPromises = sizes.map(async size => {
const thumbName = `thumb@${size}_${fileName}`;
const thumbPath = join(workingDir, thumbName);
// Resize source image
await sharp(tmpFilePath)
.resize(size, size)
.toFile(thumbPath);
// Upload to GCS
return bucket.upload(thumbPath, {
destination: join(bucketDir, thumbName)
});
});
// 4. Run the upload operations
await Promise.all(uploadPromises);
// 5. Cleanup remove the tmp/thumbs from the filesystem
return fs.remove(workingDir);
});
(code from fireship.io)
This is my package.json file
"dependencies": {
"@firebase/app": "^0.4.22",
"@firebase/app-types": "^0.4.7",
"@google-cloud/storage": "^4.1.0",
"@sendgrid/client": "^6.4.0",
"@sendgrid/contact-importer": "^6.4.0",
"@sendgrid/mail": "^6.4.0",
"@std/esm": "^0.26.0",
"@types/algoliasearch": "^3.34.5",
"@types/react": "^16.9.11",
"@types/react-dom": "^16.9.3",
"algolia-firebase-functions": "^3.2.0",
"algoliasearch": "^3.35.1",
"async.ensureasync": "^0.5.2",
"async.queue": "^0.5.2",
"bottleneck": "^2.19.5",
"debug": "^4.1.1",
"express": "^4.17.1",
"firebase": "^7.2.3",
"firebase-admin": "^8.7.0",
"firebase-functions": "^3.3.0",
"flag": "^4.4.0",
"fs-extra": "^8.1.0",
"geofirex": "0.0.6",
"images-downloader": "^1.0.3",
"lodash.chunk": "^4.2.0",
"react-redux": "^7.1.1",
"request": "^2.88.0",
"rxfire": "^3.8.8",
"rxjs": "^6.5.3",
"sharp": "^0.23.2",
"tmp": "^0.1.0"
},
Since I use the sharp package I get this message when I run: firebase deploy --only functions:myFunctionName
There was an unknown problem while trying to parse function triggers. Please ensure you are using Node.js v6 or greater.
I'm using node 12.3.1
Thanks for the help!