2016-07-29 119 views
0

我成功上傳多部分文件到AWS S3,但我現在在嘗試廣告MD5校驗每一部分:如何爲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: The XML you provided was not well-formed or did not validate against our published schema (Service: Amazon S3; Status Code: 400; Error Code: MalformedXML; Request ID: 91542E819781FDFC), S3 Extended Request ID: yQs45H/ozn5+xlxV9lRgCQWwv6gQysT6A4ablq7/Epq06pUzy0qGvMc+YAkJjo/RsHk2dedH+pI=

我做錯了什麼?

回答

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