简体   繁体   English

无法使用云删除 Firebase 存储映像 Function

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

Changed Storage path from "yourmaps" to "users" for this question将此问题的存储路径从“yourmaps”更改为“users” 在此处输入图像描述 Attempting to delete an image from Firebase Storage.尝试从 Firebase 存储中删除图像。 I have read similar stack overflow questions and attempted to solve this for days without effort.我已经阅读了类似的堆栈溢出问题,并试图在几天内毫不费力地解决这个问题。

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

A user uuid is passed to the cloud function when a users account has been deleted.删除用户帐户后,会将用户 uuid 传递到云 function。 i am attempting to delete the user image which has the same uuid in firebase Storage.我正在尝试删除 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}`)
        })
      });

Error message in Firebase Storage Log. Firebase 存储日志中的错误消息。 Path is correct however there is no object...路径是正确的,但是没有 object...

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

In Firebase Storage "Monitor rules" i can see that my attempts are allowed.在 Firebase 存储“监控规则”中,我可以看到我的尝试是允许的。 Any help would be appreciated.任何帮助,将不胜感激。 What am i missing?我错过了什么?

Since it is your default bucket, the following should do the trick (untested):由于它是您的默认存储桶,因此以下内容应该可以解决问题(未经测试):

Version with then (not using async/await)带有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;
        })
      });

Version using async / await使用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;
    }
    
  });

In other words, no need to declare the bucket as you do, you can directly declare it with admin.storage().bucket() .也就是说,不需要像你这样声明bucket,你可以直接用admin.storage().bucket()来声明。

I finally solved my issue.我终于解决了我的问题。 Here is the missing code that was required to correctly find my bucket.这是正确找到我的存储桶所需的缺失代码。

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

暂无
暂无

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

相关问题 使用Firebase Cloud功能检查存储中是否存在映像 - Check if image exists at storage with firebase cloud function 从可调用的 https 云 function 将图像上传到 Firebase 存储 - Upload an image into Firebase Storage from a callable https cloud function 通过云功能检索Firebase存储图像链接 - Retrieving a Firebase storage image link via Cloud Function Google云功能-存储-删除图像-“ ApiError:请求期间出错” - Google Cloud Function - Storage - Delete Image - “ApiError: Error during request” 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 使用Google Cloud AutoML模型预测Firebase功能存储在Google Cloud存储中的图像 - Use Google Cloud AutoML model predict an image which is stored in Google Cloud storage in Firebase function 当从实时数据库中删除对象时,Firebase功能(用NodeJS编写)从云存储中删除文件 - Firebase function (written in NodeJS) to delete file from Cloud Storage when an object is removed from Realtime Database 从Cloud功能的Firebase存储下载时出错 - Error downloading from firebase Storage in a Cloud Function 从 url Firebase 存储中删除图像 - Delete image from url Firebase Storage firebase 存储上上传的缺少图像(使用云 function 更新 Firestore DB) - Missing image uploaded on firebase storage (Using cloud function to update firestore DB)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM