Class SftpClient
An SFTP session (version 3, as OpenSSH's sftp-server and internal-sftp speak it) over an SshClient: files,
directories, links and attributes, file streams, and pipelined uploads and downloads. Open one with
OpenSftpAsync(SshClient, CancellationToken); several can share a connection, and one may be used by several callers at once.
public sealed class SftpClient : IAsyncDisposable
- Inheritance
-
SftpClient
- Implements
- Inherited Members
Remarks
Remote paths are passed to the server as they are: relative ones are relative to the login directory, and / is the
separator, whatever this machine uses.
Properties
- Extensions
The protocol extensions the server announced, with their versions, for example
posix-rename@openssh.com=1.
- Limits
The server's limits (
limits@openssh.com), or the defaults every server accepts when it announces none.
- ProtocolVersion
The protocol version the server speaks (3 for OpenSSH).
Methods
- CopyFileAsync(string, string, bool, CancellationToken)
Copies
sourcePathtodestinationPathon the server (copy-data): the data never travels through this machine. The destination is created, or withoverwritereplaced.
- CreateDirectoryAsync(string, UnixFileMode?, CancellationToken)
Creates a directory, with
permissionsif given (the server's umask applies).
- CreateHardLinkAsync(string, string, CancellationToken)
Creates the hard link
linkPathto the fileexistingPath(hardlink@openssh.com).
- CreateSymbolicLinkAsync(string, string, CancellationToken)
Creates the symbolic link
linkPathpointing totargetPath(stored as given).
- DeleteDirectoryAsync(string, CancellationToken)
Deletes an empty directory.
- DeleteFileAsync(string, CancellationToken)
Deletes a file (or a symbolic link, not its target).
- DisposeAsync()
Ends the SFTP session (the SSH connection stays up). Requests still waiting fail.
- DownloadAsync(string, Stream, SftpTransferOptions?, CancellationToken)
Downloads
remotePathintodestination, with several reads in flight (the window, by default 64 reads of up to MaxReadLength), which is what makes SFTP fast on links with latency.
- DownloadDirectoryAsync(string, string, SftpTransferOptions?, CancellationToken)
Downloads the directory
remoteDirectoryand everything under it intolocalDirectory(created if needed), file by file with DownloadFileAsync(string, string, SftpTransferOptions?, CancellationToken) and the sameoptions. Symbolic links and special files are skipped, so a link cannot lead the download outside the tree.
- DownloadFileAsync(string, string, SftpTransferOptions?, CancellationToken)
Downloads
remotePathto the local filelocalPath. With Overwrite off, an existing local file is an error; with Preserve, the local file gets the remote one's modification and access times, and its permissions where this system has Unix modes. A failed download leaves no partial file behind.
- ExistsAsync(string, CancellationToken)
Whether
pathexists (following symbolic links: a dangling link does not).
- ExpandPathAsync(string, CancellationToken)
pathwith a leading~or~userexpanded and made canonical, assftpdoes forcd ~/dir(expand-path@openssh.com, OpenSSH 8.7+). Without the extension,~and~/...start at GetHomeDirectoryAsync(CancellationToken), and a path without a tilde resolves as GetFullPathAsync(string, CancellationToken) does.
- GetAttributesAsync(string, bool, CancellationToken)
The attributes of
path; of the link itself whenfollowSymbolicLinksis false.
- GetFileSystemInfoAsync(string, CancellationToken)
The size and free space of the file system that holds
path, asdfshows them (statvfs@openssh.com).
- GetFullPathAsync(string, CancellationToken)
The canonical absolute form of
path(.is the login directory), as the server resolves it.
- GetHomeDirectoryAsync(CancellationToken)
The login directory: the server's
home-directoryextension when it has one, otherwise what.resolves to. In an OpenSSHChrootDirectorysession the extension answers the passwd home, outside the chroot; relative paths resolve from GetFullPathAsync(string, CancellationToken) of..
- GetOwnerNamesAsync(IEnumerable<uint>, IEnumerable<uint>, CancellationToken)
The names of user and group ids, as
ls -lshows them (users-groups-by-id@openssh.com, OpenSSH 9.1+).
- ListDirectoryAsync(string, CancellationToken)
Lists a directory, without
.and... Entries arrive in batches as the server sends them; names that cannot be file names in one directory (empty, or with a/) are left out.
- OpenAsync(string, FileMode, FileAccess, CancellationToken)
Opens a remote file as a seekable Stream (asynchronous reads and writes only).
modeworks as for FileStream: Create creates or truncates, CreateNew fails if the file exists, Append starts at the end. Disposing the stream closes the remote file.
- OpenReadAsync(string, CancellationToken)
Opens a remote file for reading, as OpenAsync(string, FileMode, FileAccess, CancellationToken) with Open and Read.
- OpenWriteAsync(string, CancellationToken)
Creates or truncates a remote file for writing, as OpenAsync(string, FileMode, FileAccess, CancellationToken) with Create.
- ReadSymbolicLinkAsync(string, CancellationToken)
The target of the symbolic link
path, as stored in the link.
- RenameAsync(string, string, bool, CancellationToken)
Renames or moves
oldPathtonewPath. Plain SFTP refuses whennewPathexists; withoverwriteit is replaced atomically, which needs the server'sposix-rename@openssh.com.
- SetAttributesAsync(string, SftpFileAttributes, bool, CancellationToken)
Changes the attributes that are set in
attributes; of a symbolic link itself whenfollowSymbolicLinksis false (lsetstat@openssh.com, OpenSSH 8.0+: owner, permissions and times; a server may refuse a size).
- SetAttributesAsync(string, SftpFileAttributes, CancellationToken)
Changes the attributes that are set in
attributes(size, owner, permissions, times).
- UploadAsync(Stream, string, SftpTransferOptions?, CancellationToken)
Uploads
source(read to its end) toremotePath, with several writes in flight. The remote file is created or, with Overwrite, truncated.
- UploadDirectoryAsync(string, string, SftpTransferOptions?, CancellationToken)
Uploads the local directory
localDirectoryand everything under it toremoteDirectory(created if needed), file by file with UploadFileAsync(string, string, SftpTransferOptions?, CancellationToken) and the sameoptions. Symbolic links, junctions and other reparse points are skipped, and so are special files on Unix (FIFOs, sockets, devices).
- UploadFileAsync(string, string, SftpTransferOptions?, CancellationToken)
Uploads the local file
localPathtoremotePath. With Preserve, the remote file gets the local one's modification and access times, and its permissions where this system has Unix modes.