最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

firebase - How to add data to Firestore database using python? - Stack Overflow

programmeradmin1浏览0评论

I am trying to add test_data dictionary to my Firestore database using python. It is not working since .set() method is not showing there anymore by vscode auto suggestion.

from datetime import datetime, timezone
import firebase_admin
from firebase_admin import credentials, storage, firestore, db
from google.api_core.exceptions import GoogleAPICallError

cred = credentials.Certificate("code/firebase.json")
firebase_admin.initialize_app(cred)

db = firestore.client()

try:
    test_data = {
        "session_id": "test-session",
        "filename": "test.pdf",
        "url": ".pdf",
        "uploaded_at": f"{datetime.now(timezone.utc)}"
    }
    
    data = {"name": "Los Angeles", "state": "CA", "country": "USA"}

    db.collection("cities").document("LA").set(data)

    print("Data uploaded successfully!")

except GoogleAPICallError as e:
    print(f"Firestore error: {e}")

I'm getting error:

Firestore error: 504 Deadline Exceeded

I have also updated my firebase service account roles, but still it is not working

I am trying to add test_data dictionary to my Firestore database using python. It is not working since .set() method is not showing there anymore by vscode auto suggestion.

from datetime import datetime, timezone
import firebase_admin
from firebase_admin import credentials, storage, firestore, db
from google.api_core.exceptions import GoogleAPICallError

cred = credentials.Certificate("code/firebase.json")
firebase_admin.initialize_app(cred)

db = firestore.client()

try:
    test_data = {
        "session_id": "test-session",
        "filename": "test.pdf",
        "url": "https://example/test.pdf",
        "uploaded_at": f"{datetime.now(timezone.utc)}"
    }
    
    data = {"name": "Los Angeles", "state": "CA", "country": "USA"}

    db.collection("cities").document("LA").set(data)

    print("Data uploaded successfully!")

except GoogleAPICallError as e:
    print(f"Firestore error: {e}")

I'm getting error:

Firestore error: 504 Deadline Exceeded

I have also updated my firebase service account roles, but still it is not working

Share Improve this question edited Feb 10 at 18:19 november asked Feb 10 at 17:37 novembernovember 414 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can't use .set() on a collection reference, only on a document reference. You could use .add(), this also automatically generates an ID for the document. Source: StackOverflow

The 504 error you're getting would suggest that your request to Firestore timed out. This could be due to several reasons. If the test document is large it could be too much data, although since you seem to only upload one file I doubt that's the case, but there's some info about it here.

Since you say you've updated the firebase service account roles I assume you've checked the permission, if not, try that. Otherwise it may be a connection issue, make sure your network is stable. You could also try to add some retries.

This issue was also posted on GitHub, although here they do mention MacOS.

If none of this helps I'd find out at which line in your try statement the error is thrown.

发布评论

评论列表(0)

  1. 暂无评论