Skip to content

Instantly share code, notes, and snippets.

@batyrf
Created September 15, 2020 17:05
Show Gist options
  • Save batyrf/f45734cee4ee3a25b1932c1dad3977eb to your computer and use it in GitHub Desktop.
Save batyrf/f45734cee4ee3a25b1932c1dad3977eb to your computer and use it in GitHub Desktop.
Upload CSV sheets to Cloud Firestore
#!/usr/bin/python3
import sys
import firebase_admin
import csv
import uuid
from firebase_admin import credentials
from firebase_admin import firestore
def getDb(keyPath):
cred = credentials.Certificate(keyPath)
firebase_admin.initialize_app(cred)
return firestore.client()
def setData(db, collectionName, dataDict):
documentKey = str(uuid.uuid1().int)
db.collection(collectionName).document(documentKey).set(dataDict)
print("{}/{} -> {}".format(collectionName, documentKey, dataDict))
if __name__ == "__main__":
if (len(sys.argv) < 3):
print('USAGE:\ncsvFirestoreUploader.py key.json data.csv')
sys.exit(2)
keyPath = sys.argv[1]
db = getDb(keyPath)
dataPath = sys.argv[2]
collectionName = sys.argv[3] if (len(sys.argv) == 4 and sys.argv[3]) else 'apiValues'
print("keyPath: {}, dataPath: {}, collectionName: {}".format(keyPath, dataPath, collectionName))
with open(dataPath, newline='\n') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
setData(db, collectionName, row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment