|
16 | 16 | |[](https://www.nuget.org/packages/ManagedCode.Storage.Gcp) | [ManagedCode.Storage.Gcp](https://www.nuget.org/packages/ManagedCode.Storage.Gcp) | GCP |
|
17 | 17 | |[](https://www.nuget.org/packages/ManagedCode.Storage.AspNetExtensions) | [ManagedCode.Storage.AspNetExtensions](https://www.nuget.org/packages/ManagedCode.Storage.AspNetExtensions) | AspNetExtensions |
|
18 | 18 |
|
| 19 | +# Storage |
| 20 | +--- |
| 21 | +## Storage pattern implementation for C#. |
| 22 | +A universal storage for working with multiple storage providers: |
| 23 | +- Azure |
| 24 | +- Google Cloud |
| 25 | +- Amazon |
| 26 | +- FileSystem |
| 27 | +## General concept |
| 28 | +The library incapsulates all provider specific to make connection and managing storages as easy as possible. You have as customer just connect the library in your Startup providing necessary connection strings and inject needed interfaces in your services. |
| 29 | + |
| 30 | +## Connection modes |
| 31 | +You can connect storage interface in two modes provider-specific and default. In case of default you are restricted with one storage type |
| 32 | + |
| 33 | +### Azure |
| 34 | + |
| 35 | +Default mode connection: |
| 36 | + |
| 37 | +```cs |
| 38 | +// Startup.cs |
| 39 | +services.AddAzureStorageAsDefault(new AzureStorageOptions |
| 40 | +{ |
| 41 | + Container = "{YOUR_CONTAINER_NAME}", |
| 42 | + ConnectionString = "{YOUR_CONNECTION_NAME}", |
| 43 | +}); |
| 44 | +``` |
| 45 | + |
| 46 | +Using in default mode: |
| 47 | + |
| 48 | +```cs |
| 49 | +// MyService.cs |
| 50 | +public class MyService |
| 51 | +{ |
| 52 | + private readonly IStorage _storage; |
| 53 | + |
| 54 | + public MyService(IStorage storage) |
| 55 | + { |
| 56 | + _storage = storage; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +``` |
| 61 | + |
| 62 | +Provider-specific mode connection: |
| 63 | + |
| 64 | +```cs |
| 65 | +// Startup.cs |
| 66 | +services.AddAzureStorage(new AzureStorageOptions |
| 67 | +{ |
| 68 | + Container = "{YOUR_CONTAINER_NAME}", |
| 69 | + ConnectionString = "{YOUR_CONNECTION_NAME}", |
| 70 | +}); |
| 71 | +``` |
| 72 | + |
| 73 | +Using in provider-specific mode |
| 74 | + |
| 75 | +```cs |
| 76 | +// MyService.cs |
| 77 | +public class MyService |
| 78 | +{ |
| 79 | + private readonly IAzureStorage _azureStorage; |
| 80 | + |
| 81 | + public MyService(IAzureStorage azureStorage) |
| 82 | + { |
| 83 | + _azureStorage = azureStorage; |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | +<details> |
| 88 | + <summary>Google Cloud (Click here to expand)</summary> |
| 89 | + |
| 90 | +### Google Cloud |
| 91 | +Default mode connection: |
| 92 | +```cs |
| 93 | +// Startup.cs |
| 94 | +services.AddGCPStorageAsDefault(opt => |
| 95 | +{ |
| 96 | + opt.GoogleCredential = GoogleCredential.FromFile("{PATH_TO_YOUR_CREDENTIALS_FILE}.json"); |
| 97 | + |
| 98 | + opt.BucketOptions = new BucketOptions() |
| 99 | + { |
| 100 | + ProjectId = "{YOUR_API_PROJECT_ID}", |
| 101 | + Bucket = "{YOUR_BUCKET_NAME}", |
| 102 | + }; |
| 103 | +}); |
| 104 | +``` |
| 105 | +Using in default mode: |
| 106 | +```cs |
| 107 | +// MyService.cs |
| 108 | +public class MyService |
| 109 | +{ |
| 110 | + private readonly IStorage _storage; |
| 111 | + |
| 112 | + public MyService(IStorage storage) |
| 113 | + { |
| 114 | + _storage = storage; |
| 115 | + } |
| 116 | +} |
| 117 | +``` |
| 118 | +Provider-specific mode connection: |
| 119 | +```cs |
| 120 | +// Startup.cs |
| 121 | +services.AddGCPStorage(new GCPStorageOptions |
| 122 | +{ |
| 123 | + BucketOptions = new BucketOptions() |
| 124 | + { |
| 125 | + ProjectId = "{YOUR_API_PROJECT_ID}", |
| 126 | + Bucket = "{YOUR_BUCKET_NAME}", |
| 127 | + } |
| 128 | +}); |
| 129 | +``` |
| 130 | +Using in provider-specific mode |
| 131 | +```cs |
| 132 | +// MyService.cs |
| 133 | +public class MyService |
| 134 | +{ |
| 135 | + private readonly IGCPStorage _gcpStorage; |
| 136 | + public MyService(IGCPStorage gcpStorage) |
| 137 | + { |
| 138 | + _gcpStorage = gcpStorage; |
| 139 | + } |
| 140 | +} |
| 141 | +``` |
| 142 | +</details> |
| 143 | + |
| 144 | +<details> |
| 145 | + <summary>Amazon (Click here to expand)</summary> |
| 146 | + |
| 147 | +### Amazon |
| 148 | +Default mode connection: |
| 149 | +```cs |
| 150 | +// Startup.cs |
| 151 | +//aws libarary overwrites property values. you should only create configurations this way. |
| 152 | +var awsConfig = new AmazonS3Config(); |
| 153 | +awsConfig.RegionEndpoint = RegionEndpoint.EUWest1; |
| 154 | +awsConfig.ForcePathStyle = true; |
| 155 | +awsConfig.UseHttp = true; |
| 156 | +awsConfig.ServiceURL = "http://localhost:4566"; //this is the default port for the aws s3 emulator, must be last in the list |
| 157 | +
|
| 158 | +services.AddAWSStorageAsDefault(opt => |
| 159 | +{ |
| 160 | + opt.PublicKey = "{YOUR_PUBLIC_KEY}"; |
| 161 | + opt.SecretKey = "{YOUR_SECRET_KEY}"; |
| 162 | + opt.Bucket = "{YOUR_BUCKET_NAME}"; |
| 163 | + opt.OriginalOptions = awsConfig; |
| 164 | +}); |
| 165 | +``` |
| 166 | +Using in default mode: |
| 167 | +```cs |
| 168 | +// MyService.cs |
| 169 | +public class MyService |
| 170 | +{ |
| 171 | + private readonly IStorage _storage; |
| 172 | + |
| 173 | + public MyService(IStorage storage) |
| 174 | + { |
| 175 | + _storage = storage; |
| 176 | + } |
| 177 | +} |
| 178 | +``` |
| 179 | +Provider-specific mode connection: |
| 180 | +```cs |
| 181 | +// Startup.cs |
| 182 | +services.AddAWSStorage(new AWSStorageOptions |
| 183 | +{ |
| 184 | + PublicKey = "{YOUR_PUBLIC_KEY}", |
| 185 | + SecretKey = "{YOUR_SECRET_KEY}", |
| 186 | + Bucket = "{YOUR_BUCKET_NAME}", |
| 187 | + OriginalOptions = awsConfig |
| 188 | +}); |
| 189 | +``` |
| 190 | +Using in provider-specific mode |
| 191 | +```cs |
| 192 | +// MyService.cs |
| 193 | +public class MyService |
| 194 | +{ |
| 195 | + private readonly IAWSStorage _gcpStorage; |
| 196 | + public MyService(IAWSStorage gcpStorage) |
| 197 | + { |
| 198 | + _gcpStorage = gcpStorage; |
| 199 | + } |
| 200 | +} |
| 201 | +``` |
| 202 | +</details> |
| 203 | + |
| 204 | +<details> |
| 205 | + <summary>FileSystem (Click here to expand)</summary> |
| 206 | + |
| 207 | +### FileSystem |
| 208 | +Default mode connection: |
| 209 | +```cs |
| 210 | +// Startup.cs |
| 211 | +services.AddFileSystemStorageAsDefault(opt => |
| 212 | +{ |
| 213 | + opt.CommonPath = Path.Combine(Environment.CurrentDirectory, "{YOUR_BUCKET_NAME}"); |
| 214 | + opt.Path = "{YOUR_BUCKET_NAME}"; |
| 215 | +}); |
| 216 | +``` |
| 217 | +Using in default mode: |
| 218 | +```cs |
| 219 | +// MyService.cs |
| 220 | +public class MyService |
| 221 | +{ |
| 222 | + private readonly IStorage _storage; |
| 223 | + |
| 224 | + public MyService(IStorage storage) |
| 225 | + { |
| 226 | + _storage = storage; |
| 227 | + } |
| 228 | +} |
| 229 | +``` |
| 230 | +Provider-specific mode connection: |
| 231 | +```cs |
| 232 | +// Startup.cs |
| 233 | +services.AddFileSystemStorage(new FileSystemStorageOptions |
| 234 | +{ |
| 235 | + CommonPath = Path.Combine(Environment.CurrentDirectory, "{YOUR_BUCKET_NAME}"), |
| 236 | + Path = "{YOUR_BUCKET_NAME}" |
| 237 | +}); |
| 238 | +``` |
| 239 | +Using in provider-specific mode |
| 240 | +```cs |
| 241 | +// MyService.cs |
| 242 | +public class MyService |
| 243 | +{ |
| 244 | + private readonly IFileSystemStorage _fileSystemStorage; |
| 245 | + public MyService(IFileSystemStorage fileSystemStorage) |
| 246 | + { |
| 247 | + _fileSystemStorage = fileSystemStorage; |
| 248 | + } |
| 249 | +} |
| 250 | +``` |
| 251 | +</details> |
| 252 | + |
| 253 | +## How to use |
| 254 | +We assume that below code snippets are placed in your service class with injected IStorage: |
| 255 | + |
| 256 | +```cs |
| 257 | +public class MyService |
| 258 | +{ |
| 259 | + private readonly IStorage _storage; |
| 260 | + public MyService(IStorage storage) |
| 261 | + { |
| 262 | + _storage = storage; |
| 263 | + } |
| 264 | +} |
| 265 | +``` |
| 266 | + |
| 267 | +### Exists |
| 268 | +```cs |
| 269 | +await _storage.ExistsAsync("{YOUR_FILE_NAME}"); |
| 270 | +``` |
| 271 | + |
| 272 | +### Delete |
| 273 | +```cs |
| 274 | +await _storage.DeleteAsync("{YOUR_FILE_NAME}"); |
| 275 | +``` |
| 276 | + |
| 277 | +### Download |
| 278 | +```cs |
| 279 | +var stream = await _storage.DownloadAsStreamAsync(new BlobMetadata { Name = "{YOUR_FILE_NAME}"}); |
| 280 | +using var sr = new StreamReader(stream, Encoding.UTF8); |
| 281 | + |
| 282 | +string content = await sr.ReadToEndAsync(); |
| 283 | +``` |
| 284 | + |
| 285 | +### Get |
| 286 | +```cs |
| 287 | +await _storage.GetBlobAsync("{YOUR_FILE_NAME}"); |
| 288 | +``` |
| 289 | + |
| 290 | +### Upload |
| 291 | +```cs |
| 292 | +var byteArray = Encoding.ASCII.GetBytes("{YOUR_CONTENT}"); |
| 293 | +var stream = new MemoryStream(byteArray); |
| 294 | + |
| 295 | +await _storage.UploadStreamAsync("{YOUR_FILE_NAME}", stream); |
| 296 | +``` |
| 297 | + |
| 298 | + |
0 commit comments