Spaces:
Build error
Build error
TruongTrongTien
commited on
Commit
•
f55c453
1
Parent(s):
46caff4
Phase1/TienTT: Add database.py
Browse files
app/configs/database.py
CHANGED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import firebase_admin
|
2 |
+
from firebase_admin import credentials, storage, db
|
3 |
+
|
4 |
+
storageBucket = "fsa-firebase-tutorial.appspot.com"
|
5 |
+
databaseURL = "https://fsa-firebase-tutorial-default-rtdb.asia-southeast1.firebasedatabase.app/"
|
6 |
+
|
7 |
+
cred = credentials.Certificate('credentials\\fsa-firebase-database.json')
|
8 |
+
firebase_admin.initialize_app(cred, {
|
9 |
+
'storageBucket': storageBucket,
|
10 |
+
'databaseURL': databaseURL
|
11 |
+
})
|
12 |
+
|
13 |
+
|
14 |
+
# Upload file to storage
|
15 |
+
def upload_file(file_path, destination_path):
|
16 |
+
bucket = storage.bucket(storageBucket)
|
17 |
+
ref = bucket.blob(destination_path)
|
18 |
+
ref.upload_from_filename(file_path)
|
19 |
+
return True
|
20 |
+
|
21 |
+
# Add file's url and metadata to realtime database
|
22 |
+
def store_metadata(file_url, description):
|
23 |
+
database = db.reference('exam_data') # Or your desired path
|
24 |
+
new_file_ref = database.push()
|
25 |
+
new_file_ref.set({
|
26 |
+
'file_url': file_url,
|
27 |
+
'description': description
|
28 |
+
})
|
29 |
+
return True
|
30 |
+
|
31 |
+
# Query file's url and metadata from file's metadata
|
32 |
+
def query_file(target_description):
|
33 |
+
temp_file_dict = {}
|
34 |
+
database_json = db.reference('exam_data').get()
|
35 |
+
for key, value in database_json.items():
|
36 |
+
if (target_description in value.get('description')):
|
37 |
+
temp_file_dict.update({key: value})
|
38 |
+
return temp_file_dict
|
39 |
+
|
40 |
+
# Extract file's url from a bunch of things
|
41 |
+
def extract_file_url(file_dict):
|
42 |
+
file_urls = []
|
43 |
+
for key, value in file_dict.items():
|
44 |
+
file_urls.append(value['file_url'])
|
45 |
+
return file_urls
|
46 |
+
|
47 |
+
# Read file content, given its url
|
48 |
+
def read_from_file_url(file_url_list):
|
49 |
+
temp_content_list = []
|
50 |
+
bucket = storage.bucket(storageBucket)
|
51 |
+
for file_url in file_url_list:
|
52 |
+
parts = []
|
53 |
+
parts = file_url.split('/')
|
54 |
+
legal_file_url = parts[3:][0]
|
55 |
+
ref = bucket.blob(legal_file_url)
|
56 |
+
file_content = ref.download_as_string().decode('utf-8')
|
57 |
+
temp_content_list.append(file_content)
|
58 |
+
return temp_content_list
|
credentials/fsa-firebase-database.json
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"type": "service_account",
|
3 |
+
"project_id": "fsa-firebase-tutorial",
|
4 |
+
"private_key_id": "89175f3dd111886b549bda9d5a8163ed1fe83900",
|
5 |
+
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDDUXR7JQkfiZRe\nkAFNutX4eIeJxrb00Hpkkbe9Doj+M+aYRrPyXsjEn9o0ZGeomSUwVqDHQ2Tby0lS\nlarvYnML7RaE5bIcIMuZ8CDyKsu9ZyxMIlOm4deJnOP9IfN4ascHxiiMHJBsHsLG\nVHQqmx3XJrfRVGELsxLF/0+fwQOdSiPPhU4MKxqmOjHmCLC9IJLnhrAKyQ1LgV+l\n+6fXc7EGtRVdjUCwQGZMv11UaJbjQqZSCFRpabsyw0/RenPmdyYa35R8F48YaiDR\n/WdxqqMTNbZrbhig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B7giXlDPl\nDaFJQn09DcUYO0D4ILI///OPNly7L2ZCNj2T4aARAoGANEfSQY9v2X21VcuKf5Gl\nIZ0682yvtUmkX0EF8mNSz11GIscWGjQWtEi1v+BSYB0By4mHkg67lOjKcYfaFwqT\nYCPsJgl9pfreraT7hDt8CjJo6QfIcgNhbseCyTF3qqUxQ47aN9c9n0YCd/p8lCvB\ndcbch0+stt5cRoZwXcxGSEo=\n-----END PRIVATE KEY-----\n",
|
6 |
+
"client_email": "firebase-adminsdk-yn0hp@fsa-firebase-tutorial.iam.gserviceaccount.com",
|
7 |
+
"client_id": "104598090077479952875",
|
8 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
9 |
+
"token_uri": "https://oauth2.googleapis.com/token",
|
10 |
+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
11 |
+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-yn0hp%40fsa-firebase-tutorial.iam.gserviceaccount.com",
|
12 |
+
"universe_domain": "googleapis.com"
|
13 |
+
}
|