Updated on 2026-08-14 GMT+08:00

Using MD5 to Verify File Consistency

You can use MD5 to verify file consistency when calling the following APIs to upload objects:

Sample Code 1: Performing a Text-Based Upload

This example uses MD5 to verify data consistency during text upload.

// Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK before storing them in the configuration file or environment variables. In this example, the AK and SK are stored in environment variables. Before running the code in this example, configure environment variables AccessKeyID and SecretAccessKey.
// The front-end code does not have the process environment variable, so you need to use a module bundler like webpack to define the process variable.
// Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
const AK = process.env.AccessKeyID
const SK = process.env.SecretAccessKey
// (Optional) If you use a temporary AK and SK pair and a security token to access OBS, obtain them using environment variables.
const security_token= process.env.SecurityToken
// Set server to the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
const server = "https://obs.ap-southeast-1.myhuaweicloud.com"

// Create an ObsClient instance.
var obsClient = new ObsClient({
    access_key_id: AK,
    secret_access_key: SK,
    server: server
});

async function putContent() {
    try {
        var content = 'Hello OBS';
        const params = {
            Bucket: 'examplebucket',
            Key: 'objectname',
            Body: content,
            // Calculate and pass the Base64-encoded MD5 value.
            ContentMD5: "content_base64MD5"
        };
        // Upload a text and verify the consistency.
        const result = await obsClient.putObject(params);
        if (result.CommonMsg.Status <= 300) {
            console.log("Put object(%s)'s successful!", params.Key);
            console.log("RequestId: %s", result.CommonMsg.RequestId);
            return;
        };
        console.log("An ObsError was found, which means your request sent to OBS was rejected with an error response.");
        console.log("Status: %d", result.CommonMsg.Status);
        console.log("Code: %s", result.CommonMsg.Code);
        console.log("Message: %s", result.CommonMsg.Message);
        console.log("RequestId: %s", result.CommonMsg.RequestId);
    } catch (error) {
        console.log("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network.");
        console.log(error);
    };
};

putContent()

Sample Code 2: Performing a File-Based Upload

This example uses MD5 to verify data consistency during file upload.

// Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK before storing them in the configuration file or environment variables. In this example, the AK and SK are stored in environment variables. Before running the code in this example, configure environment variables AccessKeyID and SecretAccessKey.
// The front-end code does not have the process environment variable, so you need to use a module bundler like webpack to define the process variable.
// Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
const AK = process.env.AccessKeyID
const SK = process.env.SecretAccessKey
// (Optional) If you use a temporary AK and SK pair and a security token to access OBS, obtain them using environment variables.
const security_token= process.env.SecurityToken
// Set server to the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
const server = "https://obs.ap-southeast-1.myhuaweicloud.com"

// Create an ObsClient instance.
var obsClient = new ObsClient({
    access_key_id: AK,
    secret_access_key: SK,
    server: server
});

async function putFile() {
    try {
        const params = {
            Bucket: 'examplebucket',
            Key: 'objectname',
            SourceFile: document.getElementById('file').files[0],
            // Calculate and pass the Base64-encoded MD5 value.
            ContentMD5:'file_base64MD5'
        };
        // Upload a local file and verify the consistency.
        const result = await obsClient.putObject(params);
        if (result.CommonMsg.Status <= 300) {
            console.log("Put object(%s)'s successful!", params.Key);
            console.log("RequestId: %s", result.CommonMsg.RequestId);
            return;
        };
        console.log("An ObsError was found, which means your request sent to OBS was rejected with an error response.");
        console.log("Status: %d", result.CommonMsg.Status);
        console.log("Code: %s", result.CommonMsg.Code);
        console.log("Message: %s", result.CommonMsg.Message);
        console.log("RequestId: %s", result.CommonMsg.RequestId);
    } catch (error) {
        console.log("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network.");
        console.log(error);
    };
};

putFile()

Sample Code 3: Performing an Append Upload

This example uses MD5 to verify data consistency during append upload.

// Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK before storing them in the configuration file or environment variables. In this example, the AK and SK are stored in environment variables. Before running the code in this example, configure environment variables AccessKeyID and SecretAccessKey.
// The front-end code does not have the process environment variable, so you need to use a module bundler like webpack to define the process variable.
// Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
const AK = process.env.AccessKeyID
const SK = process.env.SecretAccessKey
// (Optional) If you use a temporary AK and SK pair and a security token to access OBS, obtain them using environment variables.
const security_token= process.env.SecurityToken
// Set server to the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
const server = "https://obs.ap-southeast-1.myhuaweicloud.com"

// Create an ObsClient instance.
var obsClient = new ObsClient({
    access_key_id: AK,
    secret_access_key: SK,
    server: server
});

