I'm working with Firebase cloud functions. I have written function which gets triggered on OnCreate event from Firebase Realtime database. Currently, this function successfully upload PDF document but I want a public URL to that uploaded document so that anybody can access and see the document.
index.js
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const PDFDocument = require("pdfkit");
const os = require("os");
const fs = require("fs");
const path = require("path");
admin.initializeApp();
exports.addMessage = functions.database.ref('/messages/{pushId}/')
.onCreate((snapshot, context) => {
const original = snapshot.val();
console.log(snapshot);
const bucket = admin.storage().bucket();
const filePath = "filename.pdf";
const tmpFilePath = path.join(os.tmpdir(), path.basename(filePath));
var pdf = new PDFDocument({
size: 'A4',
info: {Title: 'Tile of File', Author: 'Author'}
});
pdf.text('Emergency Incident Report');
pdf.pipe(
// TODO: figure out how / where to store the file
fs.createWriteStream( tmpFilePath )
).on('finish', function () {
console.log('PDF closed');
return bucket.upload(tmpFilePath, {
destination: 'resized-' + path.basename(filePath),
}).then((obj)=>{
console.log(obj);
const upFile = obj[0];
return upFile.getSignedUrl({ action: 'read', expires: '03-17-2025'}).then((urlUp)=>{
console.log(urlUp[0]);
});
});
});
pdf.end();
});
But this code logs below error in firebase
A Forbidden error was returned while attempting to retrieve an access token for the Compute Engine built-in service account. This may be because the Compute Engine instance does not have the correct permission scopes specified. Identity and Access Management (IAM) API has not been used in project 575455681031 before or it is disabled. Enable it by visiting **THIS URL** then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.
at GaxiosError (/user_code/node_modules/firebase-admin/node_modules/gaxios/build/src/common.js:17:9)
at Gaxios.<anonymous> (/user_code/node_modules/firebase-admin/node_modules/gaxios/build/src/gaxios.js:72:27)
at next (native)
at fulfilled (/user_code/node_modules/firebase-admin/node_modules/gaxios/build/src/gaxios.js:16:58)
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
I have tried following solutions but nothing works
Get Download URL from file uploaded with Cloud Functions for Firebase
Get public download URL from uploaded file in firebase storage with Java rest API
What I am trying to do?
I'm uploading a document to firebase storage with success after that done Im trying to get Download Url to that document so that I could save URL to firebase