gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 20:55:19 26 4
gpt4 key购买 nike

将图像保存到云存储后,我在 firebase 函数中获取下载 url 时遇到问题。下面是我在 typescript 中的 firebase http 函数,用于将 base64 字符串保存到 firebase 云存储中的 jpeg。当我记录结果时,它总是空的。我点击此链接获取下载网址 “Get Public URL from file uploaded with firebase-admin”。但它给我以下错误:“SigningError: Identity and Access Management (IAM) API has not been used in project 81673989436 before or it is disabled.”而且不能找到要遵循的简单示例。我的计划是在上传到云存储后更新我的 firestore 表。

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)

如何获取下载地址?

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

 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;

first

second

third

fourth

最佳答案

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

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

这不足以在进入管理 SDK 以使用 Cloud Storage 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;
}

关于typescript - firebase 函数在成功将图像保存到 firebase 云存储后获取下载 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50131871/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com