firebase function get download url after successfully save image to firebase cloud storage

梦想的初衷 提交于 2019-11-28 13:57:42

You're not showing all the code here (for example, how you initialized the Firebase Admin SDK to create admin). So I'm going to assume that you used the project default credentials to initialize it like this:

import * as admin from 'firebase-admin'
admin.initializeApp()

This isn't sufficient to to be able to use getSignedUrl when reaching into the admin SDK to use Cloud Storage APIs. If you want to use getSignedUrl, you'll need to provide the credentials for a dedicated service account that you create in the console.

Assuming that you put those credentials in a file called yourServiceAccount.json in your functions folder, you should initialize the admin SDK with those credentials like this:

import * as serviceAccount from 'yourServiceAccount.json';
const adminConfig = JSON.parse(process.env.FIREBASE_CONFIG)
adminConfig.credential = admin.credential.cert(<any>serviceAccount)
admin.initializeApp(adminConfig);

Note that this is just taking the default project config from FIREBASE_CONFIG and adding the service account to them.

In order to get the import of a JSON file, you will also need to have a file (called typings.d.ts):

declare module "*.json" {
  const value: any;
  export default value;
}

Thanks @Doug Stevenson. I believe I found an answer.

  1. I solve deployment errors by recreating new project.

  2. I change my initialization to code below to solve unhandled rejection. Go to Service Acccounts under Project Settings in your firebase project and generate new private key under firebase admin sdk.

    const serviceAccount = require('./path/to/serviceaccount.json');
    admin.initializeApp({
      credential: admin.credential.cert(serviceAccount),
      databaseURL: '<ur database url>', // check it under service accounts
      storageBucket: '<ur storage bucket url>' //check it in script snippet require to add to your website.
    });
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!