如何使用Python SDK在Firestore中添加GCP桶?

yxyvkwin  于 2022-12-25  发布在  Python
关注(0)|答案(1)|浏览(104)

我正在尝试用Flutter Web应用程序将文件上传到自定义Google云存储桶。

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

该代码在默认桶中工作正常,但在使用新的自定义GCP桶时失败。

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

导致此错误的HTTP POST响应显示:

{
  "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."
  }
}

所以很明显,我需要向Firestore添加一个新的bucket并设置访问规则,然后才能将文件上传到那里。

由于这些bucket是由我的后端微服务自动创建的,是否有办法将它们添加到Firestore并使用Python SDK设置规则?或者,除了Firebase Storage之外,是否有其他方法可以使用Flutter将数据上传到GCP bucket?
谢谢你。

4dbbbstv

4dbbbstv1#

到目前为止,我认为使用SDK是不可能做到这一点的,不过这可以通过向Firebase API发出请求来完成。下面是使用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

因此,您可以使用requests并按照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

相关问题