aws/aws-sdk-net — pull request #105 by toddwprice (2014-05-19 21:49:31+00:00)
Title: ISSUE #103: fixed problem with headers being overwritten / removed

This pull request fixes a problem I discovered using the TransferUtility class to put objects in S3. The following code illustrates a test that will fail:

```
public void TestTransferUtilityUpload()
{
string accessKey = "myaccesskey";
string secretKey = "mysecretkey";

TransferUtility utility = new TransferUtility(accessKey, secretKey, Amazon.RegionEndpoint.USEast1);
var request = new TransferUtilityUploadRequest();
var contentType = "image/jpeg";
var bucketName = "MyTestBucket";
var imageStream = new MemoryStream();
Resources.thumbnail.Save(imageStream, ImageFormat.Jpeg);

//put snapshot
var guid = Guid.NewGuid().ToString();
request.Key = guid;
request.BucketName = bucketName;
request.ContentType = contentType;
request.AutoCloseStream = true;
request.InputStream = imageStream;
utility.Upload(request);

//get snapshot
var s3 = new AmazonS3Client(accessKey, secretKey, Amazon.RegionEndpoint.USEast1);
var getRequest = new GetObjectRequest();
getRequest.BucketName = bucketName;
getRequest.Key = guid;
var response = s3.GetObject(getRequest);
Assert.NotNull(response.ResponseStream);
Assert.Equal(response.Headers.ContentType, contentType); //fails -- returns "text/plain" as content type
}
```

The reason it fails is that the Headers collection of the PutObjectRequest class are written to in the wrong order in SimpleUploadCommand.cs. By writing the entire collection first as define by the consumer of the class, then writing specific elements of the collection if passed afterwards, we avoid the situation I encountered where I write a ContentType only to find it has been removed before final execution of the command.

My solution still doesn't address the fact that elements of the collection might be later overwritten. e.g. If I pass a Headers collection that contains a content type of "image/png", then pass a ContentType of "image/jpg", the prior value will be overwritten. Personally I think the Headers collection should not be passed at all if you are going to allow specific headers to be passed individually. Allow one or the other, but not both.