Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 490
Bogac Avatar asked May 03 '18 06:05

Bogac


People also ask

What is the difference between onCall and onRequest?

onRequest creates a standard API endpoint, and you'll use whatever methods your client-side code normally uses to make. HTTP requests to interact with them. onCall creates a callable. Once you get used to them, onCall is less effort to write, but you don't have all the flexibility you might be used to.

Can I store images in Firebase?

The Firebase SDKs for Cloud Storage add Google security to file uploads and downloads for your Firebase apps, regardless of network quality. You can use our SDKs to store images, audio, video, or other user-generated content.

What is Storage bucket in Firebase?

Cloud Storage for Firebase lets you upload and share user generated content, such as images and video, which allows you to build rich media content into your apps. Your data is stored in a Google Cloud Storage bucket — an exabyte scale object storage solution with high availability and global redundancy.

What is Firebase Cloud Functions used for?

Cloud Functions for Firebase is a serverless framework that lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests. Your JavaScript or TypeScript code is stored in Google's cloud and runs in a managed environment.


2 Answers

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:

enter image description here

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.

like image 84
LundinCast Avatar answered Sep 28 '22 06:09

LundinCast


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';
like image 29
amit ghosh Avatar answered Sep 28 '22 04:09

amit ghosh