【问题标题】:Delete image from url Firebase Storage从 url Firebase 存储中删除图像
【发布时间】:2021-03-31 08:41:10
【问题描述】:

在我的云函数中,我试图根据其 url 删除图像,但我认为我没有正确使用 API,因为我遇到了错误:

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

exports.deleteImageWhenOfferDeleted = functions.firestore 
  .document('offers/{offerId}')
  .onDelete(async(snap, context) => {
    console.log('----------------start function--------------------')
    const deletedOffer = snap.data();
    var imageUrlToDelete = deletedOffer.imageUrl;
    await admin.storage.getPathStorageFromUrl(imageUrlToDelete).delete();

function getPathStorageFromUrl(url){
    const baseUrl = "https://firebasestorage.googleapis.com/v0/b/muslim-coloc.appspot.com/o/";
    let imagePath = url.replace(baseUrl,"");
    const indexOfEndPath = imagePath.indexOf("?");
    imagePath = imagePath.substring(0,indexOfEndPath);
    imagePath = imagePath.replace("%2F","/");
    return imagePath;
}

【问题讨论】:

    标签: javascript node.js google-cloud-functions google-cloud-storage


    【解决方案1】:

    我认为您在 storage 字之后缺少括号。 您需要获取存储对象的实例。

    admin.storage.getReferenceFromUrl(...)
    

    应该是

    admin.storage().getReferenceFromUrl(...)
    

    顺便说一句:考虑使用 TypeScript,因为它会在编译期间捕获类似的错误。

    【讨论】:

      猜你喜欢
      • 2020-10-22
      • 1970-01-01
      • 2018-05-02
      • 1970-01-01
      • 2017-08-13
      • 2018-03-15
      • 2021-04-25
      • 2022-01-11
      • 2018-04-11
      相关资源
      最近更新 更多