Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to scope Firebase functions to a folder inside a storage bucket?

I have tested Firebase functions for storage successfully. However, I havn't seen anywhere a hint how to only invoke the function when a file is added into a folder inside my bucket. The only hint I have seen about scoping the function is for different buckets here.

Is it possible to scope the function to a folder inside my bucket , if yes how? Or would I need to have multiple buckets instead of folders to separate different tasks.

like image 308
TheeBen Avatar asked Jul 30 '17 20:07

TheeBen


People also ask

How do I upload folders to Firebase Storage?

There is no way to upload an entire folder to Cloud Storage for Firebase in one go. You will have to upload the individual files in the folder. The is no concept of an empty folder in Cloud Storage for Firebase. Folders only exist by the fact that they have files in them.

What is a Firebase Storage bucket?

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.

How do I find the path of a file in Firebase Storage?

If you want the file path on firebase storage to access it via storage reference and not URL, you can use taskSnapshot. getStorage().

What can Firebase functions do?

Firebase gives mobile developers access to a complete range of fully managed mobile-centric services including analytics, authentication and Realtime Database. Cloud Functions rounds out the offering by providing a way to extend and connect the behavior of Firebase features through the addition of server-side code.

What is FireStore function in Firebase SDK?

Cloud Firestore function triggers The Cloud Functions for Firebase SDK exports a functions.firestore object that allows you to create handlers tied to specific Cloud Firestore events. Note: Cloud Firestore events will trigger only on document changes.

Can I upload and download files from Firebase Cloud Functions?

The chapter entitled Firebase Cloud Functions introduced the basic concepts of working with Cloud Functions and Firebase Cloud Storage, including uploading and downloading files from within a cloud function.

What is Firebase Storage in Android?

Firebase Storage is an object storage service you can access via Google Cloud Platform. When using Google Firebase Storage, you can access files through references, easily upload files, and also monitor progress with tasks. You can also use cloud functions to automate backend code, as well as use UI libraries to authenticate app users.

What is the firebase architecture?

The Firebase Architecture is made of several components, which work together to provide a framework and infrastructure for developing and hosting web and mobile applications. Firebase Cloud Storage —also called Firebase File Storage, is an object storage service offered on Google Cloud Platform.


3 Answers

firebaser here

There is currently no way to trigger Cloud Functions only for writes in a specific folder in Cloud Storage. If you want to limit the triggering to a subset of the files in your project, putting them in a separate bucket is currently the only way to accomplish that.

As a workaround, you can write metadata about the image to a supported database (Realtime Database or Cloud Firestore), and use that to trigger a Cloud Function that transforms the file. This is what I usually do, as it also allows me to capture the metadata in a format that can be queried.

like image 133
Frank van Puffelen Avatar answered Oct 17 '22 10:10

Frank van Puffelen


You may check inside the function. Get the "filePath" or "fileDir" on your function and check if it is the folder you want.

const path = require('path');
const filePath = event.data.name;
const fileDir = path.dirname(filePath);

//if you want to check the posts folder only then:

if (fileDir != 'posts') {
    console.log('This is not in post folder');
    return null;
}
like image 23
Er.Se Avatar answered Oct 17 '22 10:10

Er.Se


Please note that Google Cloud Storage works on a flat filesystem. So practically there are no directories. If you're storing a file like /users/profile_pictures/photo.jpg it is basically all part of the file name. So in reality, there are no directories. There are just files. Which is why there cannot be a trigger on a directory per se. Of course you can work that around by checking the name of the file itself and see whether its start matches a particular string or not.

export const generateThumbnailTrigger = functions.storage.object().onFinalize(async (object) => {
    const filePath = object.name;
    if (filePath?.startsWith('temp/')) {
        console.log('start doing something with the file');
    } else {
        return false;
    }
});
like image 5
Muhammad bin Yusrat Avatar answered Oct 17 '22 10:10

Muhammad bin Yusrat