Firebase 如何在云函数上创建具有管理员权限的refFromURL?

Firebase 如何在云函数上创建具有管理员权限的refFromURL?,firebase,google-cloud-firestore,google-cloud-functions,firebase-storage,Firebase,Google Cloud Firestore,Google Cloud Functions,Firebase Storage,我希望在触发firestore update cloud函数时使用其http URL引用图像,以便我可以从change提供的onUpdate()获取URL函数并使用它获取对firebase存储上图像的引用并将其删除。要从云函数中删除存储在firebase云存储中的文件,您需要基于以下内容创建一个文件对象: 此文件附加到的Bucket实例 文件名 然后调用delete()方法 如Node.js库文档中所述 以下是文档中的代码示例: const storage = new Storage(); co

我希望在触发firestore update cloud函数时使用其http URL引用图像,以便我可以从
change
提供的
onUpdate()获取URL
函数并使用它获取对firebase存储上图像的引用并将其删除。

要从云函数中删除存储在firebase云存储中的文件,您需要基于以下内容创建一个
文件
对象:

  • 此文件附加到的Bucket实例

  • 文件名

  • 然后调用
    delete()
    方法

    如Node.js库文档中所述

    以下是文档中的代码示例:

    const storage = new Storage();
    const bucketName = 'Name of a bucket, e.g. my-bucket';
    const filename = 'File to delete, e.g. file.txt';
    
    // Deletes the file from the bucket
    storage
      .bucket(bucketName)
      .file(filename)
      .delete()
      .then(() => {
        console.log(`gs://${bucketName}/${filename} deleted.`);
      })
      .catch(err => {
        console.error('ERROR:', err);
      });
    
    从您的问题中,我了解到您的应用程序客户端没有bucket和文件名,只有一个下载URL(如果是web应用程序,则可能是通过生成的,或者其他SDK的类似方法)

    因此,挑战在于从下载URL派生bucket和文件名

    如果查看下载URL的格式,您会发现其组成如下:

    https://firebasestorage.googleapis.com/v0/b/<your-project-id>.appspot.com/o/<your-bucket-name>%2F<your-file-name>?alt=media&token=<a-token-string>
    

    我认为你不需要文件的URL来做你想做的事情。通常您只需要bucket和文件名。您的用户不知道任何其他标识符,因此是否要使用URL来标识文件??此外,您是在谈论文件的“存储位置URL”还是“下载URL”?我是在谈论Http下载URL。我目前正在这样做,我只是想看看是否有更有效的方法可以轻松创建引用。非常奇怪,Firebase的工程师还没有为此制作api。无论如何,非常感谢@Renaud Tarnec
    const storage = new Storage();
    
    .....
    
    exports.deleteStorageFile = functions.firestore
        .document('deletionRequests/{requestId}')
        .onUpdate((change, context) => {
          const newValue = change.after.data();
          const downloadUrl = newValue.downloadUrl;
    
          // extract the bucket and file names, for example through two dedicated Javascript functions
          const fileBucket = getFileBucket(downloadUrl);
          const fileName = getFileName(downloadUrl);
    
          return storage
            .bucket(fileBucket)
            .file(fileName)
            .delete()
    
        });