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 from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, 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 Configure OSSClient instances.
MD5 validation
When you upload a file, you can specify the Content-MD5 header to ensure data integrity and accuracy. OSS calculates the MD5 hash of the data it receives. If the calculated hash does not match the hash that you provided, OSS returns an InvalidDigest exception. If an InvalidDigest exception occurs, check whether the file was changed or corrupted during transmission and upload the file again.
The putObject, getObject, appendObject, postObject, and uploadPart operations support MD5 validation.
The following code provides an example of how to perform MD5 validation during a file upload.
package main
import (
"crypto/md5"
"encoding/base64"
"log"
"strings"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
// Calculate the MD5 checksum for the given content.
func calculateMD5(content string) string {
h := md5.New()
h.Write([]byte(content))
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
func main() {
// Load OSS access credentials from environment variables.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
log.Fatalf("Failed to create credentials provider: %v", err)
}
// Replace with your actual bucket name.
bucketName := "yourBucketName"
// Create an OSS client.
// Set yourEndpoint to the Endpoint of your bucket. For example, for the China (Hangzhou) region, set it to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual Endpoint.
// Set yourRegion to the region where your bucket is located. For example, for the China (Hangzhou) region, set it to cn-hangzhou. For other regions, use the actual region ID.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
log.Fatalf("Failed to create OSS client: %v", err)
}
// Get the specified bucket.
bucket, err := client.Bucket(bucketName)
if err != nil {
log.Fatalf("Failed to get bucket %s: %v", bucketName, err)
}
// The content to upload.
content := "yourObjectValue" // Replace with the actual content to upload.
objectName := "yourObjectName" // Replace with the actual object name.
// Calculate the MD5 checksum of the content.
contentMD5 := calculateMD5(content)
// Upload the file to OSS.
err = bucket.PutObject(objectName, strings.NewReader(content), oss.ContentMD5(contentMD5))
if err != nil {
log.Fatalf("Failed to upload object %s: %v", objectName, err)
}
log.Printf("Object '%s' uploaded successfully.", objectName)
}
CRC-64 validation
Notes on CRC-64 validation:
The putObject, getObject, appendObject, and uploadPart operations support CRC-64 validation.
CRC data validation is enabled by default for file uploads, downloads, and copies. If the CRC value calculated by the client does not match the value returned by the server, an error is returned.
Range downloads do not support CRC-64 validation.
Enabling CRC-64 validation consumes some CPU resources and affects upload and download speeds.
The following code provides an example of how to perform CRC-64 data integrity validation when you append data to an object.
package main
import (
"log"
"strings"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before running this code, make sure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
log.Fatalf("Failed to create credentials provider: %v", err)
}
// Create an OSSClient instance.
// Set yourEndpoint to the Endpoint of your bucket. For example, for the China (Hangzhou) region, set it to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual Endpoint.
// Set yourRegion to the region where your bucket is located. For example, for the China (Hangzhou) region, set it to cn-hangzhou. For other regions, use the actual region ID.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
log.Fatalf("Failed to create OSS client: %v", err)
}
// Set yourBucketName to the name of your bucket.
bucketName := "yourBucketName" // Replace with your actual bucket name.
bucket, err := client.Bucket(bucketName)
if err != nil {
log.Fatalf("Failed to get bucket %s: %v", bucketName, err)
}
// Define the name of the object to which data will be appended.
objectName := "yourObjectName" // Replace with the actual object name.
// The position for the first append operation is 0. The return value indicates the position for the next append operation. Subsequent append positions are the length of the file before the append.
request := &oss.AppendObjectRequest{
ObjectKey: objectName,
Reader: strings.NewReader("YourObjectAppendValue1"),
Position: 0,
}
// For the first append operation, initialize the CRC value to 0.
options := []oss.Option{oss.InitCRC(0)}
result, err := bucket.DoAppendObject(request, options)
if err != nil {
log.Fatalf("Failed to append object %s for the first time: %v", objectName, err)
}
log.Printf("First append successful. Next position: %d", result.NextPosition)
// The position for the second append operation starts from the length returned by the first operation.
request = &oss.AppendObjectRequest{
ObjectKey: objectName,
Reader: strings.NewReader("YourObjectAppendValue2"),
Position: result.NextPosition,
}
// The initial CRC value for the second append operation is the return value from the first operation.
options = []oss.Option{oss.InitCRC(result.CRC)}
result, err = bucket.DoAppendObject(request, options)
if err != nil {
log.Fatalf("Failed to append object %s for the second time: %v", objectName, err)
}
log.Printf("Second append successful. Next position: %d", result.NextPosition)
// You can perform multiple AppendObject operations.
log.Printf("All appends completed successfully.")
}