成功将图像保存到 firebase 云存储后,firebase 函数获取下载 url

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

我在将图像保存到云存储后无法在 firebase 函数中下载 url。下面是我在 typescript 中的 firebase http 函数,用于将 base64 字符串保存到 firebase 云存储中的 jpeg。当我记录结果时,它总是空的。我按照此 link 获取下载 url “”。但它给了我以下错误:“SigningError: Identity and Access Management (IAM) API has not been used in project 81673989436 before or it is disabled. " 并且找不到可遵循的简单示例。我的计划是更新我的 firestore table,一旦上传到云存储。

export const publishImage = functions.https.onRequest((req, res) => {
// Convert the base64 string back to an image to upload into the Google 
// Cloud Storage bucket
const base64EncodedImageString=req.body.image.replace(/^data:image\/jpeg;base64,/, "");
const mimeType = 'image/jpeg';
const randomFileName = crypto.randomBytes(16).toString('hex') + '.jpeg';    
const imageBuffer = new Buffer(base64EncodedImageString, 'base64');

const bucket = admin.storage().bucket();

// Upload the image to the bucket
const file = bucket.file('images/' + randomFileName);
file.save(imageBuffer, {
    metadata: { contentType: mimeType },
}).then(result => {
    console.log(result);
    file.makePublic();
    file.getSignedUrl({
        action: 'read',
        expires: '03-09-2491'
    }).then(urls => {
        console.log(urls[0]);
    })
});
return res.status(200).send("NOT WORKING");    
})

firebase控制台报错如下。即使有错误,我也可以将图像保存在存储中。

SigningError: Permission iam.serviceAccounts.signBlob is required to perform this operation on service account projects/vmsystem-4aa54/serviceAccounts/vmsystem-4aa54@appspot.gserviceaccount.com. at /user_code/node_modules/@google-cloud/storage/src/file.js:1715:16 at Request._callback (/user_code/node_modules/@google-cloud/storage/node_modules/google-auto-auth/index.js:356:9) at Request.self.callback (/user_code/node_modules/@google-cloud/storage/node_modules/request/request.js:186:22) at emitTwo (events.js:106:13) at Request.emit (events.js:191:7) at Request. (/user_code/node_modules/@google-cloud/storage/node_modules/request/request.js:1163:10) at emitOne (events.js:96:13) at Request.emit (events.js:188:7) at IncomingMessage. (/user_code/node_modules/@google-cloud/storage/node_modules/request/request.js:1085:12) at IncomingMessage.g (events.js:292:16)

如何下载 url?

我越来越糊涂了。现在,我坚持在更改后更新功能。 下面是我对管理员的初始化。

 import * as admin from 'firebase-admin';
 const serviceAccount = require('./../service_account.json');

 try {   
 // admin.initializeApp(functions.config().firebase); // initial
 admin.initializeApp({
     credential: admin.credential.cert(serviceAccount),
     databaseURL: "https://vmsystem-4aa54.firebaseio.com"
 });
 } catch(e) {}
 import * as simpletest from './simpletest';
 export const testsomething = simpletest.helloWorld;

您并未在此处显示所有代码(例如,您如何初始化 Firebase Admin SDK 以创建 admin)。所以我假设您使用项目默认凭据像这样初始化它:

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

这不足以在进入管理 SDK 以使用云存储 API 时使用 getSignedUrl。如果您想使用 getSignedUrl,您需要提供您在控制台中创建的专用服务帐户的凭据。

假设您将这些凭据放在函数文件夹中名为 yourServiceAccount.json 的文件中,您应该使用这些凭据初始化管理 SDK,如下所示:

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

请注意,这只是从 FIREBASE_CONFIG 获取默认项目配置并向其添加服务帐户。

为了导入 JSON 文件,您还需要有一个文件(名为 typings.d.ts):

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

谢谢@Doug Stevenson。我相信我找到了答案。

  1. 我通过重新创建新项目来解决部署错误。

  2. 我将初始化更改为以下代码以解决未处理的拒绝。转到您的 firebase 项目中项目设置下的服务帐户,并在 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.
    });