Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement get file stream for writing #33

Merged
merged 9 commits into from
Oct 1, 2024
15 changes: 7 additions & 8 deletions src/Foundatio.AzureStorage/Storage/AzureFileStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public AzureFileStorage(Builder<AzureFileStorageOptionsBuilder, AzureFileStorage
ISerializer IHaveSerializer.Serializer => _serializer;
public CloudBlobContainer Container => _container;

[Obsolete($"Use {nameof(GetFileStreamAsync)} with {nameof(FileAccess)} instead to define read or write behaviour of stream")]
[Obsolete($"Use {nameof(GetFileStreamAsync)} with {nameof(StreamMode)} instead to define read or write behavior of stream")]
public Task<Stream> GetFileStreamAsync(string path, CancellationToken cancellationToken = default)
=> GetFileStreamAsync(path, StreamMode.Read, cancellationToken);

Expand All @@ -57,20 +57,19 @@ public async Task<Stream> GetFileStreamAsync(string path, StreamMode streamMode,
if (String.IsNullOrEmpty(path))
throw new ArgumentNullException(nameof(path));

if (streamMode is StreamMode.Write)
throw new NotSupportedException($"Stream mode {streamMode} is not supported.");

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();
}
catch (StorageException ex) when (ex is { RequestInformation.HttpStatusCode: 404 })
{
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.")
};
} 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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\src\Foundatio.AzureStorage\Foundatio.AzureStorage.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down