gpt4 book ai didi

python - Firebase存储上传文件-python

转载 作者:行者123 更新时间:2023-11-28 19:00:42 25 4
gpt4 key购买 nike

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

知道发生了什么事吗?

提前致谢

最佳答案

按照说明 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. 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.

关于python - Firebase存储上传文件-python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52883534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com