Skip to content

File Transfer

File transfer enables sharing of large data blobs across the network.

The FileTransfer service

from hyveos_sdk import Connection
import asyncio
async with Connection() as connection:
file_transfer = connection.get_file_transfer_service()
# continue with usage of file_transfer
# TODO

Example 1. Obtain the File Transfer service handler. Note the async environment.

Publishing a File

A file can be uploaded from the local filesystem by using the publish_file interface.

Internally, all files are stored into a local directory, set by the HYVEOS_BRIDGE_SHARED_DIR, and are transmitted directly to requesting nodes using request-response.

publishing_node.py
from hyveos_sdk import Connection
import os
import asyncio
import aiofiles
from pathlib import Path
async def main():
async with Connection() as connection:
file_transfer = connection.get_file_transfer_service()
shared_dir = os.environ["HYVEOS_BRIDGE_SHARED_DIR"]
file_path = Path(shared_dir) / "example.txt"
async with aiofiles.open(file_path, "w") as f:
await f.write("Hello, world!")
cid = await file_transfer.publish_file(file_path)
print(f"Content ID: {cid}")
asyncio.run(main())

Example 2. Publish example.txt into a shared directory on the hyve. The path to the directory is saved under the environment variable HYVEOS_BRIDGE_SHARED_DIR per default. Write "Hello, world!" into example.txt, and finally publish the file to the hyve shared directory.

Downloading a File to a Node

The get_file interface lets nodes download files, previously uploaded into the network, by their cid The method returns the file’s path.

When the local runtime doesn’t own a copy of this file yet, it downloads it directly from the uploading node.

The file is copied into the shared directory,which is defined by the HYVEOS_BRIDGE_SHARED_DIR environment variable.

downloading_node.py
import asyncio
from hyveos_sdk import Connection
async def main():
async with Connection() as connection:
dht_service = connection.get_dht_service()
cid = await dht_service.get_record_json("file", "example")
if cid:
file_transfer = connection.get_file_transfer_service()
path = await file_tranfser.get_file(cid)
print(f"File path: {path}")
async with aiofiles.open(path, "r") as f:
contents = await f.read()
print(f"File length: {len(contents)}")
else:
print("File not found")
asyncio.run(main())

Example 3. Retrieve a file from the hyve network. Before retrieval, the cid was obtained from the DHT.

Rust: Download Progress

© 2025 P2P Industries. This documentation is licensed under the MIT License.
Cookie Policy    Privacy Policy