0

I have created an api to create bucket on firebase, bucket is successfully created on Google Cloud but I want that it should automatically be imported in firebase too. Currently I have to do it manually as i want everything to be done in firebase. Attaching some code which is used to create bucket.

function get_firebase_client()
{
    return new StorageClient(
        array(
            'keyFile' => json_decode(file_get_contents('firebase.json'), true),
            'suppressKeyFileNotice' => true
        )
    );
}

function create_firebase_bucket($bucket_name) {
    $storage = get_firebase_client();
    try {
        $storage->createBucket($bucket_name, array(
            'predefinedAcl' => 'publicRead',
            'requesterPays' => true
        ));
        return array('success' => true);
    } catch(Exception $e) {
        return array('success' => false, 'message' => json_decode($e->getMessage(), true));
    }
}
Akhilesh
  • 927
  • 1
  • 6
  • 21

1 Answers1

1

Looking around for this I noticed that this is not possible at this moment and you may want to file this as a feature request here. Also this is discussed in this question.

Anyway, being a little curious I was able to notice that the Frontend of the Firebase console makes a POST to a particular URL to add the buckets to Firebase:

https://firebasestorage.clients6.google.com/v1alpha/projects/[PROJECT_NUMBER]/buckets/[BUCKET_NAME]:addFirebase

I tried to check if that URL was working outside the Firebase console with curl and yep, it worked:

curl -X POST \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
https://firebasestorage.clients6.google.com/v1alpha/projects/[PROJECT_NUMBER]/buckets/[BUCKET_NAME]:addFirebase

Since I'm not good with PHP I cannot provide any code to authenticate with OAuth2 to get the token so I think this will be your "homework".

Dharman
  • 30,962
  • 25
  • 85
  • 135
Puteri
  • 3,348
  • 4
  • 12
  • 27