Class SftpFileProvider
- Namespace
- SshSharp.FileProviders
- Assembly
- SshSharp.FileProviders.dll
An IFileProvider over SFTP: the files under a directory on an SSH server, for ASP.NET Core static files, Razor views,
configuration and anything else that reads through Microsoft.Extensions.FileProviders. Read-only; nothing is mounted, so only
this application sees the files.
public sealed class SftpFileProvider : IFileProvider, IDisposable, IAsyncDisposable
- Inheritance
-
SftpFileProvider
- Implements
- Inherited Members
Remarks
IFileProvider is synchronous, so each lookup waits for an SFTP round trip; attributes and listings are cached for CacheDuration to keep that cheap. Streams from CreateReadStream() read ahead, and are fastest read asynchronously (as ASP.NET Core's static files middleware does).
Paths cannot leave Root: any .. segment gives a file that does not exist. Symbolic links are followed (on the
server, which decides where they lead). Watch(string) never signals: SFTP has no change notifications.
Built from a session factory (SftpFileProvider(Func<CancellationToken, Task<SftpClient>>, string, SftpFileProviderOptions?)), the provider follows a connection that drops and comes back: it keeps one session while it works, and opens a new one after the session or its connection fails, trying the failed lookup once more on it. Built from one SftpClient, it uses that session for its lifetime, and fails once it fails.
Constructors
- SftpFileProvider(SftpClient, string, SftpFileProviderOptions?)
Serves the files under
rooton the server.
- SftpFileProvider(Func<CancellationToken, Task<SftpClient>>, string, SftpFileProviderOptions?)
Serves the files under
rooton the server, through sessions fromsessionFactory: one is opened on the first lookup and shared by every lookup after it, and a new one is opened when the session or its connection fails. The provider owns the sessions it opens and disposes them (dispose the provider when it is no longer used).
Properties
- Root
The directory on the server that the provider serves.
Methods
- ClearCache()
Forgets the cached attributes and listings, so the next lookups ask the server.
- Dispose()
Disposes the provider as DisposeAsync() does, waiting for it (on the thread pool, as lookups do).
- DisposeAsync()
Disposes the SFTP sessions the provider opened from its session factory; later lookups throw ObjectDisposedException. A provider built from one SftpClient leaves that session alone: it stays the caller's, and this does nothing.
- GetDirectoryContents(string)
The entries of the directory at
subpath, or contents with Exists false.
- GetFileInfo(string)
The file at
subpath, or one with Exists false.
- Watch(string)
A token that never changes: SFTP cannot tell when a file changes.