python - Firebase 存储上传文件 -python
问题描述
我需要一些帮助我正在使用 python 3.6 将文件上传到 firebase 存储,但我无法得到合理的结果。
import firebase_admin
from firebase_admin import credentials, firestore, storage
cred=credentials.Certificate('C:\\Users\\blackturtle\\Envs\\tube\\ps.json')
firebase_admin.initialize_app(cred, {
'storageBucket': 'gs://dene-2ac17.appspot.com'
})
db = firestore.client()
bucket = storage.bucket()
blob = bucket.blob('hello.txt')
outfile='C:\\Users\\blackturtle\\Envs\\tube\\hello.txt'
blob.upload_from_filename(outfile)
该代码在下面给出了这个错误
Exception has occurred: google.api_core.exceptions.NotFound
404 POST https://www.googleapis.com/upload/storage/v1/b/gs://dene-2ac17.appspot.com/o?uploadType=multipart: ('Request failed with status code', 404, 'Expected one of', <HTTPStatus.OK: 200>)
File "C:\Users\blackturtle\Envs\tube\drive.py", line 27, in <module>
blob.upload_from_filename(outfile)
当我更改并使用下面的代码上传文件时
import firebase_admin
from firebase_admin import credentials, firestore, storage
cred=credentials.Certificate('C:\\Users\\blackturtle\\Envs\\tube\\ps.json')
firebase_admin.initialize_app(cred, {
'storageBucket': 'gs://dene-2ac17.appspot.com'
})
db = firestore.client()
bucket = storage.bucket()
blob = bucket.blob('hello.txt')
outfile='C:\\Users\\blackturtle\\Envs\\tube\\hello.txt'
with open(outfile, 'rb') as my_file:
blob.upload_from_file(my_file)
收到此错误
Exception has occurred: google.api_core.exceptions.NotFound
404 POST https://www.googleapis.com/upload/storage/v1/b/gs://dene-2ac17.appspot.com/o?uploadType=resumable: ('Response headers must contain header', 'location')
File "C:\Users\blackturtle\Envs\tube\drive.py", line 29, in <module>
blob.upload_from_file(my_file)
知道发生了什么吗?
提前致谢
解决方案
尝试'gs://dene-2ac17.appspot.com'
按照此处'dene-2ac17.appspot.com'
的说明进行更改。
使用默认存储桶
您可以在初始化 Admin SDK 时指定默认存储桶名称。然后,您可以检索到此存储桶的经过身份验证的引用。存储桶名称不得包含 gs:// 或任何其他协议前缀。例如,如果 Firebase 控制台中显示的存储桶 URL 为 gs://bucket-name.appspot.com,则将字符串 bucket-name.appspot.com 传递给 Admin SDK。
推荐阅读
- flutter - 如何解决 Flutter for Web 中的 Web 开发服务器错误?
- r - 可以通过对 OPENCPU 的 API 调用直接运行 R 片段吗?
- java - 如何使用 Tomcat 7 作为 React 开发服务器来替换 Node.js 服务器?
- java - 无法使用 jakson 将接口对象序列化为 json
- python - 为什么 Django 找不到带有给定 slug 的对象?
- algorithm - 给定 4 个点,如何计算矩形的旋转角度
- c - 如何使用我的 pkcs#8 文件获取 ecdsa 字符串
- ios - Microblink:在最新版本 4.9.1 中加载资源 uae_name_dictionary 失败
- java - 视图的行为是固定大小而不是使用 ConstraintLayout 基于屏幕的动态大小
- python - 如何在 pytest 中将自定义部分添加到终端报告