0

I am trying to upload the file to the custom Google Cloud Storage bucket with a Flutter web app.

final _storage = FirebaseStorage.instanceFor(bucket: bucketName);
Reference documentRef = _storage.ref().child(filename);
await documentRef.putData(await data);

The code works fine for a default bucket but fails with a new custom GCP bucket.

Error: FirebaseError: Firebase Storage: An unknown error occurred, please check the error payload for server response. (storage/unknown)

The HTTP POST response causing this error says:

{
  "error": {
    "code": 400,
    "message": "Your bucket has not been set up properly for Firebase Storage. Please visit 'https://console.firebase.google.com/project/{my_project_name}/storage/rules' to set up security rules."
  }
}

So apparently, I need to add a new bucket to Firestore and set up access rules before I can upload the file there.

enter image description here

Since these buckets are created automatically by my backend microservice, is there a way to add them to Firestore and set up the rules with Python SDK? Alternatively, is there any other way to upload data to GCP buckets with Flutter besides Firebase Storage?

Thank you.

Sergii
  • 587
  • 1
  • 5
  • 20

1 Answers1

1

So far I think this is not possible to be done using an SDK, nevertheless this can done making a request to the Firebase API. Here's how it would be done using curl:

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

So you can make a similar request using requests and creating the token as explained here:

import google.auth
import google.auth.transport.requests
creds, project = google.auth.default()

# creds.valid is False, and creds.token is None
# Need to refresh credentials to populate those

auth_req = google.auth.transport.requests.Request()
creds.refresh(auth_req)

# Now you can use creds.token
Puteri
  • 3,348
  • 4
  • 12
  • 27