I’m working on a project using React and Firebase. I’m working with Firebase emulator and I want to get the getDownloadURL out of a an image thats uploaded to Firebase storage so that i can add it to a corresponding image document. I tried using this code which is inside an onFinalize
cloud function:
const newFile = admin
.storage()
.bucket(object.bucket)
.file(object.name);
const config = { action: "read", expires: "01-01-2035" };
const downloadURLs = await newFile.getSignedUrl(config);
const downloadURL = downloadURLs[0];
const images = firestore.collection("images");
return images.add({
imageURL: imageLink,
downloadURL: downloadURL,
});
I get the error:
Cannot sign data without
client_email
.
I learnt that this means i need a service account and then i created one, placed the file in my functions folder and ran the following code:
export GOOGLE_APPLICATION_CREDENTIALS="functions/serviceAccount.json"
firebase emulators:start --project=demo-project
and i got the following error:
⚠ functions: Failed to initialize and load triggers. This shouldn't happen: Failed to read credentials from file functions/serviceAccount.json: Error: ENOENT: no such file or directory, open 'functions/serviceAccount.json'
I then went on to use the service account thats specified in my Firebase settings page, placed it in my environment variables and ran the functions like so…
initializeApp({
serviceAccountId: "demo-serviceAccount",
apiKey: "demo-key",
authDomain: "demo-domain",
projectId: "demo-project",
storageBucket: "demo-project.appspot.com",
appId: "demo-appId",
messagingSenderId: "demo-id",
measurementId: "demo-measurementId",
})
And this didn’t work:
Request to function failed: Error: socket hang up
This apparently is a Mac issue but when i applied the fix, nothing changed.
I’ve seen some comments mention that .json service accounts are not safe. What is best for an emulator?
Any help would be appreciated.