Object Storage Service (OSS) SDK for Java uses MD5 verification and CRC-64 to ensure data integrity when you upload, download, and copy objects.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see Regions and endpoints.
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.
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 uploads. If InvalidDigest is returned, you need to upload the object again.
The following sample code provides an example on how to configure MD5 verification in a PutObject operation:
package main
import (
"crypto/md5"
"encoding/base64"
"fmt"
"os"
"strings"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Specify the name of the bucket.
bucket, err := client.Bucket("yourBucketName")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Query 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))
// Perform simple upload.
err = bucket.PutObject("yourObjectName", strings.NewReader(content), oss.ContentMD5(strMd5))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
}
MD5 verification can be used for putObject, getObject, appendObject, postObject, and uploadPart.
CRC-64 verification
CRC-64 verification is enabled by default in object upload, download, and copy operations to check data integrity.
Take note of the following items if you use CRC-64 to verify data integrity:
CRC-64 can be used in the PutObject, GetObject, AppendObject, and UploadPart operations. By default, CRC-64 is enabled when you upload an object. If the CRC-64 value calculated on the client is different from the CRC-64 value returned by the OSS server, an error is returned.
CRC-64 is not supported in range download.
CRC-64 consumes CPU resources and slows down uploads and downloads.
The following sample 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() {
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Specify the name of the bucket.
bucket, err := client.Bucket("yourBucketName")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// The position for the first append upload is 0, and the position for the next append upload is included in the response. The position from which the next append operation starts is the current length of the object.
// Specify the full path of the object. Do not include the bucket name in the full path.
request := &oss.AppendObjectRequest{
ObjectKey: "yourObjectName",
Reader: strings.NewReader("YourObjectAppendValue1"),
Position: 0,
}
// If you append the object for the first time, set the CRC 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 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 on an object.
}