use*_*871 4 node.js google-cloud-storage firebase google-cloud-firestore
我的 Firestore 数据库中有一组配置文件和一个名为“profilePicture”的字段,其值为 downloadUrl。
我使用云功能并尝试了很长时间来弄清楚如何在删除配置文件时删除配置文件。
我知道如何在删除配置文件时创建触发器并获取配置文件图片 downloadUrl,但是如何仅使用 downloadUrl 从存储中删除文件?
Dou*_*son 10
我的理解是,用于 Cloud Storage 的节点 SDK无法将 HTTP URL 转换为存储桶内的文件路径。相反,您应该将文件路径与下载 URL 一起存储在文档中。这将使构建一个文件对象成为可能,该对象可用于在需要时删除图像。
小智 9
在 Angular 中,我使用它通过 downloadURL 从 Cloud Firestore 删除文件
constructor(private storage: AngularFireStorage) {}
onDeleteAttachment(downloadURL: string) {
this.storage.storage.refFromURL(downloadURL).delete();
}
Run Code Online (Sandbox Code Playgroud)
foradmin.storage.Storage是没有内置方法可以从 url 获取引用进行存储
但是你可以从 URL 中提取文件路径,通过删除baseUrl并在 URL 上做一些代码替换
我为此任务创建方法以接受来自存储项目的 url 和返回路径
function getPathStorageFromUrl(url:String){
const baseUrl = "https://firebasestorage.googleapis.com/v0/b/project-80505.appspot.com/o/";
let imagePath:string = url.replace(baseUrl,"");
const indexOfEndPath = imagePath.indexOf("?");
imagePath = imagePath.substring(0,indexOfEndPath);
imagePath = imagePath.replace("%2F","/");
return imagePath;
}
Run Code Online (Sandbox Code Playgroud)
注意:您必须
baseUrl为每个项目替换,您可以通过打开存储中的任何图像来找到它,然后从浏览器中的 URL 中从最后一个斜杠“/”的开始到结尾复制它
前任 :
Some image link on my storage :
https://firebasestorage.googleapis.com/v0/b/project-80505.appspot.com/o/RequestsScreenshot%2F-M6CA-2bG2aP_WwOF-dR__1i5056O335?alt=media&token=d000fab7
the base URL will be
https://firebasestorage.googleapis.com/v0/b/project-80505.appspot.com/o/
Run Code Online (Sandbox Code Playgroud)
现在在获取路径调用文件后将其从存储中删除
const storage = admin.storage();
const imagePath:string = getPathStorageFromUrl(obj.imageUrl);
storage.bucket().file(imagePath).delete().catch((err) => console.error(err));
Run Code Online (Sandbox Code Playgroud)
注意:没有解释 URL 格式的文档,这意味着 Firebase 团队可能觉得有一天需要更改它,这意味着如果格式更改,将来可能无法使用。
| 归档时间: |
|
| 查看次数: |
6459 次 |
| 最近记录: |