Skip to content

Commit 16f99e1

Browse files
committed
cache content length
1 parent 701fb99 commit 16f99e1

File tree

2 files changed

+34
-31
lines changed

2 files changed

+34
-31
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,17 @@ internal partial class SimpleUploadCommand : BaseCommand
4242
TransferUtilityConfig _config;
4343
TransferUtilityUploadRequest _fileTransporterRequest;
4444
long _totalTransferredBytes;
45+
private readonly long _cachedContentLength;
4546

4647
internal SimpleUploadCommand(IAmazonS3 s3Client, TransferUtilityConfig config, TransferUtilityUploadRequest fileTransporterRequest)
4748
{
4849
this._s3Client = s3Client;
4950
this._config = config;
5051
this._fileTransporterRequest = fileTransporterRequest;
52+
53+
// Cache content length immediately while stream is accessible to avoid ObjectDisposedException in failure scenarios
54+
this._cachedContentLength = this._fileTransporterRequest.ContentLength;
55+
5156
var fileName = fileTransporterRequest.FilePath;
5257
}
5358

@@ -107,7 +112,7 @@ private void PutObjectProgressEventCallback(object sender, UploadProgressArgs e)
107112
// Keep track of the total transferred bytes so that we can also return this value in case of failure
108113
long transferredBytes = Interlocked.Add(ref _totalTransferredBytes, e.IncrementTransferred - e.CompensationForRetry);
109114

110-
var progressArgs = new UploadProgressArgs(e.IncrementTransferred, transferredBytes, e.TotalBytes,
115+
var progressArgs = new UploadProgressArgs(e.IncrementTransferred, transferredBytes, _cachedContentLength,
111116
e.CompensationForRetry, _fileTransporterRequest.FilePath, _fileTransporterRequest);
112117
this._fileTransporterRequest.OnRaiseProgressEvent(progressArgs);
113118
}
@@ -117,7 +122,7 @@ private void FireTransferInitiatedEvent()
117122
var initiatedArgs = new UploadInitiatedEventArgs(
118123
request: _fileTransporterRequest,
119124
filePath: _fileTransporterRequest.FilePath,
120-
totalBytes: _fileTransporterRequest.ContentLength
125+
totalBytes: _cachedContentLength
121126
);
122127

123128
_fileTransporterRequest.OnRaiseTransferInitiatedEvent(initiatedArgs);
@@ -130,7 +135,7 @@ private void FireTransferCompletedEvent(TransferUtilityUploadResponse response)
130135
response: response,
131136
filePath: _fileTransporterRequest.FilePath,
132137
transferredBytes: Interlocked.Read(ref _totalTransferredBytes),
133-
totalBytes: _fileTransporterRequest.ContentLength
138+
totalBytes: _cachedContentLength
134139
);
135140

136141
_fileTransporterRequest.OnRaiseTransferCompletedEvent(completedArgs);
@@ -142,7 +147,7 @@ private void FireTransferFailedEvent()
142147
request: _fileTransporterRequest,
143148
filePath: _fileTransporterRequest.FilePath,
144149
transferredBytes: Interlocked.Read(ref _totalTransferredBytes),
145-
totalBytes: _fileTransporterRequest.ContentLength
150+
totalBytes: _cachedContentLength
146151
);
147152

148153
_fileTransporterRequest.OnRaiseTransferFailedEvent(failedArgs);

sdk/test/Services/S3/IntegrationTests/TransferUtilityTests.cs

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public class TransferUtilityTests : TestBase<AmazonS3Client>
3030
private static string fullPath;
3131
private const string testContent = "This is the content body!";
3232
private const string testFile = "PutObjectFile.txt";
33+
private static string testFilePath;
34+
private const string testKey = "SimpleUploadProgressTotalBytesTestFile.txt";
3335

3436
[ClassInitialize()]
3537
public static void ClassInitialize(TestContext a)
@@ -66,6 +68,7 @@ public static void ClassInitialize(TestContext a)
6668

6769
fullPath = Path.GetFullPath(testFile);
6870
File.WriteAllText(fullPath, testContent);
71+
testFilePath = fullPath; // Use the same file for the TotalBytes test
6972
}
7073

7174
[ClassCleanup]
@@ -444,41 +447,36 @@ public void UploadUnSeekableStreamFileSizeEqualToPartBufferSize()
444447
}
445448

446449
[TestMethod]
447-
[TestCategory("S3")]
448-
public void UploadUnseekableStreamFileSizeBetweenMinPartSizeAndPartBufferSize()
450+
public void SimpleUploadProgressTotalBytesTest()
449451
{
450-
var client = Client;
451-
var fileName = UtilityMethods.GenerateName(@"SimpleUploadTest\BetweenMinPartSizeAndPartBufferSize");
452-
var path = Path.Combine(BasePath, fileName);
453-
// there was a bug where the transfer utility was uploading 13MB file
454-
// when the file size was between 5MB and (5MB + 8192). 8192 is the s3Client.Config.BufferSize
455-
var fileSize = 5 * MEG_SIZE + 1;
456-
457-
UtilityMethods.GenerateFile(path, fileSize);
458-
//take the generated file and turn it into an unseekable stream
459-
460-
var stream = GenerateUnseekableStreamFromFile(path);
461-
using (var tu = new Amazon.S3.Transfer.TransferUtility(client))
452+
var transferConfig = new TransferUtilityConfig()
462453
{
463-
tu.Upload(stream, bucketName, fileName);
454+
MinSizeBeforePartUpload = 20 * MEG_SIZE,
455+
};
464456

465-
var metadata = Client.GetObjectMetadata(new GetObjectMetadataRequest
457+
var progressValidator = new TransferProgressValidator<UploadProgressArgs>
458+
{
459+
Validate = (progress) =>
466460
{
467-
BucketName = bucketName,
468-
Key = fileName
469-
});
470-
Assert.AreEqual(fileSize, metadata.ContentLength);
461+
Assert.IsTrue(progress.TotalBytes > 0, "TotalBytes should be greater than 0");
462+
Assert.AreEqual(testContent.Length, progress.TotalBytes, "TotalBytes should equal file length");
463+
}
464+
};
471465

472-
//Download the file and validate content of downloaded file is equal.
473-
var downloadPath = path + ".download";
474-
var downloadRequest = new TransferUtilityDownloadRequest
466+
using (var fileTransferUtility = new TransferUtility(Client, transferConfig))
467+
{
468+
var request = new TransferUtilityUploadRequest()
475469
{
476470
BucketName = bucketName,
477-
Key = fileName,
478-
FilePath = downloadPath
471+
FilePath = testFilePath,
472+
Key = testKey
479473
};
480-
tu.Download(downloadRequest);
481-
UtilityMethods.CompareFiles(path, downloadPath);
474+
475+
request.UploadProgressEvent += progressValidator.OnProgressEvent;
476+
477+
fileTransferUtility.Upload(request);
478+
479+
progressValidator.AssertOnCompletion();
482480
}
483481
}
484482

0 commit comments

Comments
 (0)