TransWikia.com

How to include Typescript types for Google cloud storage in firebase cloud functions

Stack Overflow Asked by Jørgen Rasmussen on January 1, 2021

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?

2 Answers

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"
}

Answered by Andrey Lechev on January 1, 2021

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.

Answered by Enrique Del Valle on January 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP