Firebase存储上载文件python

2024-10-01 09:30:02 发布

您现在位置:Python中文网/ 问答频道 /正文

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

该代码在下面给出了这个错误

^{pr2}$

当我改变和使用下面的代码上传文件

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)

知道发生什么事了吗?在

提前谢谢


Tags: fromimportbucketadminstorageusersblobfile
1条回答
网友
1楼 · 发布于 2024-10-01 09:30:02

尝试用'dene-2ac17.appspot.com'更改{},如here所述。在

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.

相关问题 更多 >