I have a Firebase Storage trigger which is supposed to detect a video and then take the video file and then encode it using ffmpeg and upload new encoded files back to Firebase Storage. The problem is that for some very weird and unknown reason, the file is detected but when it is to be read or manipulated, or uploaded back to a different folder, it is not getting detected. I have tried every possible thing I could think of:
using os package to get the temp directory
using fs package in different ways, from making new directories to uploading the same file to the same folder using different names.
using specific getStorage import instead of admin.storage() etc.
The issues here are:
- The mkdir is not creating any directory and is neither throwing an error.
- The upload is also not bucket.file.download is also neither creating a directory nor downloading that file to any other directory.
- And I believe upload throws that error of no such file or directory, open + the file name
Here is the code snippet I am using:
const {onObjectFinalized} = require("firebase-functions/v2/storage");
const fs = require("fs");
const mkdirp = fs.promises.mkdir;
const path = require("path");
const os = require("os");
const logger = require("firebase-functions/logger");
const {setGlobalOptions} = require("firebase-functions/v2");
const admin = require("firebase-admin");
const serviceAccount = require("./serviceAccountKey.json");
// Set the maximum instances to 10 for all functions
setGlobalOptions({maxInstances: 10});
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
storageBucket: "bucket-name",
});
// eslint-disable-next-line max-len
exports.encodeVideo = onObjectFinalized({cpu: 2, maxInstances: 5, memory: 2, timeoutSeconds: 300}, async (event) => {
// ...
const fileBucket = event.data.bucket; // Storage bucket containing the file.
const filePath = event.data.name; // File path in the bucket.
const contentType = event.data.contentType; // File content type.
logger.log("contentType: " + contentType);
logger.log("filePath: " + filePath);
logger.log("fileBucket 2: " + fileBucket);
if (!contentType.startsWith("video/")) {
return logger.log("This is not a video.");
}
const parsedPath = path.parse(filePath);
const filenameWithoutExt = parsedPath.name;
logger.log("parsedPath: " + parsedPath);
logger.log("parsedPath.dir: " + parsedPath.dir);
logger.log("parsedPath.name: " + parsedPath.name);
logger.log("parsedPath.base: " + parsedPath.base);
logger.log("parsedPath.root: " + parsedPath.root);
logger.log("filenameWithoutExt: " + filenameWithoutExt);
logger.log("os.tmpdir(): " + os.tmpdir());
const tempLocalFile = path.join(os.tmpdir(), filePath);
const tempLocalDir = path.dirname(tempLocalFile);
try {
await mkdirp(tempLocalDir, {recursive: true});
logger.log("Temporary directory has been created ", tempLocalDir);
} catch (error) {
logger.log("COULD NOT CREATE Temporary directory ", error);
}
logger.log("Temporary directory has been created ", tempLocalDir);
logger.log("Temporary file ", tempLocalFile);
const bucket = admin.storage().bucket(fileBucket);
await bucket.file(filePath).download({destination: tempLocalFile});
//--- I believe this is what gives me that error. but I do not know why
await bucket.upload(filePath, {
destination: `blurred/${parsedPath.name}.mp4`,
});
console.log("Video encoded successfully");
logger.log("Image downloaded!");
});
