getSignedUrl when using cloud functions

こ雲淡風輕ζ 提交于 2020-07-23 06:45:07

问题


I am trying to get image url so that I can pass it as src in my html. I created functions and would like to send the url in response. I tried the below but I keep getting

Error: Cannot sign data without client_email.

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';

export const getPicURL = functions.https.onRequest(
    (request, response) => {
        const storageBucket = admin
            .storage()
            .bucket('gs://my-app.appspot.com');

        const fileName = 'my-app/pic1.jpg';
        const tomorrow = new Date(
            new Date().getTime() + 24 * 60 * 60 * 1000
        );

        const signedURL = storageBucket
            .file(fileName)
            .getSignedUrl({ action: 'read', expires: tomorrow });

        signedURL
            .then((data) => {
                response.send(data[0]);
            })
            .catch((err) => {
                console.log('My Error', err);
                response.sendStatus(500).send(err);
            });
    }
);

I feel like I am missing configuration step but I don't know where to add these properties


回答1:


The issue I had is with service account. I was not initializing the app with proper config. I could find the details in https://console.firebase.google.com/ >> Project >> Settings >> Service Account >> Firebase Admin SDK

Instead of just admin.initializeApp(); we need to do the following

var admin = require("firebase-admin");

var serviceAccount = require("path/to/serviceAccountKey.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://my-app.firebaseio.com"
});


来源:https://stackoverflow.com/questions/62850467/getsignedurl-when-using-cloud-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!