FtpServer icon indicating copy to clipboard operation
FtpServer copied to clipboard

InMemoryFileSystem documentation

Open piersdeseilligny opened this issue 1 year ago • 2 comments

I'm just creating this issue because as far as I can tell, there's no documentation or examples anywhere of how to use the InMemoryFileSystem, and it's not extremely clear how to do so - would it be possible to get some sample code of a very simple setup, just to get an idea of what's required to provide a "virtual" file system to a client, and to receive incoming requests and files?

Thanks for such a great extension! Piers

piersdeseilligny avatar Jan 31 '24 18:01 piersdeseilligny

I second this request

dingetje avatar Jun 28 '24 15:06 dingetje

There didn't seem to be much to it when I gave it a go. My Main looked like the one from the DotNetFileSystem example, except with swapped names for InMemory. I chose to allow an anonymous access:

static async Task Main(string[] args)
{
    // Setup dependency injection
    var services = new ServiceCollection();

    // Add FTP server services
    // AnonymousMembershipProvider = allow only anonymous logins
    services.AddFtpServer(builder => builder
        .UseInMemoryFileSystem() 
        .EnableAnonymousAuthentication()); // allow anonymous logins

    // Configure the FTP server
    services.Configure<FtpServerOptions>(opt => opt.ServerAddress = "127.0.0.1");

    // Build the service provider
    using (var serviceProvider = services.BuildServiceProvider())
    {
        // Initialize the FTP server
        var ftpServerHost = serviceProvider.GetRequiredService<IFtpServerHost>();

        // Start the FTP server
        await ftpServerHost.StartAsync();

        Console.WriteLine("Press ENTER/RETURN to close the test application.");
        Console.ReadLine();

        // Stop the FTP server
        await ftpServerHost.StopAsync();
    }
}

That was pretty much it, I ran it, connected with filezilla and uploaded stuff, i could see it, when I restarted the server it was gone (as you might expect!)

I took a quick look into the code (download it from this repo and have a read of the comments in InMemory project) to work out e.g. that

services.Configure<InMemoryFileSystemOptions>(opt => opt.KeepAnonymousFileSystem = false);

Means the server will throw away uploaded files when the anonymous user disconnects/reconnects, whereas setting it to true means the files will be kept across client disconnect/reconnect cycles (but not server restarts)

ChairmanMawh avatar Jul 12 '24 05:07 ChairmanMawh