AWS多部件上载错误:您提供的XML格式不正确或未针对我们发布的架构进行验证

时间:2015-11-25 20:16:22

标签: c# amazon-web-services amazon-s3

我使用了亚马逊的示例代码,它似乎工作正常,但随后出现以下错误:

  

您提供的XML格式不正确或未经过验证   我们发布的架构

我认为虽然在doc(http://docs.aws.amazon.com/AmazonS3/latest/dev/LLuploadFileDotNet.html)中注释了ETag是必需的。我一直在努力建设这个。

代码:         //列出存储上传部分响应。         列出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";

2 个答案:

答案 0 :(得分:5)

发现问题是虽然示例代码不使用PartETag,但这是必要的。我在UploadPartResponse的定义下添加了以下内容:

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

然后在uploadResponses.Add部分代码之后添加以下代码:

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

最后将CompleteMultipartUploadRequest修改为以下代码:

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

答案 1 :(得分:2)

你必须简单地添加最后一行,创建一个名单,调用UploadPart两次:

<a class="simple-mail-link" href="mailto:?subject=Page%20Title&amp;body=Hi%20there%2C%20this%20might%20be%20interesting%20for%20you%0D%0AThis%20is%20the%20link%3A%20http%3A%2F%2Fblahblahblah.com%0D%0AHave%20a%20great%20day%21%0D%0AStefaan"><div class="label"> <div class="dashicons dashicons-admin-plugins"></div>Forward this to a friend</div></a></body>