Oblien Docs

File Reading

Read file contents with support for line ranges, line numbers, and encoding detection.

Read Entire File

// Using SDK
const file = await sandbox.files.get({
  filePath: '/opt/app/src/index.js'
});

console.log(file.content);
POST /files/get HTTP/1.1
Authorization: Bearer YOUR_TOKEN
Content-Type: application/json

{
  "filePath": "/opt/app/src/index.js"
}

Response:

{
  "success": true,
  "content": "import express from 'express';\n\nconst app = express();\napp.listen(3000);",
  "path": "/opt/app/src/index.js",
  "size": 1245,
  "encoding": "utf-8",
  "lines": 42
}

Read with Line Numbers

// Using SDK
const file = await sandbox.files.get({
  filePath: '/opt/app/src/index.js',
  withLineNumbers: true
});

console.log(file.content);
// Output:
//   1| import express from 'express';
//   2|
//   3| const app = express();
//   4| app.listen(3000);
POST /files/get HTTP/1.1
Authorization: Bearer YOUR_TOKEN
Content-Type: application/json

{
  "filePath": "/opt/app/src/index.js",
  "withLineNumbers": true
}

Read Line Range

// Using SDK - read lines 10-20
const file = await sandbox.files.get({
  filePath: '/opt/app/src/server.js',
  range: {
    start: 10,
    end: 20
  },
  withLineNumbers: true
});
POST /files/get HTTP/1.1
Authorization: Bearer YOUR_TOKEN
Content-Type: application/json

{
  "filePath": "/opt/app/src/server.js",
  "range": {
    "start": 10,
    "end": 20
  },
  "withLineNumbers": true
}

Response:

{
  "success": true,
  "content": "  10| const port = process.env.PORT;\n  11| const server = app.listen(port);",
  "path": "/opt/app/src/server.js",
  "range": { "start": 10, "end": 20 },
  "linesReturned": 11
}

Read Large Files

For large files, read in chunks:

const chunkSize = 100;
let currentLine = 1;
let hasMore = true;

while (hasMore) {
  const chunk = await sandbox.files.get({
    filePath: '/opt/app/large-file.log',
    range: {
      start: currentLine,
      end: currentLine + chunkSize - 1
    }
  });
  
  console.log(chunk.content);
  
  currentLine += chunkSize;
  hasMore = chunk.linesReturned === chunkSize;
}

Common Use Cases

Read Configuration

// Read and parse JSON config
const config = await sandbox.files.get({
  filePath: '/opt/app/config.json'
});

const parsed = JSON.parse(config.content);
console.log('Port:', parsed.port);

Read Package.json

const pkg = await sandbox.files.get({
  filePath: '/opt/app/package.json'
});

const packageData = JSON.parse(pkg.content);
console.log('Dependencies:', Object.keys(packageData.dependencies));

Read .env File

const envFile = await sandbox.files.get({
  filePath: '/opt/app/.env'
});

// Parse env variables
const envVars = {};
envFile.content.split('\n').forEach(line => {
  const [key, value] = line.split('=');
  if (key && value) {
    envVars[key.trim()] = value.trim();
  }
});

console.log('Environment:', envVars);

Read Code with Context

// Find a function and read surrounding code
const search = await sandbox.search({
  query: 'function handleRequest',
  options: { maxResults: 1 }
});

if (search.matches.length > 0) {
  const match = search.matches[0];
  
  // Read function with context (10 lines before/after)
  const code = await sandbox.files.get({
    filePath: match.file,
    range: {
      start: Math.max(1, match.line - 10),
      end: match.line + 10
    },
    withLineNumbers: true
  });
  
  console.log(code.content);
}

Read Multiple Files

// Read several files in parallel
const files = [
  '/opt/app/package.json',
  '/opt/app/README.md',
  '/opt/app/.gitignore'
];

const contents = await Promise.all(
  files.map(path => sandbox.files.get({ filePath: path }))
);

contents.forEach((file, index) => {
  console.log(`\n=== ${files[index]} ===`);
  console.log(file.content);
});

Binary Files

For binary files, content is returned as base64:

const image = await sandbox.files.get({
  filePath: '/opt/app/logo.png'
});

// Decode base64
const buffer = Buffer.from(image.content, 'base64');
console.log('Image size:', buffer.length, 'bytes');

Error Handling

try {
  const file = await sandbox.files.get({
    filePath: '/opt/app/nonexistent.js'
  });
} catch (error) {
  if (error.status === 404) {
    console.log('File not found');
  } else if (error.message.includes('Permission denied')) {
    console.log('No permission to read file');
  } else if (error.message.includes('Is a directory')) {
    console.log('Cannot read directory as file');
  }
}

Performance Tips

1. Use Line Ranges for Large Files

// Good - read only needed lines
await sandbox.files.get({
  filePath: 'large.log',
  range: { start: 1, end: 100 }
});

// Bad - loads entire file into memory
await sandbox.files.get({ filePath: 'large.log' });

2. Check File Size First

const listing = await sandbox.files.list({ dirPath: '/opt/app' });
const file = listing.files.find(f => f.name === 'data.json');

if (file.size > 10 * 1024 * 1024) { // > 10 MB
  console.log('File too large, reading in chunks');
  // Read in chunks...
} else {
  const content = await sandbox.files.get({ filePath: file.path });
}

3. Cache Frequently Read Files

const cache = new Map();

async function readWithCache(filePath) {
  if (cache.has(filePath)) {
    return cache.get(filePath);
  }
  
  const file = await sandbox.files.get({ filePath });
  cache.set(filePath, file);
  return file;
}

Next Steps