使用云功能时的getSignedUrl [英] getSignedUrl when using cloud functions

查看:180
本文介绍了使用云功能时的getSignedUrl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取图像URL,以便可以在HTML中将其作为src传递.我创建了函数,并希望发送该URL作为响应.我尝试了以下内容,但我不断得到

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

错误:没有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

推荐答案

我遇到的问题与服务帐户有关.我没有使用正确的配置来初始化应用程序.我可以在 https://console.firebase.google.com/>>中找到详细信息;项目>>设置>>服务帐户>> Firebase Admin SDK

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

我们需要执行以下操作,而不只是admin.initializeApp();

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"
});

这篇关于使用云功能时的getSignedUrl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