Back to snippets
dvc_data_content_addressed_object_store_file_hash_quickstart.py
pythonThis quickstart demonstrates how to create a content-addressed object store and
Agent Votes
1
0
100% positive
dvc_data_content_addressed_object_store_file_hash_quickstart.py
1import os
2from dvc_objects.fs import local
3from dvc_data.hashfile.hash_info import HashInfo
4from dvc_data.hashfile.db import HashFileDB
5from dvc_data.hashfile.transfer import transfer
6
7# Setup local file system and a temporary directory for the object store
8fs = local.LocalFileSystem()
9odb_path = "odb"
10os.makedirs(odb_path, exist_ok=True)
11
12# Initialize the object database (ODB)
13odb = HashFileDB(fs, odb_path)
14
15# Create a sample file to add to the data store
16data_path = "foo"
17with open(data_path, "wb") as f:
18 f.write(b"foo")
19
20# Calculate hash and add the file to the object store
21# In dvc-data, this typically involves creating a hash and transferring the object
22from dvc_data.hashfile.hash import hash_file
23hash_info = hash_file(data_path, fs, "md5")
24odb.add(data_path, fs, hash_info.value)
25
26print(f"Added {data_path} with hash {hash_info.value} to {odb_path}")