Skip to content

Commit

Permalink
Only upload when count is greater than 0
Browse files Browse the repository at this point in the history
  • Loading branch information
Enkidu93 committed Nov 3, 2023
1 parent cdc980f commit 3eeef67
Showing 1 changed file with 32 additions and 29 deletions.
61 changes: 32 additions & 29 deletions src/SIL.Machine.AspNetCore/Services/S3WriteStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,37 +55,40 @@ public override void Flush() { }

public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
try
if (count > 0)
{
using MemoryStream ms = new(buffer, offset, count);
int partNumber = _uploadResponses.Count + 1;
UploadPartRequest request =
new()
{
BucketName = _bucketName,
Key = _key,
UploadId = _uploadId,
PartNumber = partNumber,
InputStream = ms,
PartSize = MaxPartSize
};
request.StreamTransferProgress += new EventHandler<StreamTransferProgressArgs>(
(_, e) =>
{
_logger.LogDebug($"Transferred {e.TransferredBytes}/{e.TotalBytes}");
}
);
UploadPartResponse response = await _client.UploadPartAsync(request);
if (response.HttpStatusCode != HttpStatusCode.OK)
throw new HttpRequestException(
$"Tried to upload part {partNumber} of upload {_uploadId} to {_bucketName}/{_key} but received response code {response.HttpStatusCode}"
try
{
using MemoryStream ms = new(buffer, offset, count);
int partNumber = _uploadResponses.Count + 1;
UploadPartRequest request =
new()
{
BucketName = _bucketName,
Key = _key,
UploadId = _uploadId,
PartNumber = partNumber,
InputStream = ms,
PartSize = MaxPartSize
};
request.StreamTransferProgress += new EventHandler<StreamTransferProgressArgs>(
(_, e) =>
{
_logger.LogDebug($"Transferred {e.TransferredBytes}/{e.TotalBytes}");
}
);
_uploadResponses.Add(response);
}
catch (Exception e)
{
await AbortAsync(e);
throw;
UploadPartResponse response = await _client.UploadPartAsync(request);
if (response.HttpStatusCode != HttpStatusCode.OK)
throw new HttpRequestException(
$"Tried to upload part {partNumber} of upload {_uploadId} to {_bucketName}/{_key} but received response code {response.HttpStatusCode}"
);
_uploadResponses.Add(response);
}
catch (Exception e)
{
await AbortAsync(e);
throw;
}
}
}

Expand Down

0 comments on commit 3eeef67

Please sign in to comment.