成功将图像保存到Firebase云存储后,firebase函数获取下载url [英] firebase function get download url after successfully save image to firebase cloud storage

查看:120
本文介绍了成功将图像保存到Firebase云存储后,firebase函数获取下载url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将图像保存到云存储后,我无法在firebase函数中获取下载url.以下是我在打字稿中的firebase http函数,用于将base64字符串保存到firebase云存储中的jpeg中.当我记录结果时,它始终为空.我点击此链接获取下载网址 "从使用firebase-admin上传的文件中获取公共URL ".但这给了我以下错误:" SigningError:项目81673989436之前尚未使用身份和访问管理(IAM)API,或者它已被禁用. "并且无法找到简单的例子.我的计划是在将Firestore表上传到云存储后对其进行更新.

I'm having problem getting download url in firebase function after saving image to cloud storage. Below is my firebase http function in typescript to save base64 string to jpeg in firebase cloud storage. when i log result, it always empty. I follow this link to get download url "Get Public URL from file uploaded with firebase-admin". But it give me following error:"SigningError: Identity and Access Management (IAM) API has not been used in project 81673989436 before or it is disabled." And couldn't find simple example to follow. My plan is to update my firestore table, once uploaded to cloud storage.

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控制台中如下所示.即使出现错误,我也可以将图像保存在存储中.

The error is as follow in firebase console. Even with error, I can save image in storage.

SigningError:权限iam.serviceAccounts.signBlob需要在服务帐户项目/vmsystem-4aa54/serviceAccounts/vmsystem-4aa54@appspot.gserviceaccount.com上执行此操作. 在/user_code/node_modules/@google-cloud/storage/src/file.js:1715:16 在Request._callback(/user_code/node_modules/@google-cloud/storage/node_modules/google-auto-auth/index.js:356:9) 在Request.self.callback(/user_code/node_modules/@google-cloud/storage/node_modules/request/request.js:186:22) 在emitTwo(events.js:106:13) 在Request.emit(events.js:191:7) 应要求. (/user_code/node_modules/@google-cloud/storage/node_modules/request/request.js:1163:10) 在emitOne上(events.js:96:13) 在Request.emit(events.js:188:7) 在IncomingMessage. (/user_code/node_modules/@google-cloud/storage/node_modules/request/request.js:1085:12) 在IncomingMessage.g(events.js:292:16)

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)

如何获取下载网址?

我很困惑.现在,我停留在更改后更新功能. 下面是我对admin的初始化.

I am getting confused. Now, I am stuck at updating function after changes. Below is my initialization for admin.

 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).因此,我假设您使用了项目默认的凭据来初始化它,如下所示:

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()

在进入管理SDK以使用Cloud Storage API时,这不足以能够使用getSignedUrl.如果要使用getSignedUrl,则需要提供在控制台中创建的专用服务帐户的凭据.

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.

假设您将这些凭据放入功能文件夹中名为yourServiceAccount.json的文件中,则应使用以下凭据初始化admin SDK:

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

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

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

为了获取JSON文件的导入,您还需要有一个文件(称为Types.d.ts):

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

这篇关于成功将图像保存到Firebase云存储后,firebase函数获取下载url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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