如何编写一个 Firebase http 函数来返回可下载图像的网址(以纯文本形式)?
我使用的是最新版本的 Firebase Admin SDK。
我在默认存储桶中有一个名为 images/thumbnail.png
的文件,我想通过 HTTP 调用返回该文件的 URL,我想出了以下代码:
exports.getImageUrl = functions.https.onRequest((request, response) => {
return firebase.storage().bucket().file("images/thumbnail.png").getSignedUrl({
action: 'read',
expires: '03-09-2491'
})
.then(signedUrls => {
return response.send(signedUrls[0])
})
.catch(error => {
return response.status(500).send(error)
})
})
但是失败了:
callback is not a function
at /user_code/node_modules/firebase-admin/node_modules/@google-cloud/storage/src/file.js:1784:7
at Request._callback (/user_code/node_modules/firebase-admin/node_modules/google-auto-auth/index.js:362:9)
我做错了什么?
编辑
我添加了详细的日志记录,如下所示:
exports.getImage = functions.https.onRequest((request, response) => {
console.log("start")
const storage = firebase.storage()
console.log(storage)
const bucket = storage.bucket()
console.log(bucket)
const file = bucket.file("buildInExercises/bb_4zigzagcushionblue_thumbnail.png")
console.log(file)
return file.getSignedUrl({
action: 'read',
expires: '03-09-2491'
})
.then(urls => {
return response.send(urls)
})
.catch(error => {
return response.status(500).send(error)
})
})
我现在得到了正确的答复:
{
"message": "Permission iam.serviceAccounts.signBlob is required to perform this operation on service account projects/snookercoachapp-dev/serviceAccounts/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fb88959494909e8998949a98939a8b8bd69f9e8dbb9a8b8b888b948fd59c889e898d92989e9a9898948e958fd5989496" rel="noreferrer noopener nofollow">[email protected]</a>."
}
我似乎有权限问题?
最佳答案
该错误消息表明 getSignedUrl()
仅在您使用服务帐户初始化管理 SDK 时才起作用。这意味着您不能依赖在不带参数调用 admin.initializeApp()
时使用的默认凭据。
关于Firebase 函数 (node.js) 从存储返回图像的下载 url(通过文件名),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50706401/