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
2 changes: 1 addition & 1 deletion src/Foundatio.AzureStorage/Foundatio.AzureStorage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageReference Include="Microsoft.Azure.Storage.Blob" Version="11.2.3" />
<PackageReference Include="Microsoft.Azure.Storage.Queue" Version="11.2.3" />

<PackageReference Include="Foundatio" Version="10.6.1" Condition="'$(ReferenceFoundatioSource)' == '' OR '$(ReferenceFoundatioSource)' == 'false'" />
<PackageReference Include="Foundatio" Version="10.6.2-alpha.0.4" Condition="'$(ReferenceFoundatioSource)' == '' OR '$(ReferenceFoundatioSource)' == 'false'" />
<ProjectReference Include="..\..\..\Foundatio\src\Foundatio\Foundatio.csproj" Condition="'$(ReferenceFoundatioSource)' == 'true'" />
</ItemGroup>
</Project>
19 changes: 13 additions & 6 deletions src/Foundatio.AzureStorage/Storage/AzureFileStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,24 @@ public AzureFileStorage(Builder<AzureFileStorageOptionsBuilder, AzureFileStorage

ISerializer IHaveSerializer.Serializer => _serializer;

public async Task<Stream> GetFileStreamAsync(string path, CancellationToken cancellationToken = default) {
if (String.IsNullOrEmpty(path))
throw new ArgumentNullException(nameof(path));
public Task<Stream> GetFileStreamAsync(string path, CancellationToken cancellationToken = default)
=> GetFileStreamAsync(path, StreamMode.Read, cancellationToken);

public async Task<Stream> GetFileStreamAsync(string path, StreamMode streamMode, 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();
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
garcipat marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Update="Foundatio.TestHarness" Version="10.6.2-alpha.0.4" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,10 @@ public override Task CanRoundTripSeekableStreamAsync() {
public override Task WillRespectStreamOffsetAsync() {
return base.WillRespectStreamOffsetAsync();
}

[Fact]
public override Task WillWriteStreamContentAsync() {
return base.WillWriteStreamContentAsync();
}
}
}