OSS SDK for Java uses MD5 verification and CRC-64 to ensure data integrity when you upload, download, and copy objects.

MD5 verification

If you configure Content-MD5 in an object upload request, OSS calculates the MD5 hash of the uploaded object. If the calculated MD5 hash is different from the MD5 hash configured in the upload request, InvalidDigest is returned. This allows OSS to ensure data integrity for object upload. If InvalidDigest is returned, you must upload the object again.

The following code provides an example on how to perform MD5 verification in object upload:

package main

import (
    "crypto/md5"
    "encoding/base64"
    "fmt"
    "os"
    "strings"

    "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
    // Create an OSSClient instance.
    client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Specify the name of the bucket to which to upload the object.
    bucket, err := client.Bucket("<yourBucketName>")
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Obtain the content of the object to upload.
    content := "<yourObjectValue>"

    // Calculate the MD5 hash of the object content.
    h := md5.New()
    h.Write([]byte(content))
    strMd5 := base64.StdEncoding.EncodeToString(h.Sum(nil))

    // Upload the object by using simple upload.
    err = bucket.PutObject("<yourObjectName>", strings.NewReader(content), oss.ContentMD5(strMd5))
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
}
Note MD5 verification can be used for putObject, getObject, appendObject, postObject, and uploadPart.

CRC-64

By default, CRC-64 is enabled to ensure data integrity when you upload, download, and copy an object.

  • CRC-64 can be used for putObject, getObject, appendObject, and uploadPart operations. By default, CRC-64 is enabled when you upload an object. If the CRC64 checksum calculated on the client is different from the CRC64 checksum returned by the OSS server, an error is returned.
  • CRC-64 cannot be used in range downloads.
  • CRC-64 consumes CPU resources and slows down uploads and downloads.

The following code provides an example on how to perform CRC-64 when you upload an object by using append upload:

package main

import (
    "fmt"
    "os"
    "strings"

    "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
    // Create an OSSClient instance.
    client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Specify the name of the bucket to which to upload the object.
    bucket, err := client.Bucket("<yourBucketName>")
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // If the object is appended for the first time, the append position is 0. The returned value is the position for the next append. The position to start the next append is the total length of appended bytes and the append object.
    request := &oss.AppendObjectRequest{
        ObjectKey: "<yourObjectName>",
        Reader:    strings.NewReader("<YourObjectAppendValue1>"),
        Position:  0,
    }

    // If you append the object for the first time, set the CRC-64 value to 0.
    options := []oss.Option{oss.InitCRC(0)}
    result, err := bucket.DoAppendObject(request, options)
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // The position to start the next append is the total length included in the response for the previous append request.
    request = &oss.AppendObjectRequest{
        ObjectKey: "<yourObjectName>",
        Reader:    strings.NewReader("<YourObjectAppendValue2>"),
        Position:  result.NextPosition,
    }

    // The CRC-64 value in the next append request is the value included in the response for the previous append request.
    options = []oss.Option{oss.InitCRC(result.CRC)}
    result, err = bucket.DoAppendObject(request, options)
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    // You can perform multiple AppendObject operations.
}