File Operations
Implementation Reference
Source Files:
packages/core/src/adapters/kubernetes-adapter.ts
- File copy implementationpackages/core/src/utils/kubernetes-api.ts
- Pod-level file operations
Key Functions:
KubernetesAdapter.copyFiles()
- Core file transfer implementationK8sPod.copyTo()
- Copy files from local to podK8sPod.copyFrom()
- Copy files from pod to local
Overview
Xec provides seamless file transfer capabilities for Kubernetes pods through kubectl cp
, enabling bidirectional file operations between local filesystem and pod containers. This is essential for configuration updates, log collection, backup operations, and deployment workflows.
Basic File Operations
Copy Files to Pod
Upload files from local filesystem to pod:
import { $ } from '@xec-sh/core';
const k8s = $.k8s({ namespace: 'default' });
const pod = k8s.pod('web-server');
// Copy single file to pod
await pod.copyTo('./config.json', '/app/config.json');
console.log('Configuration uploaded to pod');
// Copy with specific container
await pod.copyTo('./nginx.conf', '/etc/nginx/nginx.conf', 'nginx');
console.log('Nginx configuration uploaded');
Copy Files from Pod
Download files from pod to local filesystem:
// Copy single file from pod
await pod.copyFrom('/app/logs/application.log', './app-logs.log');
console.log('Application logs downloaded');
// Copy from specific container
await pod.copyFrom('/var/log/nginx/access.log', './nginx-access.log', 'nginx');
console.log('Nginx access logs downloaded');
Directory Operations
Copy Directories
Transfer entire directories between local and pod:
const pod = k8s.pod('application-server');
// Copy entire directory to pod
await pod.copyTo('./assets/', '/app/assets/');
console.log('Assets directory uploaded');
// Copy directory from pod
await pod.copyFrom('/app/generated-reports/', './reports/');
console.log('Reports directory downloaded');
Selective File Copy
Copy specific files based on patterns:
// Copy multiple configuration files
const configFiles = [
{ local: './config/app.json', remote: '/app/config/app.json' },
{ local: './config/database.json', remote: '/app/config/database.json' },
{ local: './config/redis.json', remote: '/app/config/redis.json' }
];
for (const { local, remote } of configFiles) {
await pod.copyTo(local, remote);
console.log(`Uploaded ${local} -> ${remote}`);
}