Filesystem Access
The File System Access API enables reading, writing, and managing files.
The API allows interaction with local or network-based files. It provides methods for reading, writing, and managing directory structures using fsPromises
within the namespaceWrapper
class.
For a list of available fsPromises
methods, refer to: fsPromises Methods Names.
Available Methods
fs
Method
File system method to execute.
FS ensures that operations are performed within the task-specific folder
Parameters:
method
: ThefsPromise
method to invoke.path
: Target file or directory path....args
: Additional method-specific parameters.
Example:
- TypeScript
- JavaScript
import { namespaceWrapper } from "@_koii/namespace-wrapper";
// Create a directory
await namespaceWrapper.fs("mkdir", "uploads", { recursive: true });
// Copy a file
await namespaceWrapper.fs("copyFile", "uploads/handler.js", "utils/handler.js");
// Create a file
await namespaceWrapper.fs("writeFile", "uploads/handler.js");
const { namespaceWrapper } = require("@_koii/namespace-wrapper");
// Create a directory
await namespaceWrapper.fs("mkdir", "uploads", { recursive: true });
// Copy a file
await namespaceWrapper.fs("copyFile", "uploads/handler.js", "utils/handler.js");
// Create a file
await namespaceWrapper.fs("writeFile", "uploads/handler.js");
fsStaking
Method
A decentralized staking platform enabling secure token locking for rewards.
It retrieves information directly from the staking wallet's directory, ensuring all data and operations are specific to its structure and contents.
Parameters:
method
: ThefsPromise
method to call.path
: Target file or directory path....args
: Additional parameters.
Example:
- TypeScript
- JavaScript
import { namespaceWrapper } from "@_koii/namespace-wrapper";
const data = await namespaceWrapper.fsStaking(
'readFile',
'stake_info.txt',
'utf8',
)
console.log(data)
const { namespaceWrapper } = require("@_koii/namespace-wrapper");
const data = await namespaceWrapper.fsStaking(
'readFile',
'stake_info.txt',
'utf8',
)
console.log(data)
fsWriteStream
Method
Creates a write stream for file operations
Parameters:
imagepath
: Target image file path.
Example:
- TypeScript
- JavaScript
const getBasePath: string = await namespaceWrapper.getBasePath();
const imgPath: string = `${getBasePath}/img/output.jpg`;
try{
// Successful write stream creation and usage
const writeStream = await namespaceWrapper.fsWriteStream(imgPath);
if (writeStream) {
// Write image data
writeStream.write(imageBuffer);
writeStream.end();
writeStream.on('finish', () => {
console.log('Write completed');
// Output: Write completed
});
}
}
catch (error){
console.error("Error writing image:", error);
}
// Error case (invalid path)
try {
const invalidStream = await namespaceWrapper.fsWriteStream('/invalid/path/image.jpg');
} catch (error) {
console.error(error);
}
// Error case (permission denied)
try {
const restrictedStream = await namespaceWrapper.fsWriteStream('/root/restricted.jpg');
} catch (error) {
console.error(error);
}
const getBasePath = await namespaceWrapper.getBasePath();
const imgPath = `${getBasePath}/img/output.jpg`;
try{
// Successful write stream creation and usage
const writeStream = await namespaceWrapper.fsWriteStream(imgPath);
if (writeStream) {
// Write image data
writeStream.write(imageBuffer);
writeStream.end();
writeStream.on('finish', () => {
console.log('Write completed');
// Output: Write completed
});
}
}
catch (error) {
console.error("Error writing image:", error);
}
// Error case (invalid path)
try {
const invalidStream = await namespaceWrapper.fsWriteStream('/invalid/path/image.jpg');
} catch (error) {
console.error(error);
}
// Error case (permission denied)
try {
const restrictedStream = await namespaceWrapper.fsWriteStream('/root/restricted.jpg');
} catch (error) {
console.error(error);
}
fsReadStream
Method
Creates a read stream for file operations
Parameters:
imagepath
: Target image file path.
Example:
- TypeScript
- JavaScript
const getBasePath: string = await namespaceWrapper.getBasePath();
const imgPath: string = `${getBasePath}/img/output.jpg`;
try {
// Successful read
const imageBuffer: Buffer = await namespaceWrapper.fsReadStream(imgPath);
console.log(imageBuffer);
// Output: <Buffer ...>
} catch (error) {
console.error("Error reading image:", error);
}
// Error case (file not found)
try {
const buffer: Buffer = await namespaceWrapper.fsReadStream('nonexistent.jpg');
} catch (error) {
console.error("File not found:", error);
}
// Error case (corrupted file)
try {
const buffer: Buffer = await namespaceWrapper.fsReadStream('corrupted.jpg');
} catch (error) {
console.error("Corrupted file:", error);
}
const getBasePath = await namespaceWrapper.getBasePath();
const imgPath = `${getBasePath}/img/output.jpg`;
try {
// Successful read
const imageBuffer = await namespaceWrapper.fsReadStream(imgPath);
console.log(imageBuffer);
// Output: <Buffer ff d8 ff e0 00 10 4a 46 49 46 ...>
} catch (error) {
console.error("Error reading image:", error);
}
// Error case (file not found)
try {
const buffer = await namespaceWrapper.fsReadStream('nonexistent.jpg');
} catch (error) {
console.error("File not found:", error);
}
// Error case (corrupted file)
try {
const buffer = await namespaceWrapper.fsReadStream('corrupted.jpg');
} catch (error) {
console.error("Corrupted file:", error);
}
Summary
The File System Access API simplifies interaction with files and directories. Methods like fs
, fsStaking
, fsWriteStream
, and fsReadStream
provide a structured approach to managing file operations efficiently within Koii tasks.
Next Steps
To learn more about specific features, check out these guides:
- Blockchain/Transaction Operations - Work with blockchain and transaction operations.
- Task Status - Get task state information with namespace methods.
- Network/Task Handling - Manage network data and tasks.
- Audit and Distribution - Manage network data and tasks.