Use external resource (media) in firebase cloud function without storage bucket - firebase

Let's say I have an invoice to generate and the invoice contains the company's logo and other images that would be used each time an invoice is generated.
Would I have to pull those images off the storage bucket? Or can I load it into the functions directory and use it from there, similar to how the serviceaccount.json file is loaded into functions?
EDIT: The way I'm thinking of is
functions/
├── index.js
├── image.jpg
And in index.js
const path = require("path");
const pathToImage = path.join(__dirname,'/image.jpg');

The Firebase/Cloud Functions deployment process packs up everything in your functions directory, and deploys it to the runtime for your functions. This means that you can indeed include any static files you need in the Cloud Functions deployment, and then access them from your code.
This is how you also include your service account, or what happens when you split your Cloud Functions code over multiple files that you then include into index.js.

Related

Firebase cloud functions folder not showing

I am using firebase with react native . Trying to write some firebase cloud functions. For that i have used firebase init . On console it gives a success message but the function folder is not been created in my project .
I have tried several times re initializing the firebase init but no result.
Try a new folder location. Create a new folder by the name of the backend wherever you'd like and 'CD' into it before you initialize it. i.e. users/haseeb/project and see if it then creates it.
Then, move the functions folder to the root level of the project.

Simplest code that can host html files in node.js through Cloud Functions hosting?

So, after reading a lot of documentation and tutorials, I realized that node.js can use "var http = require('http')" and "var fs = require('fs')" and some other code lines to serve html files but if it is hosted by Firebase Cloud Functions, that would not work because Firebase Cloud Functions works differently.
Can I get any simple code example how I can serve an html file index.html within node.js for Firebase Cloud Functions hosting?
You can simply host your index.html file by putting it in /public folder and deploy to Firebase Host. https://firebase.google.com/docs/hosting/quickstart
Steps are quick simple.
1. Install FirebaseCLI
2. Call $firebase init - to create the project and generate firebase.json, package.json, a public folder with .html files
3. modify the file and upload using the command $firebase deploy
This works for static (just HTML) or SPA (single page application)
Cloud Function is mostly used to build dynamic websites and API.

Using firebase project ID in command prompt

Command $firebase init functions after processing is not asking for my project id at firebase which should be initialized, instead it is using default project. Here is the picture
It is because you are reinitializing the firebase functions in the current directory i.e. learning chatbots.
If you intend to overwrite the existing firebase functions in this directory you can follow these instructions:
OR if don't want to overwrite existing files:
It will be better if you create a new directory and then initialize firebase functions there.
Make a new directory outside of "learning chatbots" directory and
try initializing there.

Firebase functions redeploy utility scripts

I hit following problem working with firebase functions, I split functionality into different files, few of them have only custom utility logic.
The thing is that with firebase CLI I only can upload separate functions with :
firebase deploy --only functions:someFunction
But in this case the utility file which function someFunction uses does not get uploaded. To refresh logic in separate files I need to redeploy all the functions by executing :
firebase deploy functions
and I hit deployment limit just in few deploys.
Is there any way I can redeploy a separate file to the server?
UPD
By utility file I mean files that contain logic and used in functions. Say we have function someFunction in the index.js that goes like this :
const commons = require('./commons');
exports.someFunction = function() {
common.sayHello();
}
In this case commons is a plain javascript file (commons.js) that has utility functions, other words - utility file.
exports.sayHello = function() {
console.log('hello!');
}
This is exactly the file I would like to redeploy separately.
It's not possible to upload only a single file when when deploying functions. The entire contents of the functions folder (except node_modules) is replaced for every function at every deployment, even if you are deploying just one function at a time.

Assests folder use under Firebase Cloud Function project

I'd like to have an assets folder (and maybe it also has in terms some subfolders) under functions folder which contains several image files. Depending on database action I'd like to copy some or all contents of that assets folder (or one of its subfolder) to Firebase Storage location designated with that particular db action. (or under temp folder for editing prior to sending them to Cloud Storage location)
Is this possible? I can't find any relevant information.
I know I can put those assets under Firebase Storage in the first place, I just want to keep them under functions in terms of keeping my project structure clean. Also, any operation in Cloud Function area of Storage is per file, no bulk folder ops.
UPDATE:
Indeed it is possible, one needs to use correct file/folder location in Cloud Functions while using a custom sub-folder. I'll write an answer later when I'll have free time.
You can indeed deploy an assets folder along with your function code. For example, consider the following directory structure:
functions
│ index.js
│ package.json
│
└───assets
│ file_1.png
│ file_2.png
│
└───subfolder1
│ file_3.png
│ file_4.png
│ ...
Running the firebase deploy ... command within the functions folder will deploy the whole tree. Then, from your exported function in index.json, you'll be able to access your assets folder content like you'd usually do in Nodejs.
One thing to take into account with this approach though is that there are resource limits applied to Cloud Functions, specifically on deployment size:
Depending on the size of your picture this may be a problem. And note that, even if the total size is within the limits, the bigger it is the longer it will take to deploy when scaling up.
I tried the above solution but falied
you can convert your files in base64 and top of your function you can convert the bas64 file to your files and save it in os.tmpdir() and use it anywhere in your function.
Here is the example of how I put my font in cloud functions for later use
function writeFontsInTempDir() {
const banglaFont = 'Your base64'
fs.writeFileSync(path.join(os.tmpdir(), 'bangla_font.ttf'), banglaFont, {encoding: 'base64'});
const englishFont= 'Your base64'
fs.writeFileSync(path.join(os.tmpdir(), 'english_font.ttf'), englishFont, {encoding: 'base64'});
}
for later use, you can just call
const bangla_font_path = path.join(os.tmpdir(), 'bangla_font.ttf');
const english_font_path = path.join(os.tmpdir(), 'english_font.ttf');
You will need these imports
import * as os from 'os';
import * as path from 'path';

Resources