Skip to content

Commit

Permalink
Fixed MD5 hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Ceredron committed Nov 6, 2024
1 parent 5c53579 commit dcddc96
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/Altinn.Broker.Integrations/Azure/BlobService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Buffers;
using System.Security.Cryptography;

using Altinn.Broker.Core.Domain;
Expand All @@ -20,7 +19,7 @@ namespace Altinn.Broker.Integrations.Azure;
public class BlobService(IResourceManager resourceManager, IHttpContextAccessor httpContextAccessor, ILogger<BlobService> logger) : IBrokerStorageService
{
private const int BLOCK_SIZE = 1024 * 1024 * 32; // 32MB
private const int BLOCKS_BEFORE_COMMIT = 1000;
private const int BLOCKS_BEFORE_COMMIT = 5;

private async Task<BlobClient> GetBlobClient(Guid fileId, ServiceOwnerEntity serviceOwnerEntity)
{
Expand Down Expand Up @@ -124,15 +123,14 @@ public async Task<Stream> DownloadFile(ServiceOwnerEntity serviceOwnerEntity, Fi
{
throw new Exception("Failed to calculate MD5 hash of uploaded file");
}
var finalMd5 = BitConverter.ToString(blobMd5.Hash).Replace("-", "").ToLowerInvariant();
await CommitBlocks(blockBlobClient, blockList, firstCommit: blockList.Count <= BLOCKS_BEFORE_COMMIT, finalMd5, cancellationToken);
await CommitBlocks(blockBlobClient, blockList, firstCommit: blockList.Count <= BLOCKS_BEFORE_COMMIT, blobMd5.Hash, cancellationToken);
blockList.Clear();

double finalSpeedMBps = position / (1024.0 * 1024) / (stopwatch.ElapsedMilliseconds / 1000.0);
logger.LogInformation($"Successfully uploaded {position / (1024.0 * 1024.0 * 1024.0):N2} GiB " +
$"in {stopwatch.ElapsedMilliseconds / 1000.0:N1}s (avg: {finalSpeedMBps:N2} MB/s)");

return finalMd5;
return BitConverter.ToString(blobMd5.Hash).Replace("-", "").ToLowerInvariant();
}
catch (Exception ex)
{
Expand All @@ -156,25 +154,27 @@ await RetryPolicy.ExecuteAsync(async () =>
null,
cancellationToken: cancellationToken
);
if (blockResponse.GetRawResponse().Status != 201)
{
throw new Exception($"Failed to upload block {blockId}");
}
});
}

private async Task CommitBlocks(BlockBlobClient client, List<string> blockList, bool firstCommit, string? finalMd5,
private async Task CommitBlocks(BlockBlobClient client, List<string> blockList, bool firstCommit, byte[]? finalMd5,
CancellationToken cancellationToken)
{
await RetryPolicy.ExecuteAsync(async () =>
{
var options = new CommitBlockListOptions
{
// Only use ifNoneMatch for the first commit to ensure concurrent upload attempts do not work simultaneously
Conditions = firstCommit ? new BlobRequestConditions { IfNoneMatch = new ETag("*") } : null
Conditions = firstCommit ? new BlobRequestConditions { IfNoneMatch = new ETag("*") } : null,
HttpHeaders = finalMd5 is null ? null : new BlobHttpHeaders
{
ContentHash = finalMd5
}
};
var response = await client.CommitBlockListAsync(blockList, options, cancellationToken);
logger.LogInformation($"Committed {blockList.Count} blocks: {response.GetRawResponse().ReasonPhrase}");
});
Expand Down

0 comments on commit dcddc96

Please sign in to comment.