SFTPClient (SFTP Client v1.4.6) View Source

This is an SFTP Client that wraps Erlang's :ssh and :ssh_sftp libraries for easier usage in Elixir.

Connect & Disconnect

To open a new connection to an SFTP server:

iex> {:ok, conn} = SFTPClient.connect(host: "ftp.myhost.com")
{:ok, %SFTPClient.Conn{}}

Refer to the docs for SFTPClient.Operations.Connect.connect/1 to find out all available options.

It is strongly recommended to close a connection after your operations have completed:

iex> SFTPClient.disconnect(conn)
:ok

For short-lived connections you can also use a function as second argument. After the function body has run or raises, the connection is automatically closed.

iex> SFTPClient.connect([host: "ftp.myhost.com"], fn conn ->
...>   # Do something with conn
...>   "my result"
...> end)
{:ok, "my result"}

Download

You can download a file from the server you can use the following function.

iex> SFTPClient.download_file(conn, "my/remote/dir/file.jpg", "my/dir/local-file.jpg")
{:ok, "my/dir/local-file.jpg"}

When the third argument is an existing directory on your file system, the file is downloaded to a file with the same name as the one on the server.

iex> SFTPClient.download_file(conn, "my/remote/dir/image.png", "my/local/dir")
{:ok, "my/local/dir/image.png"}

It is also possible to use Streams to download data into a file or memory.

iex> SFTPClient.stream_file!(conn, "my/remote/file.jpg")
...> |> Stream.into(File.stream!("my/local/file.jpg"))
...> |> Stream.run()
:ok

Upload

To upload are file from the file system you can use the following function.

iex> SFTPClient.upload_file(conn, "my/local/dir/file.jpg", "my/remote/dir/file.jpg")
{:ok, "my/remote/dir/file.jpg"}

You can also use Streams to upload data. Please make sure to set a proper chunk size or the upload may be slow.

iex> File.stream!("my/local/file.jpg")
...> |> Stream.into(SFTPClient.stream_file!(conn, "my/remote/file.jpg"))
...> |> Stream.run()
:ok

List Directory

iex> SFTPClient.list_dir(conn, "my/dir")
{:ok, ["my/dir/file_1.jpg", "my/dir/file_2.jpg"]}

Create Directory

iex> SFTPClient.make_dir(conn, "my/new/dir")
:ok

Note that this operation fails unless the parent directory exists.

Delete

To delete a file:

iex> SFTPClient.delete_file(conn, "my/remote/file.jpg")
:ok

To delete a directory:

iex> SFTPClient.delete_dir(conn, "my/remote/dir")
:ok

Note that a directory cannot be deleted as long as it still contains files.

Rename

To move/rename a file or directory:

iex> SFTPClient.rename(conn, "my/remote/file.jpg", "my/remote/new-file.jpg")
:ok

File Info

You can retrieve meta data about a file from the server such as file size, modification time, file permissions, owner and so on.

iex> SFTPClient.file_info(conn, "my/remote/file.jpg")
{:ok, %File.Stat{}}

Refer to the File.Stat docs for a list of available file information.

There are also a couple of functions that handle symbolic links.

It is possible to get the target of a symlink.

iex> SFTPClient.read_link(conn, "my/remote/link.jpg")
{:ok, "my/remote/file.jpg"}

You can retrieve meta data about symlinks, similar to file_info/2.

iex> SFTPClient.link_info(conn, "my/remote/link.jpg")
{:ok, %File.Stat{}}

And you are able to create symlinks.

iex> SFTPClient.make_link(conn, "my/remote/link.jpg", "my/remote/file.jpg")
:ok

Link to this section Summary

Types

File access modes that can be used when opening files directly from the remote server.

A type incorporating all error types.

Functions

Link to this section Types

Specs

access_mode() :: :read | :write | :creat | :trunc | :append | :binary

File access modes that can be used when opening files directly from the remote server.

Specs

A type incorporating all error types.

Link to this section Functions

See SFTPClient.Operations.CloseHandle.close_handle/1.

See SFTPClient.Operations.CloseHandle.close_handle!/1.

See SFTPClient.Operations.Connect.connect/1.

Link to this function

connect(config_or_opts, fun)

View Source

See SFTPClient.Operations.Connect.connect/2.

Link to this function

connect!(config_or_opts)

View Source

See SFTPClient.Operations.Connect.connect!/1.

Link to this function

connect!(config_or_opts, fun)

View Source

See SFTPClient.Operations.Connect.connect!/2.

