【问题标题】:Cloud Functions for Firebase Storage in Typescript (Property 'ref' does not exist on type Storage)Typescript 中 Firebase 存储的云函数(存储类型上不存在属性“ref”)
【发布时间】:2020-07-26 16:32:19
【问题描述】:

我今天可能已经盯着这个太久了。我有一个云功能,可以自动为上传的图像生成缩略图。上传缩略图后,我想要第二个函数来获取缩略图的 url 和获取原始对象的 url,以便将它们保存到文档中。

当我尝试部署时,我收到以下错误: src/index.ts:28:29 - 错误 TS2339:存储类型上不存在属性“ref”。

现在消息很清楚,但我真的不知道应该改用什么。我在网上看到的大部分内容都将 ref() 显示为属性。任何帮助将不胜感激!

这个问题在下面的 //get old 部分。

    import * as functions from "firebase-functions";

    import * as admin from "firebase-admin";

    const firebase = admin.initializeApp();

    export const addNewImageToPlantFile = functions.storage.bucket("users/{userID}/plants/{docID}/images").object().onFinalize(async (object, context) => {

//***ON CREATION OF NEW THUMBNAIL, WRITE TO FILE

  //check image name to decide if run

      if (object && object.name && object.name.endsWith('_200x200.jpg')) {

      const {docID} = context.params;

  //get the path of the original image

      const split = object?.name.split('_200x200');

      const pathFull = split.join();

      console.log(pathFull);

  //get the url

      const urlThumb = object.mediaLink;

      console.log(urlThumb);

  //get the old 

      const storage = firebase.storage;

      **const reference = storage.ref();**

      const imageReference = reference.child(pathFull);

      const urlFull = await imageReference.getDownloadURL();

      console.log(urlFull);

  //*****PACK THE DATA AND UPDATE FILE*****//

  //get the time from ID



    const time = Number(docID.split('_')[0]);

  //pack the data

      const data = {
          'date': time,
          'full': urlFull,
          'thumb':urlThumb
      };

  //update document

      return admin.firestore().doc('plants/' + docID).update({
        imageSets: admin.firestore.FieldValue.arrayUnion(data)
      });

      } else {
          console.log('Uploaded image is not a thumbnail (_200x200.jpg)');
          return
      }

//FUNCTION END

    });

【问题讨论】:

    标签: typescript firebase google-cloud-functions firebase-storage


    【解决方案1】:

    您需要了解将 Firebase Admin SDK 与 Cloud Storage 结合使用的情况是,该 API 与用于 JavaScript 的客户端 SDK 不同。 Firebase 管理员实际上为 nodejs 包装了 Cloud Storage API,记录在 here

    您可能只想删除这些行:

    const storage = firebase.storage;
    const reference = storage.ref();
    

    并用这样开头的代码替换它们:

    const imageFile = admin.storage().bucket().file(pathFull);
    

    另请注意,Cloud SDK(以及所有处理 Cloud Storage 的后端 SDK)没有您在客户端库中看到的“下载 URL”概念。您将需要另一种生成 URL 的方法,即使用签名 URL,在此问题中进行了讨论:Get Download URL from file uploaded with Cloud Functions for Firebase

    因此,您必须使用imageFile 和该问题答案中的策略大纲来继续您的功能。

    【讨论】:

    • 我用它转了一圈,但你绝对是对的!我想我只是精神不振。我让该功能正常工作,但最终决定通过使用 firebasestorage API 检查来将该功能保留在用户设备上,直到它获得 url thumb 或超时。我还想确保所有链接都是公开的并且不会过期,所以为了简单起见,我认为最好坚持使用 firebasestorage API。
    • 如果您将日期设置为任意遥远的未来,签名 URL 不会过期。 100 年是一个很好的起点。
    猜你喜欢
    • 2021-12-31
    • 2021-12-26
    • 2018-01-14
    • 2016-12-19
    • 1970-01-01
    • 2021-08-23
    • 2020-12-11
    • 2023-04-02
    • 2018-08-17
    相关资源
    最近更新 更多