ruby_client/README.md
Official Ruby client library for FastDFS - A high-performance distributed file system.
Add this line to your application's Gemfile:
gem 'fastdfs'
And then execute:
bundle install
Or install it directly:
gem install fastdfs
require 'fastdfs'
# Create client configuration
config = FastDFS::ClientConfig.new(
tracker_addrs: [
'192.168.1.100:22122',
'192.168.1.101:22122'
],
max_conns: 10,
connect_timeout: 5.0,
network_timeout: 30.0
)
# Initialize client
client = FastDFS::Client.new(config)
begin
# Upload a file
file_id = client.upload_file('test.jpg')
puts "File uploaded: #{file_id}"
# Download the file
data = client.download_file(file_id)
puts "Downloaded #{data.bytesize} bytes"
# Delete the file
client.delete_file(file_id)
puts "File deleted"
ensure
# Always close the client
client.close
end
# Upload data from memory
data = "Hello, FastDFS!"
file_id = client.upload_buffer(data, 'txt')
# Upload with metadata
metadata = {
'author' => 'John Doe',
'date' => '2025-01-01'
}
file_id = client.upload_file('document.pdf', metadata)
# Download and save to local file
client.download_to_file(file_id, '/path/to/save/image.jpg')
# Download specific byte range
data = client.download_file_range(file_id, 0, 1024) # First 1024 bytes
# Check if file exists
if client.file_exists?(file_id)
puts "File exists"
# Get file information
info = client.get_file_info(file_id)
puts "Size: #{info.file_size} bytes"
puts "Created: #{info.create_time}"
puts "CRC32: #{info.crc32}"
end
config = FastDFS::ClientConfig.new(
# Tracker server addresses (required)
tracker_addrs: ['192.168.1.100:22122'],
# Maximum connections per server (default: 10)
max_conns: 10,
# Connection timeout in seconds (default: 5.0)
connect_timeout: 5.0,
# Network I/O timeout in seconds (default: 30.0)
network_timeout: 30.0,
# Idle connection timeout in seconds (default: 60.0)
idle_timeout: 60.0,
# Retry count for failed operations (default: 3)
retry_count: 3
)
The client provides detailed error types:
begin
client.upload_file('test.jpg')
rescue FastDFS::FileNotFoundError => e
puts "File not found: #{e.message}"
rescue FastDFS::NetworkError => e
puts "Network error: #{e.message}"
rescue FastDFS::ClientClosedError => e
puts "Client is closed: #{e.message}"
rescue FastDFS::Error => e
puts "FastDFS error: #{e.message}"
end
ClientClosedError - 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 is offlineTrackerServerOfflineError - Tracker server is offlineInsufficientSpaceError - Insufficient storage spaceFileAlreadyExistsError - File already existsInvalidMetadataError - Invalid metadata formatOperationNotSupportedError - Operation not supportedInvalidArgumentError - Invalid argumentProtocolError - Protocol-level errorNetworkError - Network-related errorStorageError - Storage server errorTrackerError - Tracker server errorThe client automatically manages connection pools for optimal performance:
The client is fully thread-safe and can be used concurrently from multiple threads:
require 'thread'
threads = []
10.times do |i|
threads << Thread.new do
file_id = client.upload_file("file#{i}.txt")
puts "Uploaded: #{file_id}"
end
end
threads.each(&:join)
See the examples directory for complete usage examples:
Run the test suite:
# Unit tests
bundle exec rake test
# Integration tests (requires running FastDFS cluster)
bundle exec rake test:integration
The client is optimized for performance:
Contributions are welcome! Please see CONTRIBUTING.md for details.
GNU General Public License V3 - see LICENSE for details.