From e40d08207d4db9b239eb0837b3907835bd30a34a Mon Sep 17 00:00:00 2001 From: Patrick Garcia Date: Mon, 11 Sep 2023 16:55:54 +0200 Subject: [PATCH 1/8] feat: implement new method for getting write stream --- .../Foundatio.AzureStorage.csproj | 2 +- .../Storage/AzureFileStorage.cs | 24 ++++++++++++++----- .../Foundatio.AzureStorage.Tests.csproj | 3 +++ .../Storage/AzureStorageTests.cs | 5 ++++ 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/Foundatio.AzureStorage/Foundatio.AzureStorage.csproj b/src/Foundatio.AzureStorage/Foundatio.AzureStorage.csproj index 5770a4c..396f906 100644 --- a/src/Foundatio.AzureStorage/Foundatio.AzureStorage.csproj +++ b/src/Foundatio.AzureStorage/Foundatio.AzureStorage.csproj @@ -8,7 +8,7 @@ - + diff --git a/src/Foundatio.AzureStorage/Storage/AzureFileStorage.cs b/src/Foundatio.AzureStorage/Storage/AzureFileStorage.cs index e1b6257..2a7fe7c 100644 --- a/src/Foundatio.AzureStorage/Storage/AzureFileStorage.cs +++ b/src/Foundatio.AzureStorage/Storage/AzureFileStorage.cs @@ -43,17 +43,29 @@ public AzureFileStorage(Builder _serializer; - public async Task GetFileStreamAsync(string path, CancellationToken cancellationToken = default) { - if (String.IsNullOrEmpty(path)) - throw new ArgumentNullException(nameof(path)); + public Task GetFileStreamAsync(string path, CancellationToken cancellationToken = default) + => GetFileStreamAsync(path, FileAccess.Read, cancellationToken); + public async Task GetFileStreamAsync(string path, FileAccess fileAccess, CancellationToken cancellationToken = default) { + if (String.IsNullOrEmpty(path)) + throw new ArgumentNullException(nameof(path)); + string normalizedPath = NormalizePath(path); _logger.LogTrace("Getting file stream for {Path}", normalizedPath); - + var blockBlob = _container.GetBlockBlobReference(normalizedPath); - + try { - return await blockBlob.OpenReadAsync(null, null, null, cancellationToken).AnyContext(); + switch (fileAccess) { + case FileAccess.Read: + return await blockBlob.OpenReadAsync(null, null, null, cancellationToken).AnyContext(); + case FileAccess.Write: + return await blockBlob.OpenWriteAsync(null, null, null, cancellationToken).AnyContext(); + case FileAccess.ReadWrite: + throw new NotSupportedException($"{nameof(AzureFileStorage)} only supports either read or write access, but not both at once."); + default: + throw new ArgumentOutOfRangeException(nameof(fileAccess), fileAccess, $"Unknown {nameof(FileAccess)} received"); + } } catch (StorageException ex) when (ex is { RequestInformation.HttpStatusCode: 404}) { _logger.LogDebug(ex, "Unable to get file stream for {Path}: File Not Found", normalizedPath); return null; diff --git a/tests/Foundatio.AzureStorage.Tests/Foundatio.AzureStorage.Tests.csproj b/tests/Foundatio.AzureStorage.Tests/Foundatio.AzureStorage.Tests.csproj index bd66f1b..37d6f9c 100644 --- a/tests/Foundatio.AzureStorage.Tests/Foundatio.AzureStorage.Tests.csproj +++ b/tests/Foundatio.AzureStorage.Tests/Foundatio.AzureStorage.Tests.csproj @@ -7,4 +7,7 @@ Always + + + \ No newline at end of file diff --git a/tests/Foundatio.AzureStorage.Tests/Storage/AzureStorageTests.cs b/tests/Foundatio.AzureStorage.Tests/Storage/AzureStorageTests.cs index 5fc8624..cfb7882 100644 --- a/tests/Foundatio.AzureStorage.Tests/Storage/AzureStorageTests.cs +++ b/tests/Foundatio.AzureStorage.Tests/Storage/AzureStorageTests.cs @@ -109,5 +109,10 @@ public override Task CanRoundTripSeekableStreamAsync() { public override Task WillRespectStreamOffsetAsync() { return base.WillRespectStreamOffsetAsync(); } + + [Fact] + public override Task WillWriteStreamContentAsync() { + return base.WillWriteStreamContentAsync(); + } } } \ No newline at end of file From 224d3d267262b4af5435fa605562a99cce9c15aa Mon Sep 17 00:00:00 2001 From: Patrick Garcia Date: Sun, 24 Sep 2023 09:53:22 +0200 Subject: [PATCH 2/8] chore: adapt to new stream mode --- .../Storage/AzureFileStorage.cs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/Foundatio.AzureStorage/Storage/AzureFileStorage.cs b/src/Foundatio.AzureStorage/Storage/AzureFileStorage.cs index 2a7fe7c..32203ad 100644 --- a/src/Foundatio.AzureStorage/Storage/AzureFileStorage.cs +++ b/src/Foundatio.AzureStorage/Storage/AzureFileStorage.cs @@ -44,9 +44,9 @@ public AzureFileStorage(Builder _serializer; public Task GetFileStreamAsync(string path, CancellationToken cancellationToken = default) - => GetFileStreamAsync(path, FileAccess.Read, cancellationToken); + => GetFileStreamAsync(path, StreamMode.Read, cancellationToken); - public async Task GetFileStreamAsync(string path, FileAccess fileAccess, CancellationToken cancellationToken = default) { + public async Task GetFileStreamAsync(string path, StreamMode streamMode, CancellationToken cancellationToken = default) { if (String.IsNullOrEmpty(path)) throw new ArgumentNullException(nameof(path)); @@ -56,16 +56,11 @@ public async Task GetFileStreamAsync(string path, FileAccess fileAccess, var blockBlob = _container.GetBlockBlobReference(normalizedPath); try { - switch (fileAccess) { - case FileAccess.Read: - return await blockBlob.OpenReadAsync(null, null, null, cancellationToken).AnyContext(); - case FileAccess.Write: - return await blockBlob.OpenWriteAsync(null, null, null, cancellationToken).AnyContext(); - case FileAccess.ReadWrite: - throw new NotSupportedException($"{nameof(AzureFileStorage)} only supports either read or write access, but not both at once."); - default: - throw new ArgumentOutOfRangeException(nameof(fileAccess), fileAccess, $"Unknown {nameof(FileAccess)} received"); - } + return streamMode switch { + StreamMode.Read => await blockBlob.OpenReadAsync(null, null, null, cancellationToken).AnyContext(), + StreamMode.Write => await blockBlob.OpenWriteAsync(null, null, null, cancellationToken).AnyContext(), + _ => null + }; } catch (StorageException ex) when (ex is { RequestInformation.HttpStatusCode: 404}) { _logger.LogDebug(ex, "Unable to get file stream for {Path}: File Not Found", normalizedPath); return null; From 89e43bb538b42785c6f53796e2d26f7a68452f05 Mon Sep 17 00:00:00 2001 From: Patrick Garcia Date: Sun, 24 Sep 2023 09:57:56 +0200 Subject: [PATCH 3/8] fix: updated package reference --- src/Foundatio.AzureStorage/Foundatio.AzureStorage.csproj | 2 +- .../Foundatio.AzureStorage.Tests.csproj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Foundatio.AzureStorage/Foundatio.AzureStorage.csproj b/src/Foundatio.AzureStorage/Foundatio.AzureStorage.csproj index 396f906..82f3872 100644 --- a/src/Foundatio.AzureStorage/Foundatio.AzureStorage.csproj +++ b/src/Foundatio.AzureStorage/Foundatio.AzureStorage.csproj @@ -8,7 +8,7 @@ - + diff --git a/tests/Foundatio.AzureStorage.Tests/Foundatio.AzureStorage.Tests.csproj b/tests/Foundatio.AzureStorage.Tests/Foundatio.AzureStorage.Tests.csproj index 37d6f9c..e214136 100644 --- a/tests/Foundatio.AzureStorage.Tests/Foundatio.AzureStorage.Tests.csproj +++ b/tests/Foundatio.AzureStorage.Tests/Foundatio.AzureStorage.Tests.csproj @@ -8,6 +8,6 @@ - + \ No newline at end of file From f916a71a7b78f78179965d48c4e740385091637e Mon Sep 17 00:00:00 2001 From: Patrick Garcia Date: Mon, 25 Sep 2023 08:24:07 +0200 Subject: [PATCH 4/8] fix: update project reference in directory.props instead --- tests/Directory.Build.props | 2 +- .../Foundatio.AzureStorage.Tests.csproj | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/Directory.Build.props b/tests/Directory.Build.props index f129df9..59fab44 100644 --- a/tests/Directory.Build.props +++ b/tests/Directory.Build.props @@ -11,7 +11,7 @@ - + \ No newline at end of file diff --git a/tests/Foundatio.AzureStorage.Tests/Foundatio.AzureStorage.Tests.csproj b/tests/Foundatio.AzureStorage.Tests/Foundatio.AzureStorage.Tests.csproj index e214136..bd66f1b 100644 --- a/tests/Foundatio.AzureStorage.Tests/Foundatio.AzureStorage.Tests.csproj +++ b/tests/Foundatio.AzureStorage.Tests/Foundatio.AzureStorage.Tests.csproj @@ -7,7 +7,4 @@ Always - - - \ No newline at end of file From b61d55e7713ff3409810e9825211fa5dc2a88c18 Mon Sep 17 00:00:00 2001 From: Patrick Garcia Date: Tue, 1 Oct 2024 11:03:20 +0200 Subject: [PATCH 5/8] fix typos and add missing project reference for testing --- src/Foundatio.AzureStorage/Storage/AzureFileStorage.cs | 2 +- .../Foundatio.AzureStorage.Tests.csproj | 3 +++ .../Storage/AzureStorageTests.cs | 7 +------ 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Foundatio.AzureStorage/Storage/AzureFileStorage.cs b/src/Foundatio.AzureStorage/Storage/AzureFileStorage.cs index 669add7..c4e0d05 100644 --- a/src/Foundatio.AzureStorage/Storage/AzureFileStorage.cs +++ b/src/Foundatio.AzureStorage/Storage/AzureFileStorage.cs @@ -48,7 +48,7 @@ public AzureFileStorage(Builder _serializer; public CloudBlobContainer Container => _container; - [Obsolete($"Use {nameof(GetFileStreamAsync)} with {nameof(FileAccess)} instead to define read or write behavior of stream")] + [Obsolete($"Use {nameof(GetFileStreamAsync)} with {nameof(StreamMode)} instead to define read or write behavior of stream")] public Task GetFileStreamAsync(string path, CancellationToken cancellationToken = default) => GetFileStreamAsync(path, StreamMode.Read, cancellationToken); diff --git a/tests/Foundatio.AzureStorage.Tests/Foundatio.AzureStorage.Tests.csproj b/tests/Foundatio.AzureStorage.Tests/Foundatio.AzureStorage.Tests.csproj index e854e62..8281eb4 100644 --- a/tests/Foundatio.AzureStorage.Tests/Foundatio.AzureStorage.Tests.csproj +++ b/tests/Foundatio.AzureStorage.Tests/Foundatio.AzureStorage.Tests.csproj @@ -12,4 +12,7 @@ docker-compose.yml + + + \ No newline at end of file diff --git a/tests/Foundatio.AzureStorage.Tests/Storage/AzureStorageTests.cs b/tests/Foundatio.AzureStorage.Tests/Storage/AzureStorageTests.cs index 0474926..f08cc8e 100644 --- a/tests/Foundatio.AzureStorage.Tests/Storage/AzureStorageTests.cs +++ b/tests/Foundatio.AzureStorage.Tests/Storage/AzureStorageTests.cs @@ -137,7 +137,7 @@ public override Task WillRespectStreamOffsetAsync() return base.WillRespectStreamOffsetAsync(); } - [Fact(Skip = "Write Stream is not yet supported")] + [Fact] public override Task WillWriteStreamContentAsync() { return base.WillWriteStreamContentAsync(); @@ -180,10 +180,5 @@ public virtual async Task WillNotReturnDirectoryInGetPagedFileListAsync() Assert.False(result.HasMore); Assert.Empty(result.Files); } - - [Fact] - public override Task WillWriteStreamContentAsync() { - return base.WillWriteStreamContentAsync(); - } } } From d35cd5ecf54f19759ed2bec2834bc60628e4de63 Mon Sep 17 00:00:00 2001 From: Patrick Garcia Date: Tue, 1 Oct 2024 11:17:35 +0200 Subject: [PATCH 6/8] remove unnecessary package reference --- .../Foundatio.AzureStorage.Tests.csproj | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/Foundatio.AzureStorage.Tests/Foundatio.AzureStorage.Tests.csproj b/tests/Foundatio.AzureStorage.Tests/Foundatio.AzureStorage.Tests.csproj index 8281eb4..40056b4 100644 --- a/tests/Foundatio.AzureStorage.Tests/Foundatio.AzureStorage.Tests.csproj +++ b/tests/Foundatio.AzureStorage.Tests/Foundatio.AzureStorage.Tests.csproj @@ -1,4 +1,4 @@ - + @@ -12,7 +12,4 @@ docker-compose.yml - - - \ No newline at end of file From 2304276ae9eb8b85df3e6a4beba9fd7717cc0d95 Mon Sep 17 00:00:00 2001 From: Blake Niemyjski Date: Tue, 1 Oct 2024 08:51:56 -0500 Subject: [PATCH 7/8] Update src/Foundatio.AzureStorage/Storage/AzureFileStorage.cs --- src/Foundatio.AzureStorage/Storage/AzureFileStorage.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Foundatio.AzureStorage/Storage/AzureFileStorage.cs b/src/Foundatio.AzureStorage/Storage/AzureFileStorage.cs index c4e0d05..1ad853a 100644 --- a/src/Foundatio.AzureStorage/Storage/AzureFileStorage.cs +++ b/src/Foundatio.AzureStorage/Storage/AzureFileStorage.cs @@ -67,7 +67,7 @@ public async Task GetFileStreamAsync(string path, StreamMode streamMode, return streamMode switch { StreamMode.Read => await blockBlob.OpenReadAsync(null, null, null, cancellationToken).AnyContext(), StreamMode.Write => await blockBlob.OpenWriteAsync(null, null, null, cancellationToken).AnyContext(), - _ => null + _ => throw new NotSupportedException($"Stream mode {streamMode} is not supported."); }; } catch (StorageException ex) when (ex is { RequestInformation.HttpStatusCode: 404}) { _logger.LogDebug(ex, "Unable to get file stream for {Path}: File Not Found", normalizedPath); From c06735a90cb871a6a3233f00fc259a55900cf878 Mon Sep 17 00:00:00 2001 From: Blake Niemyjski Date: Tue, 1 Oct 2024 08:55:48 -0500 Subject: [PATCH 8/8] Update src/Foundatio.AzureStorage/Storage/AzureFileStorage.cs --- src/Foundatio.AzureStorage/Storage/AzureFileStorage.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Foundatio.AzureStorage/Storage/AzureFileStorage.cs b/src/Foundatio.AzureStorage/Storage/AzureFileStorage.cs index 1ad853a..0527f36 100644 --- a/src/Foundatio.AzureStorage/Storage/AzureFileStorage.cs +++ b/src/Foundatio.AzureStorage/Storage/AzureFileStorage.cs @@ -67,7 +67,7 @@ public async Task GetFileStreamAsync(string path, StreamMode streamMode, return streamMode switch { StreamMode.Read => await blockBlob.OpenReadAsync(null, null, null, cancellationToken).AnyContext(), StreamMode.Write => await blockBlob.OpenWriteAsync(null, null, null, cancellationToken).AnyContext(), - _ => throw new NotSupportedException($"Stream mode {streamMode} is not supported."); + _ => throw new NotSupportedException($"Stream mode {streamMode} is not supported.") }; } catch (StorageException ex) when (ex is { RequestInformation.HttpStatusCode: 404}) { _logger.LogDebug(ex, "Unable to get file stream for {Path}: File Not Found", normalizedPath);