Unable to use cloud storage without specifying storageBucket when initialising app with firebase-admin
See original GitHub issueSorry 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
- Set up an expressjs (nodejs 8) cloud function and initialize the Firebase Admin SDK with no arguments.
const admin = require('firebase-admin');
admin.initializeApp();
- 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:
- Created 4 years ago
- Comments:7 (4 by maintainers)
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.That makes sense, thank you