Firebase存储上传文件-python

时间:2018-10-18 22:37:32

标签: python firebase storage firebase-storage

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

知道发生了什么吗?

预先感谢

1 个答案:

答案 0 :(得分:0)

尝试按here所述用'gs://dene-2ac17.appspot.com'更改'dene-2ac17.appspot.com'

  

使用默认存储桶

     

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

相关问题