0
votes

I see a lot of controversial documentation about using Storage with Cloud Functions. I really don't understand which documentation I should to use. Anyway I think I've tried all of them and nothing is working. Please help me. For example: I'm trying to use this docs: https://firebase.google.com/docs/storage/admin/start

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const database = admin.database();

var bucket = admin.storage().bucket("my-custom-bucket");

I have the error

Unhandled error TypeError: storage.bucket is not a function

If I see this docs: https://firebase.google.com/docs/storage/extend-with-functions.

What's that?

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const database = admin.database();
const gcs = require('@google-cloud/storage')(); //!!! - is it mistype?
const spawn = require('child-process-promise').spawn;
const path = require('path');
const os = require('os');
const fs = require('fs');

const bucket = gcs.bucket('fileBucket');

TypeError: require(...) is not a function

but anyway even if it is mistype it will be next error:

TypeError: gcs.bucket is not a function

And yes I did install:

npm install --save child-process-promise
npm install --save @google-cloud/storage

Does anybody know how to use it? Or where is correct documentation?

1

1 Answers

2
votes

This likely has nothing to do with Cloud Functions.

First of all, you should know that the Firebase Admin SDK just wraps the Cloud Storage SDK. It eventually exposes all the same objects and APIs.

Second, know that version 2.0.0 of the Cloud Storage SDK for node introduced some significant breaking changes with the way you use its APIs. Compare the old way to the new way of importing the library.

The Admin SDK (and the documentation you're looking at) was all built for the old way with 1.7.0. If you manually updated Cloud Storage to 2.0.0, then the Admin SDK might be breaking because of this change, as the primary exported object from the Cloud Storage SDK is different.

You should either use the Admin SDK without also updating the Cloud Storage dependency, or skip the Admin SDK and just use the Cloud Storage SDK directly.