question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to use cloud storage without specifying storageBucket when initialising app with firebase-admin

See original GitHub issue

Sorry if this does not belong here, I was not sure whether the issue is related to firebase-tools or firebase-admin-node

[REQUIRED] Environment info

firebase-tools: 7.2.1 and 7.2.2

Platform: macOS

[REQUIRED] Test case

I am trying to set up an api endpoint which creates an empty folder in the default firebase storage bucket

Using firebase serve to emulate the function (expressjs), this works as expected but when deployed using firebase deploy --project <projectId> I get the following error:

Error: Bucket name not specified or invalid. Specify a valid bucket name via the storageBucket option when initializing the app, or specify the bucket name explicitly when calling the getBucket() method.
    at new FirebaseError (/srv/node_modules/firebase-admin/lib/utils/error.js:42:28)
    at Storage.bucket (/srv/node_modules/firebase-admin/lib/storage/storage.js:107:15)
    at router.post (/srv/api/organisations/document-library.js:6:28)
    at Layer.handle [as handle_request] (/srv/node_modules/express/lib/router/layer.js:95:5)
    at next (/srv/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/srv/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/srv/node_modules/express/lib/router/layer.js:95:5)
    at /srv/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/srv/node_modules/express/lib/router/index.js:335:12)
    at next (/srv/node_modules/express/lib/router/index.js:275:10)

[REQUIRED] Steps to reproduce

  1. Set up an expressjs (nodejs 8) cloud function and initialize the Firebase Admin SDK with no arguments.
const admin = require('firebase-admin');
admin.initializeApp();
  1. Add endpoint that accesses the default bucket.
app.post('/', (req, res) => {
    const storage = admin.storage();
    const bucket = storage.bucket();
    // do stuff
    res.send('accessed bucket')
}

[REQUIRED] Expected behavior

Because the firebase admin sdk is initialised with no arguments, process.env.FIREBASE_CONFIG should be applied automatically (including the storageBucket key)

[REQUIRED] Actual behavior

When testing locally with firebase serve this works as expected but when deployed, I get error where bucket name is not specified

I have checked process.env.FIREBASE_CONFIG in the deployed function’s environment and it is correct: (I have removed the values for this bug report but they are correct)

{
  "projectId":<removed>,
  "databaseURL":<removed>,
  "storageBucket":<removed>,
  "locationId":<removed>
}

On the other hand, storage.appInternal.options is incorrect on the deployed function as it is missing all the process.env.FIREBASE_CONFIG key/values:

{ credential: 
   ApplicationDefaultCredential {
     credential_: MetadataServiceCredential { httpClient: [Object], httpAgent: undefined } } }

compared to locally emulated function:

{ 
  databaseURL: <removed>,
  storageBucket: <removed>,    
  projectId: <removed>,
  credential: 
     ApplicationDefaultCredential {
       credential_: 
        CertCredential {
          certificate: [Object],
          httpClient: [Object],
          httpAgent: undefined } } }

The reason I am checking storage.appInternal.options is because firebase-admin uses the default firebase storage bucket if none is specified as seen on lines 107-108 https://github.com/firebase/firebase-admin-node/blob/master/src/storage/storage.ts

const bucketName = (typeof name !== 'undefined')
      ? name :  this.appInternal.options.storageBucket; 

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
kevinajiancommented, Aug 2, 2019

I believe what’s happening is the firebase-functions sdk populates the FIREBASE_CONFIG variable, so it needs to be imported before running admin.initializeApp(). In your example admin was initialized first, while the config variable was still empty. The emulator also populates the value, so in that case it’s not empty when you’re initializing.

0reactions
petertrihocommented, Aug 2, 2019

That makes sense, thank you

Read more comments on GitHub >

github_iconTop Results From Across the Web

File upload with filestreams and Firebase ... - Stack Overflow
I just added the storageBucket name when initializing the app, ... to this one (Unable to use cloud storage without specifying storageBucket ......
Read more >
Introduction to the Admin Cloud Storage API - Firebase
The Firebase Admin SDK allows you to directly access your Cloud Storage buckets ... You can specify a default bucket name when initializing...
Read more >
Troubleshooting Cloud Functions - Google Cloud
Cloud Functions deployment can fail if the entry point to your code, that is, the exported function name, is not specified correctly. The...
Read more >
storage.js | searchcode
/functions/node_modules/firebase-admin/lib/storage/storage.js ... + 109 'storageBucket option when initializing the app, or specify the bucket name ' + 110 ...
Read more >
How to Access and Download Files in Cloud Storage - Medium
There are multiple ways to use images or other files in Google Cloud Storage or Firebase Storage* in your Firebase web application, and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found