google-cloud-functions - 使用云功能删除 firebase 存储上的文件时出现错误 No such Object

标签 google-cloud-functions firebase-storage

这个问题已经在一些地方得到了回答,但没有一个解决方案对我有用。
我也关注了官方文档。并尝试对我的代码进行故障排除一周,
但我的代码似乎是正确的,只有错误不断出现,没有这样的对象
我注意到,当我使用存储触发器为它工作的同一图像生成拇指时,我将文件路径、存储桶名称与我尝试使用 firestore 触发器删除的文件路径、存储桶名称进行比较,结果相同。
但仅在 Firestore 触发器中,这以错误结束 没有此类对象
这是我的代码,我看不出它有什么问题。

'use strict';
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');

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

//var serviceAccount = require("./xproject-cdfdc-firebase-adminsdk-xxxx-xxxxx.json");
/*admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "https://xproject-cdfdc.firebaseio.com"
});*/

admin.initializeApp();

const db = admin.firestore();
const storage = admin.storage();


exports.cleanStorage = functions.firestore.document('items/{itemId}').onDelete((snapshot,context) => {

    const id = snapshot.get('itemId');
    //log: gettig id successfully
    const imageName = snapshot.get('imageName');
    //log: getting image name successfully
    const defaultBucket = storage.bucket(); 
    //log: defaultBucket.name() => xproject-cdfdc.appspot.com
    //log: My bucket is initialzed successfully and exists:
    const imageFile = defaultBucket.file('images/'+id+'/'+imageName);
    //log: imageFile.name() =>images/W0qMLHqfQANfMsbUEmihK49NFgm2_1595320646264/photo1595320646264.jpg
    //log: this path is correct and the file exists on stoarage

    return imageFile.delete().then(result =>{
        return console.log(`Deleted ${imageName}`);
    }).catch(error => {
      return console.log("Deliting  failed: error->"+error);
      //log: error->Error: No such object: xproject-cdfdc.appspot.com/images/W0qMLHqfQANfMsbUEmihK49NFgm2_1595320646264/photo1595320646264.jpg
    });
});
所以现在我不知道我应该在我的代码中更正什么,因为在 stackoverflow 中所有类似的问题都有类似的解决方案,我已经尝试过了。
任何帮助将不胜感激。谢谢。

最佳答案

我终于解决了我的问题。
实际上,我的脚本没有任何问题,并且一切正常。
发生的事情是,在使用云功能在firestore中删除相应项目后自动删除我的存储图像之前,我在android代码中有一个功能可以完成所有工作。
现在在编写我的云功能后,我忘记编辑我的 android 应用程序代码以不删除存储图像。
因此,从 android 代码中删除比云函数更快,并且在云函数完成工作时,该文件不再存在。
所以现在我明白了“我们只是人类
这个错误让我损失了整整一个星期。
(也删除对我不起作用的文件夹并且没有这样的对象。删除文件才有效)

关于google-cloud-functions - 使用云功能删除 firebase 存储上的文件时出现错误 No such Object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63011991/

相关文章:

firebase - 避免向使用 Firebase 函数构建的 API 发送垃圾邮件

firebase - 获取 firebase 图像的 url 的成本

javascript - 我的 Firebase 云函数因 Object.value is not a function 错误而失败?

Firebase云功能无法读取硝基生成的index.mjs文件

firebase - 使用 Stackdriver 调试 Firebase Cloud Functions

ios - 将图像上传到 firebase 时 Swift 应用程序崩溃

javascript - Google Polymer paper-fab 点击即可开始下载

android - 上传图像,Firebase 显示 "User does not have permission to access this object",即使存储设置是公开的

ios - 尝试Pod安装Firebase时出现Cocoapods错误-Nanaimo

node.js - Access-Control-Allow-Methods Angular 6 firebase 不允许方法 PUT