Versions Compared

Key

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

...

STEP 2: Translator Code
Expand
titleSTEP 1: Initialize connection

Use the net.sftp.init{} command to initialize the connection. You are only need required to use the following five parameters:

Parameter

Description

Server

The server to connect to. string

Username

The remote user to login as. string

Password

The password to authenticate with. string

Port

The port to use (default = 22). integer

Timeout

Timeout for operations (default = 15 seconds). integer

Expand
title

Your net.sftp.init{} code will look something like this:

Code Block
languagelua
local Conn = net.sftp.init{
  server=Destination_HostName,
  username=Destination_Login,
  password=Destination_Password,
  port=Destination_Port,
  timeout = 60,
  }

For optimal security, use TLS/SSL enabled connections. See Enable TLS/SSL with the Network Client APIs.

Expand
titleSTEP 32: 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 listList, get Get and put 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().