我正在尝试从 firebase/gc 存储中的文件中获取签名 URL。我能找到的所有其他解决方案是创建一个服务帐户,下载服务密钥,然后在我的应用程序中使用它,但我这样做了,但仍然收到错误。我也遵循了这个建议,并将相关角色添加到我的服务帐户SigningError with Firebase getSignedUrl()。
client_email 属性位于我使用的服务密钥中。
对服务帐户和 gc 来说相当陌生,所以我可能会错过一些简单的东西。
我能够列出存储桶,并且对 cloud firestore 的所有其他请求都在工作。事实证明,它只是特定文件的签名 URL 很难检索。
让我有点疯狂,所以任何帮助都会很棒。
这是我的代码。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');
const { Storage } = require('@google-cloud/storage');
const serviceAccount = require('./emom-84ee4-firebase-adminsdk-2309z-aab93226ec.json');
const keyfileName = require('./emom-84ee4-ac9e94667d5e.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://emom-84ee4.firebaseio.com"
});
const storage = new Storage({
projectId: 'emom-84ee4',
keyFileName: keyfileName,
// TRIED THIS ALSO => keyFileName: serviceAccount
});
const typeDefs = gql`
type DownloadURL {
url: String
}
type Comment {
artist: String,
comment: String,
userId: String,
replies: [Comment]
}
type Track {
album: String,
artist: String,
artistId: String,
description: String,
genre: String,
id: ID,
title: String,
duration: Int,
comments: [Comment]
}
type User {
artist: String,
artistImageUrl: String,
artistName: String,
bio: String,
location: String,
userId: String,
website: String
}
type Query {
tracks: [Track]
users: [User]
downloadUrl: DownloadURL
}
`
const resolvers = {
Query: {
async tracks() {
const tracks = await admin
.firestore()
.collection('tracks')
.get();
return tracks.docs.map(track => track.data());
},
async users() {
const users = await admin
.firestore()
.collection('users')
.get();
return users.docs.map(user => user.data());
},
// ISSUE HAPPENING HERE
async downloadUrl() {
try {
const signedUrl = await storage
.bucket('emom-84ee4.appspot.com')
.file('tracks/ds5MaDn5ewxxvV0CK9GG.mp3')
.getSignedUrl({ action: 'read', expires: '10-25-2022' });
const url = signedUrl;
console.log(url)
return url;
} catch (error) {
console.error(error);
}
}
}
}
const app = express();
const server = new ApolloServer({ typeDefs, resolvers });
server.applyMiddleware({ app, path: '/', cors: true });
exports.graph = functions.https.onRequest(app);
运行
client_email
时出现“无法在没有 firebase emulators:start
的情况下签署数据”错误。
service_account.json
,然后运行GOOGLE_APPLICATION_CREDENTIALS="service_account.json" firebase emulators:start
来修复它
初始化应用程序时,您需要添加服务帐户密钥。
var admin = require("firebase-admin");
var creds = require("[path to your serviceAccountKey.json]");
admin.initializeApp({
credential: admin.credential.cert(creds),
});
如果您没有服务帐户,您可以阅读如何获取它这里
或使用
https://console.firebase.google.com/project/**YOUR_PROJECT**/settings/serviceaccounts/adminsdk
解决方案是直接将证书交给Storage实例:
import { Storage } from '@google-cloud/storage'
const storage = new Storage({
projectId: 'project-id',
keyFilename: 'path_to_your_cert.json'
})
const signedUrl = await storage
.bucket('emom-84ee4.appspot.com')
.file('tracks/ds5MaDn5ewxxvV0CK9GG.mp3')
.getSignedUrl({ action: 'read', expires: '10-25-2022' });