Last active
August 8, 2023 23:03
-
-
Save cetorres/044e6c8760b17525e6b5665aced474e3 to your computer and use it in GitHub Desktop.
Python TinyDB OCI custom storage
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
import json | |
import os | |
import oci | |
from tinydb import Storage | |
''' | |
OCIStorage is custom Storage for TinyDB to allow | |
store JSON DB files in OCI Object Storage service. | |
Create a TinyDB instance using the following example: | |
db = TinyDB(oci_config=OCI_CONFIG, | |
namespace=OCI_NAMESPACE, | |
bucket=OCI_IQT_BUCKET, | |
file=OCI_JOBS_DB_FILE, | |
storage=OCIStorage) | |
''' | |
class OCIStorage(Storage): | |
def __init__(self, oci_config, namespace, bucket, file): | |
oci_config = json.loads(oci_config) | |
self.client = oci.object_storage.ObjectStorageClient(oci_config) | |
self.namespace = namespace | |
self.bucket = bucket | |
self.file = file | |
def read(self): | |
try: | |
obj = self.client.get_object(self.namespace, self.bucket, self.file) | |
data = obj.data.raw.data | |
return json.loads(data) | |
except oci.exceptions.ServiceError as error: | |
if error.status == 404: | |
self.write({}) | |
return json.loads('{}') | |
def write(self, data): | |
self.client.put_object(self.namespace, self.bucket, self.file, json.dumps(data, sort_keys=True, indent=2, separators=(',', ': '))) | |
def close(self): | |
pass |
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
from tinydb import TinyDB | |
from tinydb_oci_storage import OCIStorage | |
# Config | |
OCI_CONFIG = '{}' | |
OCI_NAMESPACE = 'mynamespace' | |
OCI_BUCKET = 'mybucket' | |
OCI_DB_FILE = 'mydb.json' | |
# Create database object inside an OCI bucket | |
db = TinyDB(oci_config=OCI_CONFIG, namespace=OCI_NAMESPACE, bucket=OCI_BUCKET, file=OCI_DB_FILE, storage=OCIStorage) | |
# Get all items | |
def get_all(): | |
return db.all()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment