简体   繁体   English

Firebase Cloud function-Get document snapshot field which is the URL of a file in firebase storage and delete the file from firebase storage - js

[英]Firebase Cloud function-Get document snapshot field which is the URL of a file in firebase storage and delete the file from firebase storage - js

I want to retrieve the field value from a document snapshot which is the URL of a file in firebase storage and delete the file from firebase storage also the firestore document if the time of creation of the doc is before 24 hrs.我想从文档快照中检索字段值,即 firebase 存储中文件的 URL 并从 firebase 存储中删除文件

I am able to delete expired firestore documents successfully with the code below:我可以使用以下代码成功删除过期的 Firestore 文档:

const functions = require("firebase-functions");
const admin = require("firebase-admin");
const { firestore } = require("firebase-admin");
admin.initializeApp();

exports.removeExpiredDocuments = functions.pubsub.schedule("every 1 hours").onRun(async (context) => {
  const db = admin.firestore();
  const now = firestore.Timestamp.now();
  const ts = firestore.Timestamp.fromMillis(now.toMillis() - 86400000); // 24 hours in milliseconds = 86400000

  const snap = await db.collection("photos").where("timestamp", "<", ts).get();
  let promises = [];
  snap.forEach((snap) => {
    promises.push(snap.ref.delete());
  });
  return Promise.all(promises);
});

but I don't know how to retrieve the field value(URL of file) from the document snapshot within the forEach block and delete the file from firebase storage.但我不知道如何从 forEach 块中的文档快照中检索字段值(文件的 URL)并从 firebase 存储中删除文件。

Here's the firestore database:这是firestore数据库:

在此处输入图像描述

The field value of photourl is to be retreived. photourl 的字段值将被检索。

I think code look like:我认为代码看起来像:

//some code ....
    snap.docs.map((doc) => {
    if (doc.exist) {
       var url = doc.data().photourl;
       //do something logic call to firestorage and deleted data base on url get 
       //write logic deleted url firebase after deleted success firestorage           
    }

  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用下载 url 和 Cloud Functions 从 Firebase 存储中删除文件 - Delete a file from firebase storage using download url with Cloud Functions 如何从 Firebase 云 Function 中删除 Firebase 存储文件夹? - How to delete a Firebase Storage folder from a Firebase Cloud Function? 当从实时数据库中删除对象时,Firebase功能(用NodeJS编写)从云存储中删除文件 - Firebase function (written in NodeJS) to delete file from Cloud Storage when an object is removed from Realtime Database 通过 NodeJs 从 Firebase 存储中删除文件 - Delete File from Firebase Storage by NodeJs Firebase 存储 | 从 node.js 云函数上传时文件是“application/octet-stream” - Firebase Storage | File is "application/octet-stream" on upload from node.js cloud function 无法使用云删除 Firebase 存储映像 Function - Unable to delete Firebase Storage Image with Cloud Function 如何使用 Cloud Function 从 Cloud Storage 获取 Firebase 的 object? - How to get object from Cloud Storage for Firebase using Cloud Function? Firebase 存储-如何使用 node.js 从存储中删除文件? - Firebase Storage-How to delete file from storage with node.js? Firebase 云存储上传文件路径错误 - Firebase Cloud Storage Upload File Path Error 将API文件下载到Firebase Cloud Storage? - Downloading an API file to Firebase Cloud Storage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM