Skip to content

Commit 8ffc6cc

Browse files
author
TRybina132
committed
fix azure blob stream lenght
1 parent e7a038a commit 8ffc6cc

File tree

4 files changed

+86
-8
lines changed

4 files changed

+86
-8
lines changed

ManagedCode.Storage.Azure/BlobStream.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace ManagedCode.Storage.Azure;
88

99
public class BlobStream : Stream
1010
{
11+
// TODO: add same thing to google and aws
1112
private const string MetadataLengthKey = "STREAM_LENGTH";
1213
private const int PageSizeInBytes = 512;
1314
public const int DefaultBufferSize = 1024 * 1024 * 4;
@@ -39,6 +40,7 @@ public override long Length
3940
{
4041
var realLength = 0L;
4142
var metadata = _pageBlob.GetProperties().Value.Metadata;
43+
var contentLenght = _pageBlob.GetProperties().Value.ContentLength;
4244
if (metadata.TryGetValue(MetadataLengthKey, out var length))
4345
{
4446
if (long.TryParse(length, out realLength))
@@ -47,8 +49,8 @@ public override long Length
4749
}
4850
}
4951

50-
SetLengthInternal(realLength);
51-
return realLength;
52+
SetLengthInternal(contentLenght);
53+
return contentLenght;
5254
}
5355
}
5456

ManagedCode.Storage.Tests/Common/FileHelper.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,34 @@ public static LocalFile GenerateLocalFile(string fileName, int byteSize)
2525
return localFile;
2626
}
2727

28+
public static LocalFile GenerateLocalFileWithData(LocalFile file, int sizeInBytes)
29+
{
30+
using (var fileStream = file.FileStream)
31+
{
32+
Random random = new Random();
33+
byte[] buffer = new byte[1024]; // Buffer for writing in chunks
34+
35+
while (sizeInBytes > 0)
36+
{
37+
int bytesToWrite = (int) Math.Min(sizeInBytes, buffer.Length);
38+
39+
for (int i = 0; i < bytesToWrite; i++)
40+
{
41+
buffer[i] = (byte) random.Next(65, 91); // 'A' to 'Z'
42+
if (random.Next(2) == 0)
43+
{
44+
buffer[i] = (byte) random.Next(97, 123); // 'a' to 'z'
45+
}
46+
}
47+
48+
fileStream.Write(buffer, 0, bytesToWrite);
49+
sizeInBytes -= bytesToWrite;
50+
}
51+
}
52+
53+
return file;
54+
}
55+
2856
public static IFormFile GenerateFormFile(string fileName, int byteSize)
2957
{
3058
var localFile = GenerateLocalFile(fileName, byteSize);
Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,58 @@
1+
using System;
2+
using System.IO;
3+
using System.Threading.Tasks;
4+
using FluentAssertions;
5+
using ManagedCode.Storage.Azure;
6+
using ManagedCode.Storage.Core.Models;
7+
using ManagedCode.Storage.Tests.Common;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using Testcontainers.Azurite;
10+
using Xunit;
11+
112
namespace ManagedCode.Storage.Tests.Storages.Azure;
213

3-
public class AzureBlobStreamTests
14+
public class AzureBlobStreamTests : StreamTests<AzuriteContainer>
415
{
16+
protected override AzuriteContainer Build()
17+
{
18+
return new AzuriteBuilder()
19+
.WithImage("mcr.microsoft.com/azure-storage/azurite:3.26.0")
20+
.Build();
21+
}
22+
23+
protected override ServiceProvider ConfigureServices()
24+
{
25+
return AzureConfigurator.ConfigureServices(Container.GetConnectionString());
26+
}
527

28+
[Fact]
29+
public async Task ReadStream_WhenFileExists_ReturnData()
30+
{
31+
// Arrange
32+
var directory = "test-directory";
33+
var fileName = Guid.NewGuid().ToString();
34+
await using var localFile = LocalFile.FromRandomNameWithExtension(".txt");
35+
FileHelper.GenerateLocalFileWithData(localFile, 10);
36+
var storage = (IAzureStorage) Storage;
37+
38+
UploadOptions options = new() { FileName = localFile.Name, Directory = directory };
39+
var result = await storage.UploadAsync(localFile.FileInfo.OpenRead(), options);
40+
41+
await using var blobStream = storage.GetBlobStream(result.Value.FullName);
42+
43+
var chunkSize = localFile.FileInfo.Length;
44+
byte[] fileChunk1 = new byte[chunkSize];
45+
byte[] fileChunk2 = new byte[chunkSize];
46+
47+
// Act
48+
using var streamReader = new StreamReader(blobStream);
49+
var content = await streamReader.ReadToEndAsync();
50+
51+
// Assert
52+
content.Should().NotBeNullOrEmpty();
53+
using var fileReader = new StreamReader(localFile.FileStream);
54+
var fileContent = await fileReader.ReadToEndAsync();
55+
fileContent.Should().NotBeNullOrEmpty();
56+
content.Should().Be(fileContent);
57+
}
658
}

ManagedCode.Storage.Tests/Storages/StreamTests.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,5 @@ namespace ManagedCode.Storage.Tests.Storages;
88
public abstract class StreamTests<T> : BaseContainer<T>
99
where T : IContainer
1010
{
11-
[Fact]
12-
public async Task ReadStream_WhenFileExists_ReturnData()
13-
{
14-
15-
}
11+
1612
}

0 commit comments

Comments
 (0)