Tous les produits
Search
Centre de documentation

Object Storage Service:Check data integrity by using CRC-64

Dernière mise à jour :Aug 18, 2026

Object Storage Service (OSS) renvoie une valeur CRC-64 pour chaque objet chargé. Comparez cette valeur avec un CRC-64 calculé localement afin de vérifier l'intégrité de la transmission des données.

Fonctionnement

OSS calcule une valeur CRC-64 pour chaque objet chargé selon la norme ECMA-182, la stocke dans les métadonnées de l'objet et la renvoie dans l'en-tête de réponse x-oss-hash-crc64ecma.

Les objets chargés avant la prise en charge de CRC-64 ne disposent pas de valeur CRC-64. Les requêtes adressées à ces objets ne renvoient aucune valeur CRC-64.

Notes d'utilisation

  • Les opérations PutObject, AppendObject, PostObject et MultipartUploadPart renvoient une valeur CRC-64. Une fois le chargement terminé, comparez la valeur CRC-64 renvoyée par le serveur avec celle calculée localement.

  • L'opération MultipartComplete renvoie la valeur CRC-64 de l'objet entier uniquement si chaque partie possède une valeur CRC-64. Par exemple, si une partie a été chargée avant la prise en charge de CRC-64, aucune valeur CRC-64 n'est renvoyée pour cette partie ni pour l'objet complet.

  • Les opérations GetObject, HeadObject et GetObjectMeta renvoient la valeur CRC-64 de l'objet si elle existe. Après un appel GetObject, comparez la valeur CRC-64 renvoyée par le serveur avec celle calculée localement.

    Remarque

    Une requête GET comportant un en-tête Range renvoie toujours la valeur CRC-64 de l'objet entier.

  • Les objets créés par des opérations de copie, telles que CopyObject et UploadPartCopy, peuvent ne pas disposer de valeur CRC-64.

Exemples

L'exemple Python suivant montre comment vérifier l'intégrité des données à l'aide de CRC-64.

import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
import crcmod
import random
import string
from oss2.models import PartInfo

# 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. 
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# 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 the name of the bucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'yourBucketName')
# Create a CRC-64 verification function. 
do_crc64 = crcmod.mkCrcFun(0x142F0E1EBA9EA3693, initCrc=0, xorOut=0xffffffffffffffff, rev=True)

# Verify the CRC-64 values and display the result. 
def check_crc64(local_crc64, oss_crc64, msg="check crc64"):
    if local_crc64 != oss_crc64:
        print("{0} check crc64 failed. local:{1}, oss:{2}.".format(msg, local_crc64, oss_crc64))
        return False
    else:
        print("{0} check crc64 ok.".format(msg))
        return True

# Generate a random string of the specified length. 
def random_string(length):
    return ''.join(random.choice(string.ascii_lowercase) for i in range(length))

# Generate a random string with a length of 1024. 
content = random_string(1024)

# Specify the path of the object. 
key = 'normal-key'

# Verify data integrity for a PutObject operation. 
result = bucket.put_object(key, content)
oss_crc64 = result.headers.get('x-oss-hash-crc64ecma', '')
local_crc64 = str(do_crc64(oss2.to_bytes(content)))
check_crc64(local_crc64, oss_crc64, "put object")

# Verify data integrity for a GetObject operation. 
result = bucket.get_object(key)
oss_crc64 = result.headers.get('x-oss-hash-crc64ecma', '')
local_crc64 = str(do_crc64(result.resp.read()))
check_crc64(local_crc64, oss_crc64, "get object")

# Verify data integrity for parts and the final object. 
part_info_list = []
key = "multipart-key"
result = bucket.init_multipart_upload(key)
upload_id = result.upload_id
part_1 = random_string(1024 * 1024)
result = bucket.upload_part(key, upload_id, 1, part_1)
oss_crc64 = result.headers.get('x-oss-hash-crc64ecma', '')
local_crc64 = str(do_crc64(oss2.to_bytes(part_1)))
# Check the integrity of part 1. 
check_crc64(local_crc64, oss_crc64, "upload_part object 1")
part_info_list.append(PartInfo(1, result.etag, len(part_1)))
part_2 = random_string(1024 * 1024)
result = bucket.upload_part(key, upload_id, 2, part_2)
oss_crc64 = result.headers.get('x-oss-hash-crc64ecma', '')
local_crc64 = str(do_crc64(oss2.to_bytes(part_2)))
# Check the integrity of part 2. 
check_crc64(local_crc64, oss_crc64, "upload_part object 2")
part_info_list.append(PartInfo(2, result.etag, len(part_2)))
result = bucket.complete_multipart_upload(key, upload_id, part_info_list)
oss_crc64 = result.headers.get('x-oss-hash-crc64ecma', '')
local_crc64 = str(do_crc64(oss2.to_bytes(part_2), do_crc64(oss2.to_bytes(part_1))))
# Check whether the final object uploaded to OSS is identical to the local file. 
check_crc64(local_crc64, oss_crc64, "complete object")

SDK OSS pris en charge

Le tableau suivant répertorie la prise en charge de CRC-64 par les SDK OSS.

SDK

Prise en charge de CRC

Exemple

OSS SDK for Java

Oui

CRCSample.java

OSS SDK for Python

Oui

object_check.py

OSS SDK for PHP

Non

Aucun

OSS SDK for C#

Non

Aucun

OSS SDK for C

Oui

oss_crc_sample.c

OSS SDK for JavaScript

Non

Aucun

OSS SDK for Go

Oui

crc_test.go

OSS SDK for Ruby

Non

Aucun

OSS SDK for iOS

Oui

OSSCrc64Tests.m

OSS SDK for Android

Oui

CRC64Test.java