async function appendObject() {
    try {
        const params = {
            Bucket: 'examplebucket',
            Key: 'objectname',
            Position: 0,
            Body: 'Hello OBS',
            // Calculate and pass the Base64-encoded MD5 value.
            ContentMD5:'content_base64MD5'
        };
        // Perform an append upload and verify the consistency.
        const result = await obsClient.appendObject(params);
        if (result.CommonMsg.Status <= 300) {
            console.log("Append object(%s)'s successful!", params.Key);
            console.log("RequestId: %s", result.CommonMsg.RequestId);
            return;
        };
        console.log("An ObsError was found, which means your request sent to OBS was rejected with an error response.");
        console.log("Status: %d", result.CommonMsg.Status);
        console.log("Code: %s", result.CommonMsg.Code);
        console.log("Message: %s", result.CommonMsg.Message);
        console.log("RequestId: %s", result.CommonMsg.RequestId);
    } catch (error) {
        console.log("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network.");
        console.log(error);
    };
};

appendObject()

Sample Code 4: Uploading a Part

This example uses MD5 to verify data consistency during part upload.

// Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK before storing them in the configuration file or environment variables. In this example, the AK and SK are stored in environment variables. Before running the code in this example, configure environment variables AccessKeyID and SecretAccessKey.
// The front-end code does not have the process environment variable, so you need to use a module bundler like webpack to define the process variable.
// Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
const AK = process.env.AccessKeyID
const SK = process.env.SecretAccessKey
// (Optional) If you use a temporary AK and SK pair and a security token to access OBS, obtain them using environment variables.
const security_token= process.env.SecurityToken
// Set server to the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
const server = "https://obs.ap-southeast-1.myhuaweicloud.com"

// Create an ObsClient instance.
var obsClient = new ObsClient({
    access_key_id: AK,
    secret_access_key: SK,
    server: server
});

const PartSize = 5 * 1024 * 1024;
const lastPartSize = file.size % PartSize;
// Number of parts
const count = Math.ceil(file.size / PartSize);

async function uploadPart(n) {
    try {
        const params = {
            Bucket: 'examplebucket',
            Key: 'exampleobject',
            // Set the part number, which ranges from 1 to 10,000.
            PartNumber: n,
            // Set the upload ID.
            UploadId: 'upload id from initiateMultipartUpload',
            // Specify the large file to be uploaded.
            SourceFile: document.getElementById('input-file').files[0],
            // Set the part size.
            PartSize: count === n ? lastPartSize : PartSize,
            // Set the start offset.
            Offset: (n-1) * PartSize,
            // Calculate and pass the Base64-encoded MD5 value.
            ContentMD5:'part_base64MD5'
        };
        // Perform a resumable upload and verify the consistency.
        const result = await obsClient.uploadPart(params);
        if (result.CommonMsg.Status <= 300) {
            console.log("Upload part(%s)'s successful!", n);
            console.log("RequestId: %s", result.CommonMsg.RequestId);
            return;
        };
        console.log("An ObsError was found, which means your request sent to OBS was rejected with an error response.");
        console.log("Status: %d", result.CommonMsg.Status);
        console.log("Code: %s", result.CommonMsg.Code);
        console.log("Message: %s", result.CommonMsg.Message);
        console.log("RequestId: %s", result.CommonMsg.RequestId);
    } catch (error) {
        console.log("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network.");
        console.log(error);
    };
};

uploadPart(1)

Sample Code 5: Performing a Resumable Upload

This example uses MD5 to verify the consistency of each part during resumable upload.

// Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK before storing them in the configuration file or environment variables. In this example, the AK and SK are stored in environment variables. Before running the code in this example, configure environment variables AccessKeyID and SecretAccessKey.
// The front-end code does not have the process environment variable, so you need to use a module bundler like webpack to define the process variable.
// Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
const AK = process.env.AccessKeyID
const SK = process.env.SecretAccessKey
// (Optional) If you use a temporary AK and SK pair and a security token to access OBS, obtain them using environment variables.
const security_token= process.env.SecurityToken
// Set server to the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
const server = "https://obs.ap-southeast-1.myhuaweicloud.com"

// Create an ObsClient instance.
var obsClient = new ObsClient({
    access_key_id: AK,
    secret_access_key: SK,
    server: server
});

async function uploadFile() {
    try {
        const params = {
            Bucket: 'examplebucket',
            Key: 'objectname',
            SourceFile: document.getElementById('file').files[0],
            PartSize: 9 * 1024 * 1024,
            // Specify whether to enable the consistency check for each part. If this function is enabled, the overall upload performance will be negatively affected and the browser will consume more CPU and memory resources.
            VerifyMd5: true, 
            TaskNum: 6
        };
        // Perform a resumable upload and verify the consistency.
        const result = await obsClient.uploadFile(params);
        if (result.CommonMsg.Status <= 300) {
            console.log("Upload file(%s)'s successful!", params.Key);
            console.log("RequestId: %s", result.CommonMsg.RequestId);
            return;
        };
        console.log("An ObsError was found, which means your request sent to OBS was rejected with an error response.");
        console.log("Status: %d", result.CommonMsg.Status);
        console.log("Code: %s", result.CommonMsg.Code);
        console.log("Message: %s", result.CommonMsg.Message);
        console.log("RequestId: %s", result.CommonMsg.RequestId);
    } catch (error) {
        console.log("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network.");
        console.log(error);
    };
};

uploadFile()