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

前端 未结 4 909
失恋的感觉
失恋的感觉 2020-12-07 00:59

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

相关标签:
4条回答
  • 2020-12-07 01:14

    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.

    0 讨论(0)
  • 2020-12-07 01:14

    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;
        }
    });
    
    0 讨论(0)
  • 2020-12-07 01:33

    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;
    }
    
    0 讨论(0)
  • 2020-12-07 01:33

    You can listen to a specific path with

     exports.generateThumbnail = functions.storage.object('profile').onFinalize(async object => {
    
    })
    
    0 讨论(0)
提交回复
热议问题