go-cloud
go-cloud copied to clipboard
blob: add support for GCS via Firebase
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
- I can implement my own abstraction for blob storage.
- Perhaps I could set my Firebase admin key as
GOOGLE_APPLICATION_CREDENTIALSand just opengs://my-bucket, but I have found no documentation that indicates this is possible.
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.
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.
Filed a bug here: https://github.com/firebase/firebase-tools/issues/5129