I am trying to follow an Image resizer tutorial in firebase. I tried rewriting the code in plain NodeJS syntax, and borrowed some ideas from other sources. Attached below is my final code in index.js
const functions = require('firebase-functions')
const { Storage } = require('@google-cloud/storage');
const projectId = "REDACTED INFO"
let gcs = new Storage ({
projectId
});
const os = require('os');
const path = require('path');
const sharp = require('sharp');
const fs = require('fs-extra');
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
exports.onFileChange = functions.storage.object().onFinalize(async object => {
const bucket = gcs.bucket(object.bucket);
const filePath = object.name;
const fileName = filePath.split('/').pop();
const bucketDir = path.dirname(filePath);
const workingDir = path.join(os.tmpdir(), 'thumbs');
// const tmpFilePath = path.join(workingDir, 'source.png');
const tmpFilePath = path.join(workingDir, fileName);
//const tmpFilePath = path.join(os.tmpdir(), path.basename(filePath));
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 = path.join(workingDir, thumbName);
// Resize source image
await sharp(tmpFilePath)
.resize(size, size)
.toFile(thumbPath);
// Upload to GCS
return bucket.upload(thumbPath, {
destination: path.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);
});
exports.onFileDelete = functions.storage.object().onDelete(event => {
console.log(event);
console.log('We deleted a file, exit...')
return;
});
However, when I tried uploading an image, I keep getting these error and warnings in the firebase console logs.
Error: Anonymous caller does not have storage.objects.get access to the Google Cloud Storage object.
at new ApiError (/srv/node_modules/@google-cloud/common/build/src/util.js:59:15)
at Util.parseHttpRespMessage (/srv/node_modules/@google-cloud/common/build/src/util.js:161:41)
at Util.handleResp (/srv/node_modules/@google-cloud/common/build/src/util.js:135:76)
at Duplexify.requestStream.on.on.res (/srv/node_modules/@google-cloud/storage/build/src/file.js:823:31)
at emitOne (events.js:116:13)
at Duplexify.emit (events.js:211:7)
at emitOne (events.js:116:13)
at DestroyableTransform.emit (events.js:211:7)
at onResponse (/srv/node_modules/retry-request/index.js:200:19)
at PassThrough.<anonymous> (/srv/node_modules/retry-request/index.js:152:11)
and
MetadataLookupWarning: received unexpected error = URL is not defined code = UNKNOWN
Does anyone know what steps am I missing out on? Do inform me if more information is needed. Thanks.