From 3d50ea529c175b266e8309f6081e0e1e4f423979 Mon Sep 17 00:00:00 2001 From: "Wei Wei (AZURE)" Date: Mon, 4 Mar 2024 11:29:41 +0800 Subject: [PATCH] Respect the actualy bytes number read from file stream. --- .../File/Cmdlet/SetAzureStorageFileContent.cs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Storage/Storage/File/Cmdlet/SetAzureStorageFileContent.cs b/src/Storage/Storage/File/Cmdlet/SetAzureStorageFileContent.cs index 47c5b722648e..4604eeb932c7 100644 --- a/src/Storage/Storage/File/Cmdlet/SetAzureStorageFileContent.cs +++ b/src/Storage/Storage/File/Cmdlet/SetAzureStorageFileContent.cs @@ -239,27 +239,27 @@ await DataMovementTransferHelper.DoTransfer(() => using (FileStream stream = File.OpenRead(localFile.FullName)) { byte[] buffer = null; - long lastBlockSize = 0; - for (long offset = 0; offset < fileSize; offset += blockSize) + for (long offset = 0; offset < fileSize;) { - long currentBlockSize = offset + blockSize < fileSize ? blockSize : fileSize - offset; + long targetBlockSize = offset + blockSize < fileSize ? blockSize : fileSize - offset; // create new buffer, the old buffer will be GC - buffer = new byte[currentBlockSize]; - lastBlockSize = currentBlockSize; + buffer = new byte[targetBlockSize]; - await stream.ReadAsync(buffer: buffer, offset: 0, count: (int)currentBlockSize); + int actualBlockSize = await stream.ReadAsync(buffer: buffer, offset: 0, count: (int)targetBlockSize); if (!fipsEnabled && hash != null) { - hash.AppendData(buffer); + hash.AppendData(buffer, 0, actualBlockSize); } Task task = UploadFileRangAsync(fileClient, - new HttpRange(offset, currentBlockSize), - new MemoryStream(buffer), + new HttpRange(offset, actualBlockSize), + new MemoryStream(buffer, 0, actualBlockSize), progressHandler); runningTasks.Add(task); + offset += actualBlockSize; + // Check if any of upload range tasks are still busy if (runningTasks.Count >= maxWorkers) {