Skip to content

Commit fa2c582

Browse files
committed
Add Progress listeners for initiated, complete, and failed for simple upload
update docs and function namse update comments add tests/update comments remove response from progressargs add missing properties update test Refactor test rearrange test update tests add commented out tests comments comments update comments fix spelling use interlocked read and fix build dev config update comment fix internals update dev config cache content length Change S3 service type from patch to minor rename variable stack-info: PR: #4059, branch: GarrettBeatty/stacked/1
1 parent 4f9bdfa commit fa2c582

File tree

5 files changed

+563
-30
lines changed

5 files changed

+563
-30
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"services": [
3+
{
4+
"serviceName": "S3",
5+
"type": "minor",
6+
"changeLogMessages": [
7+
"Added progress tracking events to simple upload"
8+
]
9+
}
10+
]
11+
}

sdk/src/Services/S3/Custom/Transfer/Internal/SimpleUploadCommand.cs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,18 @@ internal partial class SimpleUploadCommand : BaseCommand
4141
IAmazonS3 _s3Client;
4242
TransferUtilityConfig _config;
4343
TransferUtilityUploadRequest _fileTransporterRequest;
44+
long _totalTransferredBytes;
45+
private readonly long _contentLength;
4446

4547
internal SimpleUploadCommand(IAmazonS3 s3Client, TransferUtilityConfig config, TransferUtilityUploadRequest fileTransporterRequest)
4648
{
4749
this._s3Client = s3Client;
4850
this._config = config;
4951
this._fileTransporterRequest = fileTransporterRequest;
52+
53+
// Cache content length immediately while stream is accessible to avoid ObjectDisposedException in failure scenarios
54+
this._contentLength = this._fileTransporterRequest.ContentLength;
55+
5056
var fileName = fileTransporterRequest.FilePath;
5157
}
5258

@@ -103,9 +109,48 @@ private PutObjectRequest ConstructRequest()
103109

104110
private void PutObjectProgressEventCallback(object sender, UploadProgressArgs e)
105111
{
106-
var progressArgs = new UploadProgressArgs(e.IncrementTransferred, e.TransferredBytes, e.TotalBytes,
107-
e.CompensationForRetry, _fileTransporterRequest.FilePath);
112+
// Keep track of the total transferred bytes so that we can also return this value in case of failure
113+
long transferredBytes = Interlocked.Add(ref _totalTransferredBytes, e.IncrementTransferred - e.CompensationForRetry);
114+
115+
var progressArgs = new UploadProgressArgs(e.IncrementTransferred, transferredBytes, _contentLength,
116+
e.CompensationForRetry, _fileTransporterRequest.FilePath, _fileTransporterRequest);
108117
this._fileTransporterRequest.OnRaiseProgressEvent(progressArgs);
109118
}
119+
120+
private void FireTransferInitiatedEvent()
121+
{
122+
var initiatedArgs = new UploadInitiatedEventArgs(
123+
request: _fileTransporterRequest,
124+
filePath: _fileTransporterRequest.FilePath,
125+
totalBytes: _contentLength
126+
);
127+
128+
_fileTransporterRequest.OnRaiseTransferInitiatedEvent(initiatedArgs);
129+
}
130+
131+
private void FireTransferCompletedEvent(TransferUtilityUploadResponse response)
132+
{
133+
var completedArgs = new UploadCompletedEventArgs(
134+
request: _fileTransporterRequest,
135+
response: response,
136+
filePath: _fileTransporterRequest.FilePath,
137+
transferredBytes: Interlocked.Read(ref _totalTransferredBytes),
138+
totalBytes: _contentLength
139+
);
140+
141+
_fileTransporterRequest.OnRaiseTransferCompletedEvent(completedArgs);
142+
}
143+
144+
private void FireTransferFailedEvent()
145+
{
146+
var failedArgs = new UploadFailedEventArgs(
147+
request: _fileTransporterRequest,
148+
filePath: _fileTransporterRequest.FilePath,
149+
transferredBytes: Interlocked.Read(ref _totalTransferredBytes),
150+
totalBytes: _contentLength
151+
);
152+
153+
_fileTransporterRequest.OnRaiseTransferFailedEvent(failedArgs);
154+
}
110155
}
111156
}

sdk/src/Services/S3/Custom/Transfer/Internal/_async/SimpleUploadCommand.async.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,20 @@ await this.AsyncThrottler.WaitAsync(cancellationToken)
3838
.ConfigureAwait(continueOnCapturedContext: false);
3939
}
4040

41+
FireTransferInitiatedEvent();
42+
4143
var putRequest = ConstructRequest();
42-
await _s3Client.PutObjectAsync(putRequest, cancellationToken)
44+
var response = await _s3Client.PutObjectAsync(putRequest, cancellationToken)
4345
.ConfigureAwait(continueOnCapturedContext: false);
46+
47+
var mappedResponse = ResponseMapper.MapPutObjectResponse(response);
48+
49+
FireTransferCompletedEvent(mappedResponse);
50+
}
51+
catch (Exception)
52+
{
53+
FireTransferFailedEvent();
54+
throw;
4455
}
4556
finally
4657
{

0 commit comments

Comments
 (0)