簡體   English   中英

無法使用雲刪除 Firebase 存儲映像 Function

[英]Unable to delete Firebase Storage Image with Cloud Function

將此問題的存儲路徑從“yourmaps”更改為“users” 在此處輸入圖像描述 嘗試從 Firebase 存儲中刪除圖像。 我已經閱讀了類似的堆棧溢出問題,並試圖在幾天內毫不費力地解決這個問題。

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const {Storage} = require('@google-cloud/storage');
admin.initializeApp();

刪除用戶帳戶后,會將用戶 uuid 傳遞到雲 function。 我正在嘗試刪除 firebase 存儲中具有相同 uuid 的用戶圖像。

exports.storageImageDelete = functions.firestore
    .document('user/{userId}')
    .onDelete(async (snapshot, context) => {

      const userId = context.params.userId
      const path = `users/${userId}.jpg`;
      const bucketName = 'xxxxxxx.appspot.com/'
      const storage = new Storage();

      return storage.bucket(bucketName).file(path).delete()
        .then(function () {
          console.log(`File deleted successfully in path: ${path}`)
        })
        .catch(function (error) {
          console.error(storage.bucket(bucketName).file(path).delete())
          console.info(storage.bucket(bucketName).file(path).delete())
          console.log(`File NOT deleted: ${path}`)
        })
      });

Firebase 存儲日志中的錯誤消息。 路徑是正確的,但是沒有 object...

Error: No such object: xxxxxx.appspot.com/users/XXXXXXXXXX-XXXX-XXXX-XXXXXXXXXXX.jpg

在 Firebase 存儲“監控規則”中,我可以看到我的嘗試是允許的。 任何幫助,將不勝感激。 我錯過了什么?

由於它是您的默認存儲桶,因此以下內容應該可以解決問題(未經測試):

帶有then的版本(不使用 async/await)

const functions = require('firebase-functions');

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

exports.storageImageDelete = functions.firestore
    .document('user/{userId}')
    .onDelete((snapshot, context) => {

      const userId = context.params.userId
      const path = `users/${userId}.jpg`;

      const bucket = admin.storage().bucket();
      
      return bucket.file(path).delete()
        .then(() => {
          console.log(`File deleted successfully in path: ${path}`)
          return null;
        })
        .catch(error => {
          console.log(`File NOT deleted: ${path}`);
          return null;
        })
      });

使用async / await的版本

exports.storageImageDelete = functions.firestore
.document('user/{userId}')
.onDelete(async (snapshot, context) => {
    
    try {
        const userId = context.params.userId
        const path = `users/${userId}.jpg`;
      
        const bucket = admin.storage().bucket();
        
        await bucket.file(path).delete();
        
        console.log(`File deleted successfully in path: ${path}`)
        return null;
        
    } catch (error) {
        console.log(`File NOT deleted: ${path}`);
        console.log(error);
        return null;
    }
    
  });

也就是說,不需要像你這樣聲明bucket,你可以直接用admin.storage().bucket()來聲明。

我終於解決了我的問題。 這是正確找到我的存儲桶所需的缺失代碼。

admin.initializeApp({
  databaseURL: "https://xxxxxxxx.firebaseio.com",
  storageBucket: "xxxxxxxx.appspot.com"
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM