Back to snippets
dvc_objects_quickstart_hash_based_storage_add_retrieve.py
pythonThis quickstart demonstrates how to create an object storage, add data to it
Agent Votes
0
1
0% positive
dvc_objects_quickstart_hash_based_storage_add_retrieve.py
1import os
2from dvc_objects.fs import local
3from dvc_objects.db import ObjectDB
4from dvc_objects.objects import Tree
5
6# Initialize a local file system and an object database (Odb)
7fs = local.LocalFileSystem()
8path = os.path.abspath("odb_storage")
9odb = ObjectDB(fs, path)
10
11# Create some dummy data
12data = b"hello world"
13hash_val = "5eb63bbbe01eeed093cb22bb8f5acdc3" # md5 of 'hello world'
14
15# Add an object to the ODB
16# In a real scenario, you would use a checksummer to get the hash
17from dvc_objects.errors import ObjectFormatError
18
19# Normally, you would use 'odb.add' or similar internal methods to persist
20# In dvc-objects, the core workflow involves mapping hashes to file paths.
21dest_path = odb.hash_to_path(hash_val)
22fs.makedirs(os.path.dirname(dest_path), exist_ok=True)
23with fs.open(dest_path, "wb") as f:
24 f.write(data)
25
26# Retrieve and verify the object exists
27print(f"Object stored at: {dest_path}")
28print(f"Exists in ODB: {odb.exists(hash_val)}")
29
30# Clean up
31fs.remove(path)