Secure file transfer with SFTP in Lua using luasocket
Learn how to import files from FTP servers in Lua using the LuaSocket library. This guide provides practical examples and best practices for automating FTP file transfers in your Lua projects.
Setting up luasocket
LuaSocket is a Lua extension library that provides support for network operations, including FTP. You can install it using LuaRocks:
luarocks install luasocket
Connecting to an FTP server
To start working with FTP in Lua, you need to establish a connection to the FTP server using LuaSocket:
local socket = require("socket")
local ftp = require("socket.ftp")
local connection = {
host = "ftp.example.com",
user = "username",
password = "password",
port = 21
}
Listing files and directories
You can retrieve a directory listing from the FTP server using the ftp.get
function:
local ftp = require("socket.ftp")
local ltn12 = require("ltn12")
local function list_directory(path)
local t = {}
local ok, err = ftp.get{
host = connection.host,
user = connection.user,
password = connection.password,
path = path,
type = "a",
sink = ltn12.sink.table(t)
}
if not ok then
error("Failed to list directory: " .. err)
end
local listing = table.concat(t)
print("Directory Listing for " .. path .. ":")
print(listing)
end
list_directory("/")
This function retrieves the raw directory listing from the specified path and prints it to the console.
Downloading files from the FTP server
To download files from the FTP server, you can use the ftp.get
function along with a file sink:
local ftp = require("socket.ftp")
local ltn12 = require("ltn12")
local function download_file(remote_path, local_path)
local file = io.open(local_path, "wb")
if not file then
error("Failed to open local file for writing")
end
local ok, err = ftp.get{
host = connection.host,
user = connection.user,
password = connection.password,
path = remote_path,
sink = ltn12.sink.file(file)
}
if not ok then
error("Failed to download file: " .. err)
end
print("Downloaded " .. remote_path .. " to " .. local_path)
end
download_file("/remote/file.txt", "local_file.txt")
This function downloads a file from the FTP server and saves it locally.
Automating FTP file imports in your Lua projects
You can automate the process of importing files from an FTP server by combining the directory listing and file download functions. For example, you can download all files from a specific directory:
local ftp = require("socket.ftp")
local ltn12 = require("ltn12")
local lfs = require("lfs")
local function list_files(path)
local t = {}
local ok, err = ftp.get{
host = connection.host,
user = connection.user,
password = connection.password,
path = path,
type = "a",
sink = ltn12.sink.table(t)
}
if not ok then
error("Failed to list directory: " .. err)
end
local listing = table.concat(t)
local files = {}
for filename in listing:gmatch("(%S+)") do
table.insert(files, filename)
end
return files
end
local function download_all_files(remote_dir, local_dir)
lfs.mkdir(local_dir)
local files = list_files(remote_dir)
for _, file in ipairs(files) do
local remote_path = remote_dir .. "/" .. file
local local_path = local_dir .. "/" .. file
download_file(remote_path, local_path)
end
end
download_all_files("/remote/directory", "local_directory")
This script lists all files in the remote directory and downloads each one to a local directory.
Error handling and best practices
Implement robust error handling to make your FTP operations reliable:
local function safe_execute(func, ...)
local ok, result = pcall(func, ...)
if not ok then
print("Error: " .. result)
end
return ok, result
end
-- Usage example
safe_execute(download_file, "/remote/file.txt", "local_file.txt")
Using pcall
allows your script to handle errors gracefully without crashing.
Conclusion
By leveraging the LuaSocket library, you can efficiently import files from FTP servers in your Lua projects. Automating FTP file transfers can streamline data processing tasks and integrate external resources seamlessly.
At Transloadit, we offer powerful file processing capabilities through our encoding REST API, which can complement your Lua-based file transfer solutions with advanced file processing features.