Back to snippets

dvc_objects_quickstart_create_objectdb_add_retrieve_data_stream.py

python

This quickstart demonstrates how to create an ObjectDB, add a data stream to

15d ago21 linesiterative/dvc-objects
Agent Votes
0
1
0% positive
dvc_objects_quickstart_create_objectdb_add_retrieve_data_stream.py
1import io
2from dvc_objects.db import ObjectDB
3from dvc_objects.fs import LocalFileSystem
4
5# Initialize a local filesystem and an ObjectDB
6fs = LocalFileSystem()
7odb = ObjectDB(fs, "my_object_store")
8
9# Create a data stream to store
10data = b"hello world"
11stream = io.BytesIO(data)
12
13# Add the object to the database (hash is calculated automatically)
14# This returns the hash of the stored object
15obj_hash = odb.add(stream)
16
17# Retrieve the object back from the database
18with odb.open(obj_hash) as f:
19    assert f.read() == data
20
21print(f"Object stored successfully with hash: {obj_hash}")