See SFTPClient.Operations.DeleteDir.delete_dir/2.

See SFTPClient.Operations.DeleteDir.delete_dir!/2.

See SFTPClient.Operations.DeleteFile.delete_file/2.

Link to this function

delete_file!(conn, path)

View Source

See SFTPClient.Operations.DeleteFile.delete_file!/2.

See SFTPClient.Operations.Disconnect.disconnect/1.

Link to this function

download_file(conn, remote_path, local_path)

View Source

See SFTPClient.Operations.DownloadFile.download_file/3.

Link to this function

download_file!(conn, remote_path, local_path)

View Source

See SFTPClient.Operations.DownloadFile.download_file!/3.

See SFTPClient.Operations.FileInfo.file_info/2.

See SFTPClient.Operations.FileInfo.file_info!/2.

See SFTPClient.Operations.ListDir.list_dir/2.

See SFTPClient.Operations.ListDir.list_dir!/2.

See SFTPClient.Operations.MakeDir.make_dir/2.

See SFTPClient.Operations.MakeDir.make_dir!/2.

Link to this function

make_link(conn, symlink_path, target_path)

View Source

See SFTPClient.Operations.MakeLink.make_link/3.

Link to this function

make_link!(conn, symlink_path, target_path)

View Source

See SFTPClient.Operations.MakeLink.make_link!/3.

See SFTPClient.Operations.OpenDir.open_dir/2.

Link to this function

open_dir(conn, path, fun)

View Source

See SFTPClient.Operations.OpenDir.open_dir/3.

See SFTPClient.Operations.OpenDir.open_dir!/2.

Link to this function

open_dir!(conn, path, fun)

View Source

See SFTPClient.Operations.OpenDir.open_dir!/3.

Link to this function

open_file(conn, path, modes)

View Source

See SFTPClient.Operations.OpenFile.open_file/3.

Link to this function

open_file(conn, path, modes, fun)

View Source

See SFTPClient.Operations.OpenFile.open_file/4.

Link to this function

open_file!(conn, path, modes)

View Source

See SFTPClient.Operations.OpenFile.open_file!/3.

Link to this function

open_file!(conn, path, modes, fun)

View Source

See SFTPClient.Operations.OpenFile.open_file!/4.

See SFTPClient.Operations.ReadDir.read_dir/1.

See SFTPClient.Operations.ReadDir.read_dir!/1.

See SFTPClient.Operations.ReadFile.read_file/2.

See SFTPClient.Operations.ReadFile.read_file!/2.

Link to this function

read_file_chunk(handle, length)

View Source

See SFTPClient.Operations.ReadFileChunk.read_file_chunk/2.

Link to this function

read_file_chunk!(handle, length)

View Source

See SFTPClient.Operations.ReadFileChunk.read_file_chunk!/2.

See SFTPClient.Operations.ReadLink.read_link/2.

See SFTPClient.Operations.ReadLink.read_link!/2.

Link to this function

rename(conn, old_path, new_path)

View Source

See SFTPClient.Operations.Rename.rename/3.

Link to this function

rename!(conn, old_path, new_path)

View Source

See SFTPClient.Operations.Rename.rename!/3.

This function is deprecated. Use stream_file!/2 instead.

See SFTPClient.Operations.StreamFile.stream_file/2.

Link to this function

stream_file(conn, path, chunk_size)

View Source
This function is deprecated. Use stream_file!/3 instead.

See SFTPClient.Operations.StreamFile.stream_file/3.

Link to this function

stream_file!(conn, path)

View Source

See SFTPClient.Operations.StreamFile.stream_file!/2.

Link to this function

stream_file!(conn, path, chunk_size)

View Source

See SFTPClient.Operations.StreamFile.stream_file!/3.

Link to this function

upload_file(conn, local_path, remote_path)

View Source

See SFTPClient.Operations.UploadFile.upload_file/3.

Link to this function

upload_file!(conn, local_path, remote_path)

View Source

See SFTPClient.Operations.UploadFile.upload_file!/3.

Link to this function

write_file(conn, path, data)

View Source

See SFTPClient.Operations.WriteFile.write_file/3.

Link to this function

write_file!(conn, path, data)

View Source

See SFTPClient.Operations.WriteFile.write_file!/3.

Link to this function

write_file_chunk(handle, data)

View Source

See SFTPClient.Operations.WriteFileChunk.write_file_chunk/2.

Link to this function

write_file_chunk!(handle, data)

View Source

See SFTPClient.Operations.WriteFileChunk.write_file_chunk!/2.