Created
September 15, 2020 17:05
-
-
Save batyrf/f45734cee4ee3a25b1932c1dad3977eb to your computer and use it in GitHub Desktop.
Upload CSV sheets to Cloud Firestore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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