gpt4 book ai didi

firebase - 如何在 Firebase 函数中获取下载 URL

转载 作者:行者123 更新时间:2023-12-03 23:12:11 27 4
gpt4 key购买 nike

使用 Firebase Storage 存储图像时,图像的 URL 如下所示:
https://firebasestorage.googleapis.com/v0/b/[MY-APP].appspot.com/o/[FILE-NAME]?alt=media&token=[TOKEN]
我想得到这个网址。
根据 thisthis 以及 thisthis ,我需要使用 .getDownloadURL() 方法,该方法位于“存储引用”中……但可用对象的文档与实际对象不符。
当我尝试访问下面代码中文档建议的对象上的 .getDownloadURL() 方法时,我得到了各种未找到属性的错误。

        const admin = require('firebase-admin');
admin.initializeApp();

admin
.storage()
.bucket()
.upload(imageToBeUploaded.filepath, {
destination: storageFolder,
resumable: false,
metadata: {
metadata: {
contentType: imageToBeUploaded.mimetype
}
}
})
.then((taskSnapshot) => {
// HOW DO I GET THE DOWNLOADABLE URL
})
我试过以下: taskSnapshot[1].getDownloadURL() , admin.storage().ref(storageFolder+'/'+imageFileName).getDownloadURL() , admin.storageRef(storageFolder+'/'+imageFileName).getDownloadURL() , admin.storage().ref().child(storageFolder+'/'+imageFileName).getDownloadURL() ,
.. 和一堆其他人。
反复试验没有找到具有该方法的“storageRef”,
如何获取我的图片的可下载网址?

最佳答案

我使用的解决方案是首先更改我的存储桶的访问规则,如下所示:

https://console.firebase.google.com/u/0/project/[MY_APP]/storage[MY_APP].appspot.com/rules

rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read;
allow write: if request.auth != null;
}
}
}

这意味着 URL 上不需要 token 才能查看图像。

然后我刚刚对 URL 进行了硬编码:
            .then((taskSnapshot) => {
const imageUrl = `https://firebasestorage.googleapis.com/v0/b/` +
`${MY_APP_ID}.appspot.com/o/${imageFileName}?alt=media`;
return imageUrl;
})

关于firebase - 如何在 Firebase 函数中获取下载 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58689145/

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