sftp_ex v0.2.6 SftpEx

Functions for transferring and managing files through SFTP

Summary

Functions

Creates a Connection struct if the connection is successful, else will return {:error, reason}

Stops the SSH application

Download a file given the connection and remote_path Returns {:ok, data}, {:error, reason}

Gets the type given a remote path. Types: connection = Connection remote_path = handle() or string()

Lists the contents of a directory given a connection a handle or remote path

Types: connection = Connection remote_path = string() or handle()

Lists the contents of a directory given a connection a handle or remote path Types: connection = Connection remote_path = string()

Opens a file or directory given a connection and remote_path

Renames a file or directory

Removes a file from the server. Types:

connection = Connection
file = string()

Removes a directory and all files within it Types:

connection = Connection
remote_path = string()

Types: connection = Connection remote_path = handle() or string()

Creates an SFTP stream by opening an SFTP connection and opening a file in read or write mode

Uploads data to a remote path via SFTP Returns :ok, or {:error, reason}

Functions

connect(opts)

Creates a Connection struct if the connection is successful, else will return {:error, reason}

A connection struct will contain the

channel_pid = pid()
connection_pid = pid()
host = string()
port = integer()
opts = [{Option, Value}]

Default values are set for the following options:

user_interaction: false, silently_accept_hosts: true, rekey_limit: 1000000000000, port: 22

*NOTE: The only required option is ‘:host’

The rekey_limit value is set at a large amount because the Erlang library creates an exception when the server is negotiating a rekey. Setting the value at a high number of bytes will avoid a rekey event occurring.

Other available options can be found at http://erlang.org/doc/man/ssh.html#connect-3

Returns {:ok, Connection}, or {:error, reason}

disconnect(connection)

Stops the SSH application

Types: connection = Connection

Returns :ok

download(connection, remote_path)

Download a file given the connection and remote_path Returns {:ok, data}, {:error, reason}

get_type(connection, remote_path)

Gets the type given a remote path. Types: connection = Connection remote_path = handle() or string()

type = :device | :directory | :regular | :other

Returns {:ok, type}, or {:error, reason}

ls(connection, remote_path)

Lists the contents of a directory given a connection a handle or remote path

Types: connection = Connection handle = handle() remote_path = string()

Returns {:ok, [Filename]}, or {:error, reason}

lstat(connection, remote_path)

Types: connection = Connection remote_path = string() or handle()

Returns {:ok, File.Stat}, or {:error, reason}

mkdir(connection, remote_path)

Lists the contents of a directory given a connection a handle or remote path Types: connection = Connection remote_path = string()

Returns :ok, or {:error, reason}

open(connection, remote_path)

Opens a file or directory given a connection and remote_path

Types: connection = Connection handle = handle() remote_path = string()

Returns {:ok, handle}, or {:error, reason}

rename(connection, old_name, new_name)

Renames a file or directory

Types:

connection = Connection
old_name = string()
new_name = string()

Returns {:ok, handle}, or {:error, reason}

rm(connection, file)

Removes a file from the server. Types:

connection = Connection
file = string()

Returns :ok, or {:error, reason}

rm_dir(connection, remote_path)

Removes a directory and all files within it Types:

connection = Connection
remote_path = string()

Returns :ok, or {:error, reason}

size(connection, remote_path)

Types: connection = Connection remote_path = handle() or string()

Returns size as {:ok, integer()} or {:error, reason}

stream!(connection, remote_path, byte_size \\ 32768)

Creates an SFTP stream by opening an SFTP connection and opening a file in read or write mode.

Below is an example of reading a file from a server.

An example of writing a file to a server is the following.

stream = File.stream!(“filename.txt”)

  |> Stream.into(SftpEx.stream!(connection,"/home/path/filename.txt"))
  |> Stream.run

This follows the same pattern as Elixir IO streams so a file can be transferred from one server to another via SFTP as follows.

stream = SftpEx.stream!(connection,”/home/path/filename.txt”) |> Stream.into(SftpEx.stream!(connection2,”/home/path/filename.txt”)) |> Stream.run

Types: connection = Connection remote_path = string()

Returns SFTP.Stream

upload(connection, remote_path, file_handle)

Uploads data to a remote path via SFTP Returns :ok, or {:error, reason}