This module implements an SSH FTP (SFTP) client. SFTP is a secure, encrypted file transfer service available for SSH.
Type definitions that are used more than once in this module, or abstractions to indicate the intended use of the data type, or both:
Opaque to the user, returned by
=
If the request functions for the SFTP channel return
If no connection reference is provided, a connection is set
up, and the new connection is returned. An SSH channel process
is started to handle the communication with the SFTP server.
The returned
Options:
The time-out is passed to the
Desired SFTP protocol version. The actual version is the minimum of the desired version and the maximum supported versions by the SFTP server.
All other options are directly passed to
Stops an SFTP channel. Does not close the SSH connection.
Use
Reads a file from the server, and returns the data in a binary,
like
Writes a file to the server, like
Lists the given directory on the server, returning the filenames as a list of strings.
Opens a file on the server and returns a handle, which can be used for reading or writing.
Opens a handle to a directory on the server. The handle can be used for reading directory contents.
Opens a handle to a tar file on the server, associated with
Example of writing and then reading a tar file follows:
{ok,HandleWrite} = ssh_sftp:open_tar(ChannelPid, ?tar_file_name, [write]),
ok = erl_tar:add(HandleWrite, .... ),
ok = erl_tar:add(HandleWrite, .... ),
...
ok = erl_tar:add(HandleWrite, .... ),
ok = erl_tar:close(HandleWrite),
%% And for reading
{ok,HandleRead} = ssh_sftp:open_tar(ChannelPid, ?tar_file_name, [read]),
{ok,NameValueList} = erl_tar:extract(HandleRead,[memory]),
ok = erl_tar:close(HandleRead),
The
The
The
The previous write and read example can be extended with encryption and decryption as follows:
%% First three parameters depending on which crypto type we select:
Key = <<"This is a 256 bit key. abcdefghi">>,
Ivec0 = crypto:rand_bytes(16),
DataSize = 1024, % DataSize rem 16 = 0 for aes_cbc
%% Initialization of the CryptoState, in this case it is the Ivector.
InitFun = fun() -> {ok, Ivec0, DataSize} end,
%% How to encrypt:
EncryptFun =
fun(PlainBin,Ivec) ->
EncryptedBin = crypto:block_encrypt(aes_cbc256, Key, Ivec, PlainBin),
{ok, EncryptedBin, crypto:next_iv(aes_cbc,EncryptedBin)}
end,
%% What to do with the very last block:
CloseFun =
fun(PlainBin, Ivec) ->
EncryptedBin = crypto:block_encrypt(aes_cbc256, Key, Ivec,
pad(16,PlainBin) %% Last chunk
),
{ok, EncryptedBin}
end,
Cw = {InitFun,EncryptFun,CloseFun},
{ok,HandleWrite} = ssh_sftp:open_tar(ChannelPid, ?tar_file_name, [write,{crypto,Cw}]),
ok = erl_tar:add(HandleWrite, .... ),
ok = erl_tar:add(HandleWrite, .... ),
...
ok = erl_tar:add(HandleWrite, .... ),
ok = erl_tar:close(HandleWrite),
%% And for decryption (in this crypto example we could use the same InitFun
%% as for encryption):
DecryptFun =
fun(EncryptedBin,Ivec) ->
PlainBin = crypto:block_decrypt(aes_cbc256, Key, Ivec, EncryptedBin),
{ok, PlainBin, crypto:next_iv(aes_cbc,EncryptedBin)}
end,
Cr = {InitFun,DecryptFun},
{ok,HandleRead} = ssh_sftp:open_tar(ChannelPid, ?tar_file_name, [read,{crypto,Cw}]),
{ok,NameValueList} = erl_tar:extract(HandleRead,[memory]),
ok = erl_tar:close(HandleRead),
Closes a handle to an open file or directory on the server.
Reads
If the file is read past
The
Reads from an open file, without waiting for the result. If the
handle is valid, the function returns
The
Writes
Typical error reasons:
File is not opened for writing.
No space is left on the device.
Writes to an open file, without waiting for the result. If the
handle is valid, the function returns
Sets the file position of the file referenced by
The same as
Absolute offset.
Offset from the current position.
Offset from the end of file.
The same as eariler with
Returns a
Returns a
Writes file information from a
Reads the link target from the symbolic link specified
by
Creates a symbolic link pointing to
Renames a file named
Deletes the file specified by
Creates a directory specified by
Deletes a directory specified by