File transfer enables sharing of large data blobs across the network.
The FileTransfer
service
from hyveos_sdk import Connection
async with Connection () as connection:
file_transfer = connection. get_file_transfer_service ()
# continue with usage of file_transfer
use hyveos_sdk :: Connection;
let connection = Connection :: new () . await . unwrap ();
let mut file_transfer = connection . file_transfer () ;
// continue with usage of file_transfer
import { Client } from ' hyveos-sdk '
import { Connection } from ' hyveos-web '
const transport = new Connection ( ' http://localhost:8080 ' )
const client = new Client (transport)
const filetransferService = client . filetransfer
// continue with usage of filetransferService
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.
from hyveos_sdk import Connection
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 } " )
use hyveos_sdk :: Connection;
let connection = Connection :: new () . await . unwrap ();
let mut file_transfer = connection . file_transfer ();
let shared_dir = std :: env :: var ( " HYVEOS_BRIDGE_SHARED_DIR " ) . unwrap ();
let file_path = Path :: new ( & shared_dir ) . join ( " example.txt " );
tokio :: fs :: write ( & file_path , " Hello, world! " ) . await . unwrap ();
let cid = file_transfer . publish_file ( & file_path ) . await . unwrap ();
println! ( " Content ID: { cid :?} " );
import { Client } from ' hyveos-sdk '
import { Connection } from ' hyveos-web '
const transport = new Connection ( ' http://localhost:8080 ' )
const client = new Client (transport)
const filetransferService = client . filetransfer
const sharedDir = process . env [ " HYVEOS_BRIDGE_SHARED_DIR " ]
const filePath = path . join (sharedDir , ' example.txt ' )
await fs . writeFile (filePath , ' Hello, world! ' )
const cid = await fileTransfer . publishFile (filePath)
console . log ( ` Content ID: ${ cid } ` )
main () . catch ( err => { console . error ( ' Error running main: ' , err) process . exit ( 1 ) })
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.
from hyveos_sdk import Connection
async with Connection () as connection:
dht_service = connection. get_dht_service ()
cid = await dht_service. get_record_json ( " file " , " example " )
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 ) } " )
use hyveos_sdk :: Connection;
let connection = Connection :: new () . await . unwrap ();
let mut dht_service = connection . dht ();
let cid = dht_service . get_record_json ( " file " , " example " ) . await . unwrap ();
let mut file_transfer = connection . file_transfer ();
let path = file_transfer . get_file ( cid ) . await . unwrap ();
println! ( " File path: {} " , path . display ());
let contents = tokio :: fs :: read_to_string ( path ) . await . unwrap ();
println! ( " File length: {} " , contents . len ());
println! ( " File not found " );
import { Client } from ' hyveos-sdk '
import { Connection } from ' hyveos-web '
const transport = new Connection ( ' http://localhost:8080 ' )
const client = new Client (transport)
const dhtService = client . dht
const cid = await dhtService . getRecordJson ( " file " , " example " )
const fileTransferService = connection . filetransfer
const stream = await fileTransferService . getFile (cid)
// stream: ReadableStream
// need to read out the stream to get the file data
const reader = pathOrStream . getReader ()
const { done , value } = await reader . read ()
totalSize += value ?. length || 0
console . log ( ' File length: ${totalSize} ' )
console . log ( ' File not found ' )
main () . catch ( err => { console . error ( ' Error running main: ' , err) process . exit ( 1 ) })
Example 3. Retrieve a file from the hyve network.
Before retrieval, the cid
was obtained from the DHT .
Rust: Download Progress Rust Add-on: Download Progress
The Rust SDK offers the possibility to download files with progress updates, by the get_file_with_progress
interface.
While downloading, it emits events with the download progress as a percentage.
use indicatif :: ProgressBar;
let mut stream = file_transfer_service
. get_file_with_progress ( cid )
let progress_bar = ProgressBar :: new ( 100 );
match stream . next () . await . unwrap () . unwrap () {
DownloadEvent :: Progress ( progress ) => {
progress_bar . set_position ( progress );
DownloadEvent :: Ready ( path ) => {