繁体   English   中英

如何使用 Python SDK 将 GCP 存储桶添加到 Firestore?

[英]How to add GCP bucket to the Firestore with Python SDK?

我正在尝试使用 Flutter 网络应用程序将文件上传到自定义 Google Cloud Storage 存储桶。

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 添加一个新存储桶并设置访问规则,然后才能将文件上传到那里。

在此处输入图像描述

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

谢谢你。

到目前为止,我认为这是不可能使用 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发出类似的请求并创建令牌如下所述:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM