Skip to content

Commit 230138d

Browse files
authored
Fix: Do not overwrite content-type header if set already for MPU scenarios (#3829)
1 parent ce549b2 commit 230138d

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
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: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,22 @@ private InitiateMultipartUploadRequest ConstructInitiateMultipartUploadRequest(R
348348
if (this._fileTransporterRequest.Metadata != null && this._fileTransporterRequest.Metadata.Count > 0)
349349
initRequest.Metadata = this._fileTransporterRequest.Metadata;
350350
if (this._fileTransporterRequest.Headers != null && this._fileTransporterRequest.Headers.Count > 0)
351-
initRequest.Headers = this._fileTransporterRequest.Headers;
352-
351+
{
352+
foreach (var headerKey in this._fileTransporterRequest.Headers.Keys)
353+
{
354+
// InitiateMultipartUploadRequest already has its content-type header set.
355+
// don't copy the Content-Type if it's set already as to not overwrite the original content-type
356+
if (string.Equals(headerKey, HeaderKeys.ContentTypeHeader) && this._fileTransporterRequest.IsSetContentType())
357+
{
358+
continue;
359+
}
360+
else
361+
{
362+
initRequest.Headers[headerKey] = this._fileTransporterRequest.Headers[headerKey];
363+
}
364+
}
365+
}
366+
353367
return initRequest;
354368
}
355369

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using Amazon.S3.Util;
1212
using AWSSDK_DotNet.IntegrationTests.Utils;
1313
using Amazon.Util;
14+
using System.Net.Mime;
15+
using System.Runtime.InteropServices.ComTypes;
1416

1517
namespace AWSSDK_DotNet.IntegrationTests.Tests.S3
1618
{
@@ -1031,6 +1033,33 @@ public void TestZeroLengthDownloadToNonExistingPath()
10311033
Assert.IsTrue(File.Exists(filePath));
10321034
}
10331035

1036+
[TestMethod]
1037+
[TestCategory("S3")]
1038+
public void TestMultipartUploadWithSetContentTypeNotOverwritten()
1039+
{
1040+
// 20 MB stream
1041+
var fileName = UtilityMethods.GenerateName(@"SetContentType");
1042+
var path = Path.Combine(BasePath, fileName);
1043+
var fileSize = 20 * MEG_SIZE;
1044+
UtilityMethods.GenerateFile(path, 20 * MEG_SIZE);
1045+
var transferUtilityRequest = new TransferUtilityUploadRequest
1046+
{
1047+
BucketName = bucketName,
1048+
FilePath = path,
1049+
Key = "test-content-type",
1050+
ContentType = MediaTypeNames.Text.Plain,
1051+
Headers =
1052+
{
1053+
ContentEncoding = "gzip",
1054+
},
1055+
};
1056+
var tu = new TransferUtility(Client);
1057+
tu.Upload(transferUtilityRequest);
1058+
var downloadPath = path + ".download";
1059+
var metadata = Client.GetObjectMetadata(new GetObjectMetadataRequest { BucketName = bucketName, Key = "test-content-type" });
1060+
Assert.IsTrue(metadata.Headers.ContentType.Equals(MediaTypeNames.Text.Plain));
1061+
}
1062+
10341063
#if ASYNC_AWAIT
10351064

10361065
[TestMethod]

0 commit comments

Comments
 (0)