Andrew
Andrew

Reputation: 21

Cloud Functions for Firebase Storage in Typescript (Property 'ref' does not exist on type Storage)

I may have very well been staring at this too long today. I have a cloud function that automatically generates thumbnails for uploaded images. After a thumbnail is uploaded, I would like a second function to get the thumbnail's url and the get the url of the original object so they can be saved to a document.

When I try to deploy, I'm getting the following error: src/index.ts:28:29 - error TS2339: Property 'ref' does not exist on type Storage.

Now the message is quite clear but I really have no idea what I should be using instead. Most of what I've seen online shows ref() as a property. Any help would be much appreciated!

This issue is in the //get the old section below.

    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

    });

Upvotes: 1

Views: 4240

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317372

One thing you need to understand what using the Firebase Admin SDK with Cloud Storage is that the API is not the same as the client SDK for JavaScript. Firebase Admin actually wraps the Cloud Storage API for nodejs, documented here.

You probably just want to remove these lines:

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

And replace them with code that starts like this:

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

Note also that the Cloud SDK (and all backend SDKs that deal with Cloud Storage) do not have a concept of "download URL" like you see in the client libraries. You will need another way to generate a URL, that's using a signed URL, which is discussed in this question: Get Download URL from file uploaded with Cloud Functions for Firebase

So you will have to continue your function using imageFile and the strategy outline in the answer to that question.

Upvotes: 3

Related Questions