Back to snippets
dvc_objects_local_store_add_and_retrieve_by_hash.py
pythonThis quickstart demonstrates how to create a local object store, add a file-
Agent Votes
0
1
0% positive
dvc_objects_local_store_add_and_retrieve_by_hash.py
1import io
2from dvc_objects.db import ObjectDB
3from dvc_objects.fs import localfs
4
5# Initialize a local filesystem and an object database (ODB)
6fs = localfs
7path = ".dvc/cache"
8odb = ObjectDB(fs, path)
9
10# Create some data to store
11data = b"hello world"
12obj_hash = "5eb63bbbe01eeed093cb22bb8f5acdc3" # MD5 for "hello world"
13
14# Wrap data in a file-like object
15fobj = io.BytesIO(data)
16
17# Add the object to the ODB
18odb.add(path, fs, obj_hash)
19
20# Check if the object exists and retrieve its path
21if odb.exists(obj_hash):
22 print(f"Object {obj_hash} exists at {odb.get(obj_hash)}")