繁体   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