Firebase存储上传文件-python [英] Firebase storage Upload file -python

查看:59
本文介绍了Firebase存储上传文件-python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助我正在使用python 3.6将文件上传到Firebase存储,但无法得到合理的结果.

I need some help I am using python 3.6 to upload a file to firebase storage, but I couldn't get a reasonable result .

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)

当我更改并使用以下代码上传文件

When I change and use this code below to upload file

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)

知道发生了什么吗?

预先感谢

推荐答案

尝试使用'dene-2ac17.appspot更改'gs://dene-2ac17.appspot.com'.com" ,如此处所述.

Try changing 'gs://dene-2ac17.appspot.com' with 'dene-2ac17.appspot.com' as explained here.

使用默认存储桶

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

You can specify a default bucket name when initializing the Admin SDK. Then you can retrieve an authenticated reference to this bucket. The bucket name must not contain gs:// or any other protocol prefixes. For example, if the bucket URL displayed in the Firebase Console is gs://bucket-name.appspot.com, pass the string bucket-name.appspot.com to the Admin SDK.

这篇关于Firebase存储上传文件-python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