Lua
Lua
SFTP Upload and Download to a BinData Object
See more SFTP Examples
Demonstrates how to SFTP upload from a BinData object, and download into a BinData object.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local sftp = chilkat.SFtp.new()
-- Set some timeouts, in milliseconds:
sftp.connect_timeout_ms = 5000
sftp.idle_timeout_ms = 10000
-- Connect to the SSH server and then authenticate.
local port = 22
local success = sftp:connect("MY-SSH-SERVER-DOMAIN-OR-IP", port)
if success then
success = sftp:authenticate_pw("MY-SSH-LOGIN", "MY-SSH-PASSWORD")
if success then
success = sftp:initialize_sftp()
end
end
if not success then
print(sftp.last_error_text)
return
end
local bdA = chilkat.BinData.new()
bdA:load_file("qa_data/jpg/penguins.jpg")
-- Upload the contents of bdA to the SSH/SFTP server.
local remotePath = "sftpTesting/penguins.jpg"
success = sftp:upload_bd(bdA, remotePath)
if not success then
print(sftp.last_error_text)
return
end
-- Download the file..
local bdB = chilkat.BinData.new()
success = sftp:download_bd(remotePath, bdB)
if not success then
print(sftp.last_error_text)
return
end
-- Verify that bdA and bdB have the exact same contents.
print("size of bdA: " .. bdA.num_bytes)
if bdA:contents_equal(bdB) then
print("Contents are equal. Success.")
else
print("Contents are NOT equal. Failed.")
end
sftp:disconnect()