javascript_client/README.md
Official JavaScript/Node.js client library for FastDFS distributed file system.
npm install fastdfs-client
Or with yarn:
yarn add fastdfs-client
const { Client } = require('fastdfs-client');
// Create client
const client = new Client({
trackerAddrs: ['192.168.1.100:22122']
});
async function example() {
try {
// Upload a file
const fileId = await client.uploadBuffer(
Buffer.from('Hello, FastDFS!'),
'txt'
);
console.log('Uploaded:', fileId);
// Download the file
const data = await client.downloadFile(fileId);
console.log('Downloaded:', data.toString());
// Delete the file
await client.deleteFile(fileId);
console.log('Deleted');
} finally {
await client.close();
}
}
example();
const client = new Client({
// Required: List of tracker server addresses
trackerAddrs: ['192.168.1.100:22122', '192.168.1.101:22122'],
// Optional: Maximum connections per server (default: 10)
maxConns: 10,
// Optional: Connection timeout in milliseconds (default: 5000)
connectTimeout: 5000,
// Optional: Network I/O timeout in milliseconds (default: 30000)
networkTimeout: 30000,
// Optional: Idle connection timeout in milliseconds (default: 60000)
idleTimeout: 60000,
// Optional: Number of retries for failed operations (default: 3)
retryCount: 3
});
uploadFile(localFilename, metadata?)Upload a file from the local filesystem.
const fileId = await client.uploadFile('/path/to/file.jpg');
// With metadata
const fileId = await client.uploadFile('/path/to/file.jpg', {
author: 'John Doe',
date: '2025-01-01'
});
uploadBuffer(data, fileExtName, metadata?)Upload data from a Buffer.
const data = Buffer.from('Hello, World!');
const fileId = await client.uploadBuffer(data, 'txt');
uploadAppenderFile(localFilename, metadata?)Upload an appender file that can be modified later.
const fileId = await client.uploadAppenderFile('/path/to/log.txt');
uploadAppenderBuffer(data, fileExtName, metadata?)Upload an appender file from a Buffer.
const data = Buffer.from('Initial log content\n');
const fileId = await client.uploadAppenderBuffer(data, 'log');
uploadSlaveFile(masterFileId, prefixName, fileExtName, data, metadata?)Upload a slave file (thumbnail, preview, etc.) associated with a master file.
const thumbnailData = createThumbnail(imageData);
const thumbId = await client.uploadSlaveFile(
masterFileId,
'thumb',
'jpg',
thumbnailData
);
downloadFile(fileId)Download a complete file.
const data = await client.downloadFile(fileId);
downloadFileRange(fileId, offset, length)Download a specific byte range.
// Download first 1024 bytes
const header = await client.downloadFileRange(fileId, 0, 1024);
// Download from offset 1000 to end
const tail = await client.downloadFileRange(fileId, 1000, 0);
downloadToFile(fileId, localFilename)Download and save to local filesystem.
await client.downloadToFile(fileId, '/path/to/save/file.jpg');
deleteFile(fileId)Delete a file from FastDFS.
await client.deleteFile(fileId);
getFileInfo(fileId)Get file information (size, create time, CRC32, source IP).
const info = await client.getFileInfo(fileId);
console.log('Size:', info.fileSize);
console.log('Created:', info.createTime);
console.log('CRC32:', info.crc32);
console.log('Source IP:', info.sourceIpAddr);
fileExists(fileId)Check if a file exists.
const exists = await client.fileExists(fileId);
setMetadata(fileId, metadata, flag?)Set or update file metadata.
// Overwrite all metadata (default)
await client.setMetadata(fileId, {
author: 'John Doe',
version: '2.0'
}, 'OVERWRITE');
// Merge with existing metadata
await client.setMetadata(fileId, {
tags: 'important'
}, 'MERGE');
getMetadata(fileId)Retrieve file metadata.
const metadata = await client.getMetadata(fileId);
console.log('Author:', metadata.author);
appendFile(fileId, data)Append data to an appender file.
await client.appendFile(fileId, Buffer.from('New log entry\n'));
modifyFile(fileId, offset, data)Modify content at a specific offset.
await client.modifyFile(fileId, 0, Buffer.from('Modified header\n'));
truncateFile(fileId, size)Truncate file to specified size.
await client.truncateFile(fileId, 1024); // Truncate to 1KB
close()Close the client and release all resources.
await client.close();
The examples/ directory contains comprehensive examples:
Run an example:
node examples/01_basic_upload.js
The client provides detailed error types for different scenarios:
const {
Client,
FileNotFoundError,
NetworkError,
InvalidFileIDError,
ClientClosedError
} = require('fastdfs-client');
try {
await client.downloadFile(fileId);
} catch (error) {
if (error instanceof FileNotFoundError) {
console.log('File does not exist');
} else if (error instanceof NetworkError) {
console.log('Network communication failed');
} else if (error instanceof InvalidFileIDError) {
console.log('Invalid file ID format');
} else {
console.log('Other error:', error.message);
}
}
FastDFSError - Base error classClientClosedError - Client has been closedFileNotFoundError - File does not existNoStorageServerError - No storage server availableConnectionTimeoutError - Connection timeoutNetworkTimeoutError - Network I/O timeoutInvalidFileIDError - Invalid file ID formatInvalidResponseError - Invalid server responseStorageServerOfflineError - Storage server offlineTrackerServerOfflineError - Tracker server offlineInsufficientSpaceError - Insufficient storage spaceFileAlreadyExistsError - File already existsInvalidMetadataError - Invalid metadata formatOperationNotSupportedError - Operation not supportedInvalidArgumentError - Invalid argumentProtocolError - Protocol-level errorNetworkError - Network communication errorconst client = new Client(config);
try {
// Your operations
} finally {
await client.close();
}
The client automatically manages connection pooling. Configure maxConns based on your workload:
const client = new Client({
trackerAddrs: ['192.168.1.100:22122'],
maxConns: 50 // For high-concurrency applications
});
try {
const fileId = await client.uploadFile('file.jpg');
} catch (error) {
if (error instanceof NetworkError) {
// Retry or log for investigation
} else if (error instanceof InvalidArgumentError) {
// Fix the input
} else {
// Handle other errors
}
}
await client.uploadBuffer(data, 'jpg', {
userId: '12345',
uploadTime: new Date().toISOString(),
originalName: 'photo.jpg'
});
// Upload original
const originalId = await client.uploadBuffer(imageData, 'jpg');
// Upload thumbnail
const thumbData = await resizeImage(imageData, 150, 150);
const thumbId = await client.uploadSlaveFile(
originalId, 'thumb', 'jpg', thumbData
);
maxConns for high-concurrency scenariosThis project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)Made with ❤️ for the FastDFS community