1
votes

per https://firebase.google.com/docs/storage/admin/start below code returns a reference to bucket defined in Google Cloud Storage client libraries

var admin = require("firebase-admin"); 
var bucket = admin.storage().bucket()

but the second line fails in firebase functions (however admin.database() works), my guess is Google Cloud Storage client libraries is not properly imported, according to https://cloud.google.com/storage/docs/reference/libraries it can be done by

npm install --save @google-cloud/storage

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

but how to associate the Storage with the bucket created from admin?

1

1 Answers

0
votes

(https://www.npmjs.com/package/firebase-admin)

Default Bucket resolved with Firebase admin:

const bucket = admin.storage().bucket();

Custom bucket resolved with Firebase admin:

const bucketName = "my-awesome-bucket";
const bucket = admin.storage().bucket(bucketName);

Custom Bucket resolved with @google-cloud/storage:
(https://www.npmjs.com/package/@google-cloud/storage)

const {Storage} = require('@google-cloud/storage');
const storage = new Storage();

const bucketName = "my-awesome-bucket";
const bucket = storage.bucket(bucketName);

Both .bucket() method will create a Bucket object to interact with a Cloud Storage bucket.

Both method create a bucket object as explained in their docs:

This is what the official documentation says:

You can use the bucket references returned by the Admin SDK in conjunction with the official Google Cloud Storage client libraries to upload, download, and modify content in the buckets associated with your Firebase projects.