Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a reference to a google cloud storage bucket inside my cloud function?

I am trying to replicate the guide Example: image transformation.

I'm ok until I need to call:

import * as gcs from '@google-cloud/storage';
import * as functions from 'firebase-functions';

export const onFileChange = functions.storage.object().onFinalize(object => {

  // this line throws a TypeScript Error
  const destBucket = gcs.bucket(fileBucket);
  ...
}

Property 'bucket' does not exist on type 'typeof Storage'. Did you mean 'Bucket'?

I also tried:

const destBucket = new gcs.Bucket(new gcs.Storage({}), object.bucket);

this compiles but when executing I get an error:

gcs.Storage is not a constructor

It seems as if the API has changed, but I'm updated to the latest versions:

"@google-cloud/storage": "^1.6.0",
"firebase-functions": "^1.0.2",

How can I get a reference to the Bucket so that I can call?:

destBucket
    .file(filePath)
    .download({ destination: tempFilePath })
    .then(() => { ... })
like image 338
Nate May Avatar asked Dec 09 '25 17:12

Nate May


1 Answers

Have you declared gcs with a config object like:

const config = {
    projectId: '....',
    keyFilename: './.......-adminsdk-0vlsn-34c393497c.json'
};

const storage = require('@google-cloud/storage')(config);

You have to use a service-key.json generated in Firebase, as explained in this Cloud Functions official sample https://github.com/firebase/functions-samples/tree/master/generate-thumbnail

"Go to the Firebase Console, select the gear image > project settings > Service Accounts and click Generate New Private Key to download a Service Account Key JSON document."


In addition the error message most probably means that you have to create an instance of Storage.

like image 156
Renaud Tarnec Avatar answered Dec 11 '25 16:12

Renaud Tarnec