|
1 | 1 | # DistributedFileStorage.MongoDB [](http://badge.fury.io/nu/DistributedFileStorage.MongoDB) |
2 | 2 | .NET DistributedFileStorage with MongoDB implementation of IDfsDatabase |
3 | 3 |
|
| 4 | + |
| 5 | +## Features |
| 6 | +* Storing metadata in the database (SQL/NoSQL) |
| 7 | +* Storing files in the file system |
| 8 | +* Deduplication of files by content |
| 9 | +* Distributed storage (multiple disks) |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +## Example: Simple console app |
| 14 | +``` |
| 15 | +dotnet new console --name "DfsExample" |
| 16 | +cd DfsExample |
| 17 | +dotnet add package DistributedFileStorage.MongoDB |
| 18 | +dotnet add package Microsoft.Extensions.DependencyInjection |
| 19 | +``` |
| 20 | + |
| 21 | +program.cs: |
| 22 | +```C# |
| 23 | +using DistributedFileStorage; |
| 24 | +using Microsoft.Extensions.DependencyInjection; |
| 25 | + |
| 26 | + |
| 27 | +// add services to the container |
| 28 | +var services = new ServiceCollection() |
| 29 | + .AddDfsMongo<string>((sp, options) => |
| 30 | + { |
| 31 | + // add database settings |
| 32 | + options.Database.ConnectionString = "mongodb://localhost:27017"; |
| 33 | + |
| 34 | + // add path construction algorithm |
| 35 | + var rnd = new Random(); |
| 36 | + options.FileStorage.PathBuilder = (fileId) => Path.GetFullPath($@"_dfs\fake_disk_{rnd.Next(1, 3)}\{DateTime.Now:yyyy\\MM\\dd}\{fileId}"); |
| 37 | + }) |
| 38 | + .BuildServiceProvider(); |
| 39 | + |
| 40 | + |
| 41 | +var dfs = services.GetRequiredService<IDistributedFileStorage<string>>(); |
| 42 | + |
| 43 | +// upload |
| 44 | +using var uploadFile = new MemoryStream(System.Text.Encoding.UTF8.GetBytes("test text")); |
| 45 | +var fileId = await dfs.Add(uploadFile, $"example.txt", "my metadata"); |
| 46 | + |
| 47 | +// get info |
| 48 | +var fileInfo = await dfs.GetInfo(fileId); |
| 49 | +Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(fileInfo)); |
| 50 | + |
| 51 | +// download |
| 52 | +using var downloadFile = File.Create("example.txt"); |
| 53 | +await foreach (var chunk in dfs.GetContent(fileId)) |
| 54 | + await downloadFile.WriteAsync(chunk); |
| 55 | +``` |
| 56 | + |
| 57 | + |
| 58 | +[More examples...](https://github.com/mustaddon/DistributedFileStorage/tree/main/Examples/) |
0 commit comments