简体   繁体   English

Firebase存储上传文件-python

[英]Firebase storage Upload file -python

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

That code gives this error below 该代码在下面给出了此错误

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)

Got this error 得到了这个错误

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)

Any idea what's going on? 知道发生了什么事吗?

Thanks in advance 提前致谢

Try changing 'gs://dene-2ac17.appspot.com' with 'dene-2ac17.appspot.com' as explained here . 尝试改变'gs://dene-2ac17.appspot.com''dene-2ac17.appspot.com'作为解释在这里

Use a default bucket 使用默认存储桶

You can specify a default bucket name when initializing the Admin SDK. 您可以在初始化Admin SDK时指定默认存储桶名称。 Then you can retrieve an authenticated reference to this bucket. 然后,您可以检索对此存储桶的经过身份验证的引用。 The bucket name must not contain gs:// or any other protocol prefixes. 存储桶名称不得包含gs://或任何其他协议前缀。 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控制台中显示的存储区URL为gs://bucket-name.appspot.com,请将字符串bucket-name.appspot.com传递给Admin SDK。

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

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