Filesharing Service Fetch File Task¤
task api name:
fetch_file
The fetch_file task streams a file from the File Sharing worker to the client in chunks with offset support. This enables efficient downloading of large files with resume capability. The task is typically called through the client helper method NFPClient.fetch_file() which handles the streaming protocol, caching, and local file management automatically.
Using it from Python¤
from norfab.core.nfapi import NorFab
with NorFab(inventory="./inventory.yaml") as nf:
client = nf.make_client()
# Download file (recommended way - uses client helper)
ret = client.fetch_file(url="nf://filesharing/test_file_1.txt")
local_path = ret["content"]
print(local_path)
# Download and read content as text
ret = client.fetch_file(url="nf://filesharing/test_file_1.txt", read=True)
print(ret["content"]) # text content
# Direct task invocation (not recommended - use client helper instead)
reply = client.run_job(
service="filesharing",
task="fetch_file",
workers="any",
kwargs={
"url": "nf://filesharing/test_file_1.txt",
"chunk_size": 256000,
"offset": 0,
},
)
print(reply)
Using it from nfcli¤
NORFAB CLI exposes File Sharing under the file command group.
nf#man tree file.copy
root
└── file: File sharing service
└── copy: Copy files
├── url: File location, default 'nf://'
├── destination: File location to save downloaded content
└── read: Print file content, default 'False'
nf#
API Reference¤
Fetch a file in chunks with offset support.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
URL path starting with 'nf://' to fetch file from |
required |
chunk_size
|
int
|
Size of chunk to read in bytes (default: 256KB) |
256000
|
offset
|
int
|
Number of bytes to offset (default: 0) |
0
|
Returns:
| Type | Description |
|---|---|
Result
|
Result containing file chunk bytes or error message |
Source code in norfab\workers\filesharing_worker\filesharing_worker.py
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | |