Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Expand
titleSTEP 2: Send and receive files via SFTP/FTP

When working with files Here is a sample script showing how you can use the net api to List, Get and Put a file via SFTP:

Code Block
languagelua
function main(Data)
  
  -- (1) Initialize Connection
    local Conn = net.sftp.init{
    server=Destination_HostName,
    username=Destination_Login,
    password=Destination_Password,
    port=Destination_Port,
    timeout = 60,
  }

  -- (2) List files from SFTP Server
  local list = Conn:list{remote_path="/"}

  -- (3) Get a file from the SFTP server
  local get = Conn:get{remote_path='afile.txt'}

  -- (4) Put a file on the SFTP Server
  local put = Conn:put{remote_path='sample.txt', data=Data}

end
Expand
titleSTEP 3: Processing Files

Use io.read() to read the files received or io.write() to process accordingly. Always close file handles to reduce the overhead of unclosed connections. See File Operations and the FIL Library.

Expand
titleSTEP 4: Handle Processed Files

Once processing is complete you can will typically want to move or delete the the files to avoid reprocessing.

For this, the net API supports renaming and removing files – net.*.rename()net.*.delete().

Moving Files:

Code Block
languagelua
local fileName = 'afile.txt'
local newDirectory = '/processed/'..fileName

Conn:rename{old_path=sourcePath, new_path=newDirectory}
iguana.logInfo("File moved to "..newDirectory)

Deleting Files:

Code Block
local fileName = 'afile.txt'
local sourcePath = '/'..fileName

Conn:delete{remote_path=sourcePath}
iguana.logInfo("File "..fileName.." deleted from server.")