File Operations Example¶
This example demonstrates all file manipulation operations in Nexus: write, read, copy, move, and delete with metadata tracking and optimistic concurrency control.
🎯 What You'll Learn¶
- Write content to files (inline, stdin, from files)
- Read files with and without metadata
- Copy files with automatic deduplication
- Move and rename files
- Delete files safely
- Use optimistic concurrency control (create-only, conditional updates)
- Handle binary files
- Work with permissions
🚀 Quick Start¶
import nexus
# Connect (embedded mode - no auth)
nx = nexus.connect(config={"data_dir": "./nexus-data"})
# Write a file
nx.write("/workspace/hello.txt", b"Hello, Nexus!")
# Read it back
content = nx.read("/workspace/hello.txt")
print(content.decode()) # "Hello, Nexus!"
# Copy file
nx.copy("/workspace/hello.txt", "/workspace/hello-backup.txt")
# Move/rename
nx.move("/workspace/hello-backup.txt", "/workspace/archived/hello.txt")
# Delete
nx.delete("/workspace/archived/hello.txt")
# Write files
nexus write /workspace/hello.txt "Hello, Nexus!"
echo "Content from stdin" | nexus write /workspace/file.txt --input -
# Read files
nexus cat /workspace/hello.txt
nexus cat /workspace/hello.txt --metadata
# Copy files
nexus cp /workspace/hello.txt /workspace/hello-backup.txt
# Move files
nexus move /workspace/hello-backup.txt /workspace/archived/hello.txt --force
# Delete files
nexus rm /workspace/archived/hello.txt --force
📝 Write Operations¶
📖 Read Operations¶
import nexus
nx = nexus.connect(config={"data_dir": "./nexus-data"})
# Read with full metadata
result = nx.read("/workspace/hello.txt", return_metadata=True)
print(f"Content: {result['content'].decode()}")
print(f"ETag: {result['etag']}")
print(f"Version: {result['version']}")
print(f"Size: {result['size']} bytes")
print(f"Modified: {result['modified_at']}")
📋 Copy Operations¶
🔀 Move Operations¶
🗑️ Delete Operations¶
🔒 Optimistic Concurrency Control¶
import nexus
nx = nexus.connect(config={"data_dir": "./nexus-data"})
# Read current version
result = nx.read("/workspace/data.json", return_metadata=True)
current_etag = result['etag']
# Modify content
new_content = modify_data(result['content'])
try:
# Write only if ETag matches (no concurrent modifications)
nx.write(
"/workspace/data.json",
new_content,
if_match=current_etag
)
print("Update successful")
except nexus.ConflictError:
print("File was modified by another process!")
🎬 Complete Workflow Example¶
import nexus
nx = nexus.connect(config={"data_dir": "./nexus-data"})
# 1. Create initial document
nx.mkdir("/workspace/docs", parents=True)
nx.write("/workspace/docs/README.md", b"# My Project")
# 2. Create backup before editing
nx.copy("/workspace/docs/README.md", "/workspace/docs/README.backup.md")
# 3. Update the document
updated_content = b"""# My Project
## Overview
This is an awesome project built with Nexus.
## Features
- Feature 1
- Feature 2
"""
nx.write("/workspace/docs/README.md", updated_content)
# 4. Archive old backup
nx.mkdir("/workspace/archive/docs", parents=True)
nx.move(
"/workspace/docs/README.backup.md",
"/workspace/archive/docs/README.backup.md"
)
# 5. Clean up after verification
nx.delete("/workspace/archive/docs/README.backup.md")
🔐 Permission-Aware Operations¶
import nexus
nx = nexus.connect(remote_url="http://localhost:2026", api_key="admin-key")
# Grant user read-only access
nx.rebac_create("user", "alice", "direct_viewer", "file", "/workspace/hello.txt")
# Alice can read
alice_nx = nexus.connect(remote_url="http://localhost:2026", api_key="alice-key")
content = alice_nx.read("/workspace/hello.txt") # ✓ Works
# Alice cannot write
try:
alice_nx.write("/workspace/hello.txt", b"Update") # ✗ Denied
except nexus.PermissionError:
print("Write denied - viewer has read-only access")
# Upgrade to editor
nx.rebac_create("user", "alice", "direct_editor", "file", "/workspace/hello.txt")
# Now Alice can write
alice_nx.write("/workspace/hello.txt", b"Updated by Alice") # ✓ Works
🏃 Run the Full Demo¶
Try the complete interactive demo script:
# Start server
./scripts/init-nexus-with-auth.sh
# In another terminal
source .nexus-admin-env
./examples/cli/file_operations_demo.sh
The demo covers: - ✅ Write operations (inline, stdin, file, JSON, binary) - ✅ Read operations (basic, with metadata) - ✅ Copy operations (simple, cross-directory) - ✅ Move/rename operations - ✅ Delete operations - ✅ Optimistic concurrency control - ✅ Permission-aware operations
📚 What's Next?¶
- Directory Operations - Learn hierarchical permission inheritance
- Permissions Example - Master ReBAC access control
- API Reference - Complete API documentation