The OSS SDK for C++ 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 inconsistent with that configured in the uploaded request, InvalidDigest is returned. This allows OSS to ensure data integrity in the uploaded object.

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

#include <alibabacloud/oss/OssClient.h>
#include "../src/utils/Utils.h"
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* Initialize the OSS account information. */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
    std::string ObjectName = "yourObjectName";

    /* Initialize network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    std::shared_ptr<std::iostream> content = std::make_shared<std::stringstream>();
    *content << "test cpp sdk";
    PutObjectRequest request(BucketName, ObjectName, content);

    /*Configure MD5 verification. */
    std::string contentMd5 = ComputeContentMD5(*content);
    request.setContentMd5(contentMd5);

    /* Upload the object. */
    auto outcome = client.PutObject(request);

    if (! outcome.isSuccess()) {
        /*Handle exceptions.*/
        std::cout << "PutObject fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        ShutdownSdk();
        return -1;
    }

    /* Release network resources. */
    ShutdownSdk();
    return 0;
}
Note MD5 verification can be used for putObject, getObject, appendObject, postObject, and uploadPart operations.

CRC-64

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

Note
  • 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 in object downloads:

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* Initialize the OSS account information. */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
    std::string ObjectName = "yourObjectName";

    /* Initialize network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

    /*Download an object to local memory.*/ */
    GetObjectRequest request(BucketName, ObjectName);
    /* The OSS SDK for C++ enables CRC-64 by default. */
    /* If CRC-64 fails, a Crc64 validation failed error is returned. */    
    auto outcome = client.GetObject(request);
    if (outcome.isSuccess()) {    
        std::cout << "getObjectToBuffer" << "server crc:" << outcome.result().Metadata().HttpMetaData().at("x-oss-hash-crc64ecma-by-client") << std::endl; 
    }
    else {
        /* Handle exceptions.*/
        std::cout << "getObjectToBuffer fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        ShutdownSdk();
        return -1;
    }

    /* Release network resources. */
    ShutdownSdk();
    return 0;
}