go-cloud icon indicating copy to clipboard operation
go-cloud copied to clipboard

blob: add support for GCS via Firebase

Open tooolbox opened this issue 5 years ago • 1 comments
trafficstars

Is your feature request related to a problem? Please describe.

I am using the Go Firebase Admin SDK in my service. Up until now it has mainly been used for Auth. Now I would like to use it for blob storage; Firebase conveniently allows you to access GCS buckets using your existing credential set. (Awesome!) At the same time, I would like to use the Go CDK to abstract this blob storage so I can for example use flatfile for local development.

Describe the solution you'd like

Ideally I could use the blob package to open a GCS bucket using Firebase credentials.

Right now I do this:

opt := option.WithCredentialsFile("~/my-credentials.json")
firebaseApp, _ := firebase.NewApp(context.Background(), nil, opt)
storage, _ := firebaseApp.Storage(context.Background())
bucket, _ := storage.Bucket("my-bucket")

But ideally I could do this:

bucket, _ := blob.OpenBucket(ctx, "fb://my-bucket")

Describe alternatives you've considered

  1. I can implement my own abstraction for blob storage.
  2. Perhaps I could set my Firebase admin key as GOOGLE_APPLICATION_CREDENTIALS and just open gs://my-bucket, but I have found no documentation that indicates this is possible.

tooolbox avatar Sep 20 '20 20:09 tooolbox

Can you try 2) ?

From a quick glance, firebase.Storage just creates a storage.Client the same way that Go CDK does, and (firebase App](https://github.com/firebase/firebase-admin-go/blob/master/firebase.go#L96) just calls [firebase.Storage` with whatever client options you passed when you created the App. The only tricky business is how it can load config defaults for Firebase (https://github.com/firebase/firebase-admin-go/blob/master/firebase.go#L167).

Unfortunately the firebase packages do not expose anything like the storage.Client or the config options it computes so that kinds of limits the options in terms of starting things off with firebase code and then converting to a Go CDK blob.Bucket.

If you can figure out how to get the google.Credentials for your firebase account, you should be able to create a gcp.TokenSource (https://github.com/google/go-cloud/blob/master/gcp/gcp.go#L88) and from there a gcp.HttpClient (https://github.com/google/go-cloud/blob/master/gcp/gcp.go#L58) that you can use to create a blob.Bucket (https://github.com/google/go-cloud/blob/master/blob/gcsblob/gcsblob.go#L313).

If you can figure out a reasonable way to get Firebase creds and you want to use the default URLOpener you could send a PR to update it, somewhere around here.

vangent avatar Sep 20 '20 21:09 vangent

I think you can use the gs bucket URL directly: gs://{projectID}.appspot.com. If you need to run the emulators locally you will have to set the environement:

	os.Setenv("FIREBASE_STORAGE_EMULATOR_HOST", "localhost:9199")
	os.Setenv("STORAGE_EMULATOR_HOST", "localhost:9199")

One to configre firebase, the other to configure go cloud blob. I think the behaviour of the firebase emulator is limited tho. Getting an error:

Error in : blob (code=NotFound): storage: bucket doesn't exist

on trying to List with a Delimiter and Prefix options set. Works fine on the real implementation.

emcfarlane avatar Oct 15 '22 09:10 emcfarlane

Filed a bug here: https://github.com/firebase/firebase-tools/issues/5129

emcfarlane avatar Oct 15 '22 10:10 emcfarlane