File Transfer
TL;DR
Transfer files between local and remote machines using the CMDOP Python SDK. Download and upload individual files or entire directories with progress callbacks, resume interrupted transfers, verify integrity with checksums, sync only changed files (like rsync), transfer archives for speed, run concurrent transfers, and copy files directly between remote servers.
Transfer files between local and remote machines with chunking, progress, and resume support.
How do I download a file from a remote server?
from cmdop import AsyncCMDOPClient
async with AsyncCMDOPClient.remote(api_key="cmd_xxx") as client:
# Download a single file from the remote server to a local path
await client.files.download(
"my-server",
"/var/log/app.log",
"./app.log"
)How do I show a progress bar during download?
from tqdm import tqdm
async def download_with_progress(hostname, remote_path, local_path):
# Fetch file metadata to determine the total size for the progress bar
info = await client.files.info(hostname, remote_path)
with tqdm(total=info.size, unit='B', unit_scale=True) as pbar:
# Callback invoked periodically with bytes downloaded so far
async def on_progress(downloaded, total):
pbar.update(downloaded - pbar.n)
await client.files.download(
hostname,
remote_path,
local_path,
on_progress=on_progress
)
await download_with_progress("my-server", "/data/backup.tar.gz", "./backup.tar.gz")How do I resume an interrupted download?
# Resume downloading from where it left off (skips already-downloaded bytes)
await client.files.download(
"my-server",
"/data/large.iso",
"./large.iso",
resume=True # Continues from where it stopped
)How do I upload a file to a remote server?
# Upload a local file to the specified path on the remote server
await client.files.upload(
"./config.yaml",
"my-server",
"/app/config.yaml"
)How do I show a progress bar during upload?
import os
from tqdm import tqdm
async def upload_with_progress(local_path, hostname, remote_path):
# Get local file size for the progress bar total
file_size = os.path.getsize(local_path)
with tqdm(total=file_size, unit='B', unit_scale=True) as pbar:
# Callback invoked periodically with bytes uploaded so far
async def on_progress(uploaded, total):
pbar.update(uploaded - pbar.n)
await client.files.upload(
local_path,
hostname,
remote_path,
on_progress=on_progress
)
await upload_with_progress("./data.tar.gz", "my-server", "/tmp/data.tar.gz")How do I set file permissions on upload?
# Upload a script and make it executable in one step
await client.files.upload(
"./deploy.sh",
"my-server",
"/app/deploy.sh",
mode=0o755
)How do I transfer an entire directory?
How do I download a full directory?
# Recursively download every file in the remote directory to a local folder
await client.files.download_dir(
"my-server",
"/app/logs",
"./logs"
)How do I upload a full directory?
# Recursively upload every file in the local directory to the remote server
await client.files.upload_dir(
"./dist",
"my-server",
"/app/public"
)How do I filter which files to transfer?
# Download only files matching a glob pattern
await client.files.download_dir(
"my-server",
"/app/logs",
"./logs",
pattern="*.log" # Only .log files
)
# Upload a project directory but exclude build artifacts
await client.files.upload_dir(
"./project",
"my-server",
"/app",
exclude=["node_modules", "*.pyc", "__pycache__"]
)How do I transfer directories as archives for faster speed?
How do I download a directory as an archive?
# Compress the remote directory into a tar.gz and download (faster for many small files)
await client.files.download_archive(
"my-server",
"/app/logs",
"./logs.tar.gz",
format="tar.gz"
)How do I upload an archive and extract it on the server?
# Upload a local archive and automatically extract it at the destination
await client.files.upload_archive(
"./dist.tar.gz",
"my-server",
"/app/public",
extract=True
)How do I adjust the transfer chunk size?
# Use larger chunks for high-bandwidth connections (default varies)
await client.files.download(
"my-server",
"/data/file.iso",
"./file.iso",
chunk_size=1024 * 1024 # 1MB chunks
)How do I verify file integrity with checksums?
# Download and automatically compare MD5 checksums after transfer
await client.files.download(
"my-server",
"/data/backup.tar.gz",
"./backup.tar.gz",
verify_checksum=True # Compares MD5 after transfer
)
# Retrieve the MD5 checksum of a remote file without downloading it
checksum = await client.files.checksum("my-server", "/data/backup.tar.gz")
print(f"MD5: {checksum}")How do I sync files like rsync (only transfer changes)?
# Sync a local directory to remote, transferring only modified files
result = await client.files.sync(
"./project",
"my-server",
"/app",
delete=False # Don't delete remote files not in local
)
print(f"Transferred: {result.transferred_files}")
print(f"Skipped: {result.skipped_files}")
print(f"Total bytes: {result.total_bytes}")Error Handling
from cmdop.exceptions import (
TransferError,
DiskFullError,
ConnectionLostError
)
try:
await client.files.upload("./large.iso", "my-server", "/data/large.iso")
except DiskFullError:
# Raised when the remote disk has insufficient space
print("Not enough space on remote")
except ConnectionLostError:
# Raised when the network connection drops during transfer
print("Connection lost during transfer")
# Can resume later with resume=True
except TransferError as e:
# Catch-all for any other transfer failure
print(f"Transfer failed: {e}")How do I run multiple transfers concurrently?
import asyncio
async def transfer_many():
files = [
("file1.log", "/logs/file1.log"),
("file2.log", "/logs/file2.log"),
("file3.log", "/logs/file3.log"),
]
# Upload all files in parallel using asyncio.gather
await asyncio.gather(*[
client.files.upload(local, "my-server", remote)
for local, remote in files
])
await transfer_many()How do I copy files between two remote servers?
# Transfer a file directly from one remote server to another (no local download)
await client.files.remote_copy(
source_host="server-a",
source_path="/data/file.tar.gz",
dest_host="server-b",
dest_path="/backup/file.tar.gz"
)How do I use temporary files for safe uploads?
# Upload to a temp path first, then atomically move to the final location
temp_path = f"/tmp/{uuid.uuid4()}"
await client.files.upload("./config.yaml", "my-server", temp_path)
await client.files.move("my-server", temp_path, "/app/config.yaml")What are the best practices for file transfers?
1. Use Archives for Many Small Files
# Instead of transferring 10000 small files individually,
# create an archive, transfer it, then extract on the remote2. Enable Resume for Large Files
# Always enable resume for large files to handle network interruptions
await client.files.download(
"my-server", "/data/10gb.iso", "./10gb.iso",
resume=True
)3. Verify Critical Transfers
# Enable checksum verification for important files (backups, releases, etc.)
await client.files.upload(
"./backup.tar.gz", "my-server", "/data/backup.tar.gz",
verify_checksum=True
)4. Use Sync for Deployments
# Sync only transfers changed files, making deployments much faster
await client.files.sync("./dist", "my-server", "/app/public")Next
- Directory Browsing β List and navigate
- Terminal β Terminal operations
Last updated on