Get Download URL from file uploaded with Cloud Functions for Firebase

后端 未结 23 1789
春和景丽
春和景丽 2020-11-22 01:19

After uploading a file in Firebase Storage with Functions for Firebase, I\'d like to get the download url of the file.

I have this :

...

return buck         


        
相关标签:
23条回答
  • 2020-11-22 01:47

    For those trying to use the token parameter to share the file and would like to use gsutil command, here is how I did it:

    First you need to authenticate by running: gcloud auth

    Then run:

    gsutil setmeta -h "x-goog-meta-firebaseStorageDownloadTokens:$FILE_TOKEN" gs://$FIREBASE_REPO/$FILE_NAME

    Then you can download the file with the following link:

    https://firebasestorage.googleapis.com/v0/b/$FIREBASE_REPO/o/$FILE_NAME?alt=media&token=$FILE_TOKEN

    0 讨论(0)
  • 2020-11-22 01:48

    For those who are using Firebase SDK andadmin.initializeApp:

    1 - Generate a Private Key and place in /functions folder.

    2 - Configure your code as follows:

    const serviceAccount = require('../../serviceAccountKey.json');
    try { admin.initializeApp(Object.assign(functions.config().firebase, { credential: admin.credential.cert(serviceAccount) })); } catch (e) {}
    

    Documentation

    The try/catch is because I'm using a index.js that imports other files and creates one function to each file. If you're using a single index.js file with all functions, you should be ok with admin.initializeApp(Object.assign(functions.config().firebase, { credential: admin.credential.cert(serviceAccount) }));.

    0 讨论(0)
  • 2020-11-22 01:50

    With the recent changes in the functions object response you can get everything you need to "stitch" together the download URL like so:

     const img_url = 'https://firebasestorage.googleapis.com/v0/b/[YOUR BUCKET]/o/'
    + encodeURIComponent(object.name)
    + '?alt=media&token='
    + object.metadata.firebaseStorageDownloadTokens;
    
    console.log('URL',img_url);
    
    0 讨论(0)
  • 2020-11-22 01:51

    If you're working on a Firebase project, you can create signed URLs in a Cloud Function without including other libraries or downloading a credentials file. You just need to enable the IAM API and add a role to your existing service account (see below).

    Initialize the admin library and get a file reference as your normally would:

    import * as functions from 'firebase-functions'
    import * as admin from 'firebase-admin'
    
    admin.initializeApp(functions.config().firebase)
    
    const myFile = admin.storage().bucket().file('path/to/my/file')
    

    You then generate a signed URL with

    myFile.getSignedUrl({action: 'read', expires: someDateObj}).then(urls => {
        const signedUrl = urls[0]
    })
    

    Make sure your Firebase service account has sufficient permissions to run this

    1. Go to the Google API console and enable the IAM API (https://console.developers.google.com/apis/api/iam.googleapis.com/overview)
    2. Still in the API console, go to the main menu, "IAM & admin" -> "IAM"
    3. Click edit for the "App Engine default service account" role
    4. Click "Add another role", and add the one called "Service Account Token Creator"
    5. Save and wait a minute for the changes to propagate

    With a vanilla Firebase config, the first time you run the above code you'll get an error Identity and Access Management (IAM) API has not been used in project XXXXXX before or it is disabled.. If you follow the link in the error message and enable the IAM API, you'll get another error: Permission iam.serviceAccounts.signBlob is required to perform this operation on service account my-service-account. Adding the Token Creator role fixes this second permission issue.

    0 讨论(0)
  • 2020-11-22 01:51

    Without signedURL() using makePublic()

    const functions = require('firebase-functions');
    const admin = require('firebase-admin');
    
    admin.initializeApp()
    var bucket = admin.storage().bucket();
    
    // --- [Above] for admin related operations, [Below] for making a public url from a GCS uploaded object
    
    const { Storage } = require('@google-cloud/storage');
    const storage = new Storage();
    
    exports.testDlUrl = functions.storage.object().onFinalize(async (objMetadata) => {
        console.log('bucket, file', objMetadata.bucket + ' ' + objMetadata.name.split('/').pop()); // assuming file is in folder
        return storage.bucket(objMetadata.bucket).file(objMetadata.name).makePublic().then(function (data) {
            return admin.firestore().collection('publicUrl').doc().set({ publicUrl: 'https://storage.googleapis.com/' + objMetadata.bucket + '/' + objMetadata.name }).then(writeResult => {
                return console.log('publicUrl', writeResult);
            });
        });
    });
    
    0 讨论(0)
  • 2020-11-22 01:52

    For those wondering where the Firebase Admin SDK serviceAccountKey.json file should go. Just place it in the functions folder and deploy as usual.

    It still baffles me why we can't just get the download url from the metadata like we do in the Javascript SDK. Generating a url that will eventually expire and saving it in the database is not desirable.

    0 讨论(0)
提交回复
热议问题