Russ960
Russ960

Reputation: 1339

AWS Multi-Part Upload error: The XML you provided was not well-formed or did not validate against our published schema

I have used the sample code from Amazon and it seems to work ok but then fails with the following error:

The XML you provided was not well-formed or did not validate against our published schema

I think the ETags is required though commented out in doc (http://docs.aws.amazon.com/AmazonS3/latest/dev/LLuploadFileDotNet.html). I have been struggling building this.

Code: // List to store upload part responses. List uploadResponses = new List();

    // 1. Initialize.
    InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest
    {
        BucketName = bucketName,
        Key = keyName
    };

    InitiateMultipartUploadResponse initResponse = s3Client.InitiateMultipartUpload(initiateRequest);

    // 2. Upload Parts.
    long contentLength = new FileInfo(fileName).Length;
    long partSize = 5 * (long)Math.Pow(2, 20); // 5 MB

    try
    {
        long filePosition = 0;
        for (int i = 1; filePosition < contentLength; i++)
        {

            // Create request to upload a part.
            UploadPartRequest uploadRequest = new UploadPartRequest
            {
                BucketName = bucketName,
                Key = keyName,
                UploadId = initResponse.UploadId,
                PartNumber = i,
                PartSize = partSize,
                FilePosition = filePosition,
                FilePath = fileName
            };

            // Upload part and add response to our list.
            uploadResponses.Add(s3Client.UploadPart(uploadRequest));
            filePosition += partSize;
        }

        // Step 3: complete.
        CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest
        {
            BucketName = bucketName,
            Key = keyName,
            UploadId = initResponse.UploadId
        };

        CompleteMultipartUploadResponse completeUploadResponse =
          s3Client.CompleteMultipartUpload(completeRequest);

        return "Complete";

Upvotes: 5

Views: 7215

Answers (2)

wolle
wolle

Reputation: 31

you have to simply add the last line, instaed of creating a list an calling UploadPart twice:

 CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest
            {
                BucketName = bucketName,
                Key = keyName,
                UploadId = initResponse.UploadId,
                PartETags = uploadResponses.Select(part=>new PartETag(part.PartNumber,part.ETag)).ToList()
            };

Upvotes: 3

Russ960
Russ960

Reputation: 1339

The issue was found that while the example code does not use PartETag it is necessary. I added the following under the definition of UploadPartResponse:

List<PartETag> partETags = new List<PartETag>();

Then added the following after the uploadResponses.Add portion of code:

PartETag petag = new PartETag(s3Client.UploadPart(uploadRequest).PartNumber, s3Client.UploadPart(uploadRequest).ETag);
partETags.Add(petag);

Finally modified CompleteMultipartUploadRequest to the code below:

// Step 3: complete.
CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest
{
    BucketName = bucketName,
    Key = fileTest, 
    UploadId = initResponse.UploadId,
    PartETags = partETags
};

Upvotes: 8

Related Questions