|
14 | 14 | using Azure.Storage.Sas; |
15 | 15 | using Blobs; |
16 | 16 | using Microsoft.Identity.Client; |
| 17 | +using Microsoft.AspNetCore.StaticFiles; |
17 | 18 | using FluentStorage.Blobs; |
18 | 19 | using FluentStorage.Azure.Blobs.Gen2.Model; |
19 | 20 |
|
20 | 21 | namespace FluentStorage.Azure.Blobs { |
21 | 22 | //auth scenarios: https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/storage/Azure.Storage.Blobs/samples/Sample02_Auth.cs |
22 | 23 |
|
23 | 24 |
|
24 | | - class AzureBlobStorage : IAzureBlobStorage { |
| 25 | + class AzureBlobStorage : IAzureBlobStorage { |
25 | 26 |
|
26 | 27 | private readonly BlobServiceClient _client; |
27 | 28 | private readonly StorageSharedKeyCredential _sasSigningCredentials; |
28 | 29 | private readonly string _containerName; |
29 | 30 | private readonly ConcurrentDictionary<string, BlobContainerClient> _containerNameToContainerClient = |
30 | 31 | new ConcurrentDictionary<string, BlobContainerClient>(); |
| 32 | + private static readonly FileExtensionContentTypeProvider _mime = |
| 33 | + new FileExtensionContentTypeProvider(); |
31 | 34 |
|
32 | 35 | public AzureBlobStorage( |
33 | 36 | BlobServiceClient blobServiceClient, |
@@ -129,9 +132,19 @@ public async Task WriteAsync(string fullPath, Stream dataStream, |
129 | 132 |
|
130 | 133 | BlockBlobClient client = container.GetBlockBlobClient(path); |
131 | 134 |
|
| 135 | + string contentType; |
| 136 | + if (!_mime.TryGetContentType(path, out contentType)) { |
| 137 | + contentType = "application/octet-stream"; |
| 138 | + } |
132 | 139 | try { |
| 140 | + var options = new BlobUploadOptions { |
| 141 | + HttpHeaders = new BlobHttpHeaders { |
| 142 | + ContentType = contentType |
| 143 | + } |
| 144 | + }; |
133 | 145 | await client.UploadAsync( |
134 | 146 | new StorageSourceStream(dataStream), |
| 147 | + options: options, |
135 | 148 | cancellationToken: cancellationToken).ConfigureAwait(false); |
136 | 149 | } |
137 | 150 | catch (RequestFailedException ex) when (ex.ErrorCode == "OperationNotAllowedInCurrentState") { |
|
0 commit comments