Skip to content

Commit 555a1da

Browse files
committed
ASMO-5264 Fix as review comments
1 parent 5f797fc commit 555a1da

File tree

7 files changed

+60
-22
lines changed

7 files changed

+60
-22
lines changed

src/Samhammer.AzureBlobStorage.Test/IntegrationTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ public IntegrationTest()
3838
clientFactory.GetDefaultContainerName().Returns(DefaultContainerName);
3939
clientFactory.GetClient().Returns(new BlobServiceClient(ConnectionString));
4040
var options = Substitute.For<IOptions<AzureBlobStorageOptions>>();
41+
var streamManagerService = Substitute.For<IStreamManagerService>();
4142

42-
_service = new AzureBlobStorageService<IAzureBlobStorageClientFactory>(clientFactory, options);
43+
_service = new AzureBlobStorageService<IAzureBlobStorageClientFactory>(clientFactory, streamManagerService, options);
4344

4445
_comparisonOptions = o => o
4546
.Using<DateTimeOffset>(ctx =>
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
namespace Samhammer.AzureBlobStorage.Options
1+
using System;
2+
3+
namespace Samhammer.AzureBlobStorage.Options
24
{
35
public class AzureBlobStorageOptions
46
{
57
public string ConnectionString { get; set; }
68

79
public string ContainerName { get; set; }
810

9-
public int FileUrlExpiresByDays { get; set; } = 1;
11+
public TimeSpan? FileUrlExpires { get; set; }
1012
}
1113
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Samhammer.AzureBlobStorage.Options
2+
{
3+
public class StreamManagerOptions
4+
{
5+
public long? MaxSmallPoolFreeBytes { get; set; }
6+
7+
public long? MaxLargePoolFreeBytes { get; set; }
8+
}
9+
}

src/Samhammer.AzureBlobStorage/ServiceCollectionExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public static class ServiceCollectionExtensions
1212
public static IServiceCollection AddDefaultAzureBlobStorage(this IServiceCollection services, IConfiguration configuration)
1313
{
1414
services.Configure<AzureBlobStorageOptions>(configuration.GetSection(nameof(AzureBlobStorageOptions)));
15+
services.Configure<StreamManagerOptions>(configuration.GetSection(nameof(StreamManagerOptions)));
1516

1617
RegisterAzureBlobStorage<IDefaultAzureBlobStorageClientFactory, DefaultAzureBlobStorageClientFactory>(services, true);
1718

@@ -42,6 +43,7 @@ private static void RegisterAzureBlobStorage<TFactoryInterface, TFactoryImpl>(IS
4243
{
4344
services.AddSingleton<TFactoryInterface, TFactoryImpl>();
4445
services.AddSingleton<IAzureBlobStorageService<TFactoryInterface>, AzureBlobStorageService<TFactoryInterface>>();
46+
services.AddSingleton<IStreamManagerService, StreamManagerService>();
4547

4648
if (registerAsDefault)
4749
{

src/Samhammer.AzureBlobStorage/Services/AzureBlobStorageService.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ public class AzureBlobStorageService<T> : IAzureBlobStorageService<T> where T :
2222

2323
private readonly AzureBlobStorageOptions _blobStorageOptions;
2424

25-
public AzureBlobStorageService(T blobStorageClientFactory, IOptions<AzureBlobStorageOptions> blobStorageOptions)
25+
private readonly IStreamManagerService _streamManagerService;
26+
27+
public AzureBlobStorageService(T blobStorageClientFactory, IStreamManagerService streamManagerService, IOptions<AzureBlobStorageOptions> blobStorageOptions)
2628
{
2729
_defaultContainerName = blobStorageClientFactory.GetDefaultContainerName();
2830
_client = blobStorageClientFactory.GetClient();
2931
_blobStorageOptions = blobStorageOptions.Value;
32+
_streamManagerService = streamManagerService;
3033
}
3134

3235
public string GetStorageAccountName()
@@ -76,7 +79,7 @@ public async Task<BlobContract> GetBlobContentsAsync(string blobName, string con
7679
var blobClient = await GetBlobClient(containerClient, blobName);
7780

7881
var properties = (await blobClient.GetPropertiesAsync()).Value;
79-
var stream = StreamHelper.StreamManager.GetStream();
82+
var stream = _streamManagerService.GetStream();
8083
await blobClient.DownloadToAsync(stream);
8184
stream.Position = 0;
8285

@@ -117,7 +120,10 @@ private Uri CreateServiceSASBlob(BlobClient blobClient)
117120
Resource = "b",
118121
};
119122

120-
sasBuilder.ExpiresOn = DateTimeOffset.UtcNow.AddDays(_blobStorageOptions.FileUrlExpiresByDays);
123+
sasBuilder.ExpiresOn = _blobStorageOptions.FileUrlExpires.HasValue
124+
? DateTimeOffset.UtcNow.Add(_blobStorageOptions.FileUrlExpires.Value)
125+
: DateTimeOffset.UtcNow.AddDays(1);
126+
121127
sasBuilder.SetPermissions(BlobContainerSasPermissions.Read);
122128

123129
Uri sasURI = blobClient.GenerateSasUri(sasBuilder);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.IO;
2+
using Microsoft.Extensions.Options;
3+
using Microsoft.IO;
4+
using Samhammer.AzureBlobStorage.Options;
5+
6+
namespace Samhammer.AzureBlobStorage.Services
7+
{
8+
public class StreamManagerService : IStreamManagerService
9+
{
10+
private const long MaxSmallPoolFreeBytes = 1000000;
11+
12+
private const long MaxLargePoolFreeBytes = 10000000;
13+
14+
private readonly RecyclableMemoryStreamManager _streamManager;
15+
16+
public StreamManagerService(IOptions<StreamManagerOptions> streamManagerOptions)
17+
{
18+
StreamManagerOptions options = streamManagerOptions.Value;
19+
_streamManager = new RecyclableMemoryStreamManager(
20+
options.MaxSmallPoolFreeBytes.HasValue ? options.MaxSmallPoolFreeBytes.Value : MaxSmallPoolFreeBytes,
21+
options.MaxLargePoolFreeBytes.HasValue ? options.MaxLargePoolFreeBytes.Value : MaxLargePoolFreeBytes);
22+
}
23+
24+
public MemoryStream GetStream()
25+
{
26+
return _streamManager.GetStream();
27+
}
28+
}
29+
30+
public interface IStreamManagerService
31+
{
32+
public MemoryStream GetStream();
33+
}
34+
}

src/Samhammer.AzureBlobStorage/StreamHelper.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)