1
votes

I have a Firebase cloud function written in Typescript where I access a Google storage bucket.

If I get the bucket like this:

const storageBucket = admin.storage().bucket(storageBucketName);

VScode shows storageBucket has the type Bucket.

But if I try to use this as parameter on a function:

async function deleteOldBackup(storageBucket: Bucket) {

the type Bucket is underline in red as "cannot find Bucket".

If I try to import the Bucket type like this:

import { Bucket, File } from "@google-cloud/storage"

I get the error that Bucket from admin is different than the Bucket in my deleteOldBackup functions.

How can I get propper Typescript type for google storage in my firebase cloud functions?

3

3 Answers

2
votes

Update dependencies to the latest versions and it'll work.

I've got these and the types are matching:

"dependencies": {
  "firebase-admin": "^9.3.0",
  "firebase-functions": "^3.11.0",
  "@google-cloud/storage": "^5.4.0"
}
1
votes

The way that you have to import the Client Library is like this line:“const {Storage} = require('@google-cloud/storage');”

If you want, for example, list the objects in your bucket. You have to use something like this code:

/

**
 * TODO(developer): Uncomment the following line before running the sample.
 */
// const bucketName = 'Name of a bucket, e.g. my-bucket';

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();

async function listFiles() {
  // Lists files in the bucket
  const [files] = await storage.bucket(bucketName).getFiles();

  console.log('Files:');
  files.forEach(file => {
    console.log(file.name);
  });
}

listFiles().catch(console.error);

You can find more information about this example in the link and more information about the client library in the link.

0
votes

Hm ... this works for me now:

import { File } from "@google-cloud/storage";

type GetFileHelperResult = {
  file?: File;
};

I'm using:

"firebase": "8.2.6",
"firebase-admin": "^9.2.0",
"firebase-functions": "^3.11.0",