AWS多部分上传错误:您提供的XML格式不正确,或者没有根据我们发布的架构进行验证

本文关键字:有根据 我们 或者 验证 格式 错误 多部 AWS XML 不正确 | 更新日期: 2023-09-27 18:24:29

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

您提供的XML格式不正确或未根据我们发布的架构

我认为ETag是必需的,尽管在文档中有注释(http://docs.aws.amazon.com/AmazonS3/latest/dev/LLuploadFileDotNet.html)。我一直在努力建造这个。

代码://用于存储上传部件响应的列表。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";

AWS多部分上传错误:您提供的XML格式不正确,或者没有根据我们发布的架构进行验证

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

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

然后在上传响应后添加以下内容。添加部分代码:

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
};

您只需添加最后一行,就可以创建一个列表并调用UploadPart两次:

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