【问题标题】:Firebase storage Upload file -pythonFirebase 存储上传文件 -python
【发布时间】:2018-10-18 22:37:32
【问题描述】:

我需要一些帮助 我正在使用 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)

知道发生了什么吗?

提前致谢

【问题讨论】:

    标签: python firebase storage firebase-storage


    【解决方案1】:

    尝试按照here 的说明将'gs://dene-2ac17.appspot.com' 更改为'dene-2ac17.appspot.com'

    使用默认存储桶

    您可以在以下情况下指定默认存储桶名称 初始化管理 SDK。然后您可以检索经过身份验证的 引用此存储桶。存储桶名称不得包含 gs:// 或 任何其他协议前缀。例如,如果显示存储桶 URL 在 Firebase 控制台中是 gs://bucket-name.appspot.com,通过 字符串 bucket-name.appspot.com 到 Admin SDK。

    【讨论】:

    • 感谢您的建议。我刚刚尝试了您的建议,但得到了 sama 错误('响应标头必须包含标头','位置')任何建议将不胜感激@Cheche
    • 哎哟!,我认为它会工作。是您的第一个或第二个代码上的错误吗?,
    • 我按照您建议的方式再次分别尝试了两个代码块,它们给出的错误与上述相同。 @Cheche
    • 好的,抱歉@digging - 最后两枪:a) 尝试隔离您的问题,删除 db = firestore.client(); b) 检查您的凭据是否正确。
    • 感谢您抽出宝贵时间解决@Cheche 的问题,这都是关于凭据问题的。我从 firebase 复制凭据并粘贴,然后它就像魅力一样工作。再次感谢您
    猜你喜欢
    • 2018-10-13
    • 2020-04-22
    • 2021-12-02
    • 2019-01-26
    • 2020-11-21
    • 2019-11-06
    • 2020-07-10
    • 2020-04-08
    • 1970-01-01
    相关资源
    最近更新 更多