Skip to content

SFTPClient API Documentation

SFTPClient.SFTP Method
julia
SFTP(url::AbstractString, username::AbstractString, public_key_file::AbstractString, private_key_file::AbstractString;disable_verify_peer=false, disable_verify_host=false, verbose=false)

Creates a new SFTP client using certificate authentication, and keys in the files specified

sftp = SFTP("sftp://mysitewhereIhaveACertificate.com", "myuser", "test.pub", "test.pem")

source
SFTPClient.SFTP Method
julia
SFTP(url::AbstractString, username::AbstractString, password::AbstractString;create_known_hosts_entry=true, disable_verify_peer=false, disable_verify_host=false)

Creates a new SFTP Client: url: The url to connect to, e.g., sftp://mysite.com username: The username to use password: The users password create_known_hosts_entry: Automatically create an entry in known hosts

Example: sftp = SFTP("sftp://test.rebex.net", "demo", "password")

source
SFTPClient.SFTP Method
julia
SFTP(url::AbstractString, username::AbstractString;disable_verify_peer=false, disable_verify_host=false)

Creates a new SFTP client using certificate authentication.

sftp = SFTP("sftp://mysitewhereIhaveACertificate.com", "myuser")

Note! You must provide the username for this to work.

Before using this method, you must set up your certificates in ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub

Of course, the host need to be in the known_hosts file as well.

Test using your local client first: ssh myuser@mysitewhereIhaveACertificate.com

See other method if you want to use files not in ~/ssh/

source
Base.Filesystem.cd Method
julia
cd(sftp::SFTP, dir::AbstractString)

Change the directory for the SFTP client.
source
Base.Filesystem.filemode Method
julia
Base.isdir(st::SFTPStatStruct)

Get the filemode of the directory

source
Base.Filesystem.isdir Method
julia
Base.isdir(st::SFTPStatStruct)

Test if st is a directory

source
Base.Filesystem.isfile Method
julia
Base.isfile(st::SFTPStatStruct)

Test if st is a file

source
Base.Filesystem.mkdir Method
julia
mkdir(sftp::SFTP, dir::AbstractString)

Create a directory
source
Base.Filesystem.mv Method
julia
mv(
sftp::SFTP,
old_name::AbstractString,
new_name::AbstractString
)

Move, i.e., rename the file.

source
Base.Filesystem.readdir Function
julia
readdir(sftp::SFTP, join::Bool = false, sort::Bool = true)

Reads the current directory. Returns a vector of Strings just like the regular readdir function.

source
Base.Filesystem.rm Method
julia
rm(sftp::SFTP, file_name::AbstractString)

Remove (delete) the file
source
SFTPClient.download Function
julia
SFTPClient.download(
sftp::SFTP,
file_name::AbstractString,
 output = tempname();downloadDir::Union{String, Nothing}=nothing)

 Download a file. You can download it and use it directly, or save it to a file. 
 Specify downloadDir if you want to save downloaded files. You can also use broadcasting.
Example:

sftp = SFTP("sftp://test.rebex.net/pub/example/", "demo", "password")
files=readdir(sftp)
downloadDir="/tmp"
SFTPClient.download.(sftp, files, downloadDir=downloadDir)

You can also use it like this:
df=DataFrame(CSV.File(SFTPClient.download(sftp, "/mydir/test.csv")))
source
SFTPClient.rmdir Method
julia
rmdir(sftp::SFTP, dir_name::AbstractString)

Remove (delete) the directory
source
SFTPClient.sftpstat Method
julia
sftpstat(sftp::SFTP, path::AbstractString)

Like Julia stat, but returns a Vector of SFTPStatStructs. Note that you can only run this on directories. Can be used for checking if a file was modified, and much more.

source
SFTPClient.upload Method
julia
upload(sftp::SFTP, file_name::AbstractString)

Upload (put) a file to the server. Broadcasting can be used too.

files=readdir() upload.(sftp,files)

source
SFTPClient.walkdir Method
julia
SFTPClient.walkdir(sftp::SFTP, root; topdown=true, follow_symlinks=false, onerror=throw)
Return an iterator that walks the directory tree of a directory.
The iterator returns a tuple containing `(rootpath, dirs, files)`.


# Examples
```julia
for (root, dirs, files) in walkdir(sftp, ".")
    println("Directories in $root")
    for dir in dirs
        println(joinpath(root, dir)) # path to directories
    end
    println("Files in $root")
    for file in files
        println(joinpath(root, file)) # path to files
    end
end
```
source