Skip to content

Commit aa1478f

Browse files
authored
Fix: Do not overwrite content-type header if explicitly set in multipart upload scenario (#3830)
1 parent e121f61 commit aa1478f

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
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": "patch",
6+
"changeLogMessages": [
7+
"Fix: Fixed a bug where Content-Type header was being overwritten in multipart upload scenarios."
8+
]
9+
}
10+
]
11+
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,21 @@ private InitiateMultipartUploadRequest ConstructInitiateMultipartUploadRequest(R
354354
if (this._fileTransporterRequest.Metadata != null && this._fileTransporterRequest.Metadata.Count > 0)
355355
initRequest.Metadata = this._fileTransporterRequest.Metadata;
356356
if (this._fileTransporterRequest.Headers != null && this._fileTransporterRequest.Headers.Count > 0)
357-
initRequest.Headers = this._fileTransporterRequest.Headers;
357+
{
358+
foreach (var headerKey in this._fileTransporterRequest.Headers.Keys)
359+
{
360+
// InitiateMultipartUploadRequest already has its content-type header set.
361+
// don't copy the Content-Type if it's set already as to not overwrite the original content-type
362+
if (string.Equals(headerKey, HeaderKeys.ContentTypeHeader) && this._fileTransporterRequest.IsSetContentType())
363+
{
364+
continue;
365+
}
366+
else
367+
{
368+
initRequest.Headers[headerKey] = this._fileTransporterRequest.Headers[headerKey];
369+
}
370+
}
371+
}
358372

359373
return initRequest;
360374
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Amazon.S3.Util;
1212
using AWSSDK_DotNet.IntegrationTests.Utils;
1313
using Amazon.Util;
14+
using System.Net.Mime;
1415

1516
namespace AWSSDK_DotNet.IntegrationTests.Tests.S3
1617
{
@@ -1011,6 +1012,32 @@ public void TestZeroLengthDownloadToNonExistingPath()
10111012
Assert.IsTrue(File.Exists(filePath));
10121013
}
10131014

1015+
[TestMethod]
1016+
[TestCategory("S3")]
1017+
public void TestMultipartUploadWithSetContentTypeNotOverwritten()
1018+
{
1019+
// 20 MB stream
1020+
var fileName = UtilityMethods.GenerateName(@"SetContentType");
1021+
var path = Path.Combine(BasePath, fileName);
1022+
var fileSize = 20 * MEG_SIZE;
1023+
UtilityMethods.GenerateFile(path, 20 * MEG_SIZE);
1024+
var transferUtilityRequest = new TransferUtilityUploadRequest
1025+
{
1026+
BucketName = bucketName,
1027+
FilePath = path,
1028+
Key = "test-content-type",
1029+
ContentType = MediaTypeNames.Text.Plain,
1030+
Headers =
1031+
{
1032+
ContentEncoding = "gzip",
1033+
},
1034+
};
1035+
var tu = new TransferUtility(Client);
1036+
tu.Upload(transferUtilityRequest);
1037+
var downloadPath = path + ".download";
1038+
var metadata = Client.GetObjectMetadata(new GetObjectMetadataRequest { BucketName = bucketName, Key = "test-content-type" });
1039+
Assert.IsTrue(metadata.Headers.ContentType.Equals(MediaTypeNames.Text.Plain));
1040+
}
10141041
#if ASYNC_AWAIT
10151042

10161043
[TestMethod]

0 commit comments

Comments
 (0)