如何为AWS S3多部分上传生成md5校验和?

时间:2016-07-29 13:32:23

标签: amazon-web-services amazon-s3 multipart md5sum

我已成功将多部分文件上传到AWS S3,但现在我尝试向每个部分添加MD5校验和:

static void sendPart(existingBucketName, keyName, multipartRepsonse, partNum,
                     sendBuffer, partSize, vertx, partETags, s3, req, resultClosure)
{

    // Create request to upload a part.
    MessageDigest md = MessageDigest.getInstance("MD5")
    byte[] digest = md.digest(sendBuffer.bytes)
    println(digest.toString())
    InputStream inputStream = new ByteArrayInputStream(sendBuffer.bytes)
    UploadPartRequest uploadRequest = new UploadPartRequest()
        .withBucketName(existingBucketName).withKey(keyName)
        .withUploadId(multipartRepsonse.getUploadId()).withPartNumber(partNum)
        .withInputStream(inputStream)
        .withMD5Digest(Base64.getEncoder().encode(digest).toString())
        .withPartSize(partSize);

    // Upload part and add response to our list.
    vertx.executeBlocking({ future ->

            // Do the blocking operation in here

            // Imagine this was a call to a blocking API to get the result
            try {
                println("Sending chunk for ${keyName}")
                PartETag eTag = s3.uploadPart(uploadRequest).getPartETag()
                partETags.add(eTag);
                println("Etag: " + eTag.ETag)
                req.response().write("Sending Chunk\n")
            } catch(Exception e) {
            }

            def result = "success!"

            future.complete(result)
        }, resultClosure)
}

但是我收到以下错误:

  

AmazonS3Exception:您提供的XML格式不正确或没有   验证我们发布的架构(服务:Amazon S3;状态   代码:400;错误代码:MalformedXML;请求ID:91542E819781FDFC),S3   扩展请求ID:   yQs45H / ozn5 + xlxV9lRgCQWwv6gQysT6A4ablq7 / Epq06pUzy0qGvMc + YAkJjo / RsHk2dedH + PI =

我做错了什么?

1 个答案:

答案 0 :(得分:0)

看起来我正在错误地转换摘要。

static void sendPart(existingBucketName, keyName, multipartRepsonse, partNum,
                     sendBuffer, partSize, vertx, partETags, s3, req, resultClosure)
{

    // Create request to upload a part.
    MessageDigest md = MessageDigest.getInstance("MD5")
    byte[] digest = md.digest(sendBuffer.bytes)
    InputStream inputStream = new ByteArrayInputStream(sendBuffer.bytes)
    UploadPartRequest uploadRequest = new UploadPartRequest()
        .withBucketName(existingBucketName).withKey(keyName)
        .withUploadId(multipartRepsonse.getUploadId()).withPartNumber(partNum)
        .withInputStream(inputStream)
        .withMD5Digest(Base64.getEncoder().encodeToString(digest))
        .withPartSize(partSize)

    // Upload part and add response to our list.
    vertx.executeBlocking({ future ->

            try {
                println("Sending chunk for ${keyName}")
                PartETag eTag = s3.uploadPart(uploadRequest).getPartETag()
                partETags.add(eTag);
                req.response().write("Sending Chunk\n")
            } catch(Exception e) {
            }

            def result = "success!"

            future.complete(result)
        }, resultClosure)
}