El SDK de OSS para Java utiliza la verificación MD5 y CRC-64 para garantizar la integridad de los datos durante las cargas, descargas y copias de objetos.
Notas
En este tema se utiliza el endpoint público de la región China (Hangzhou). Si necesita acceder a OSS desde otros servicios de Alibaba Cloud en la misma región que OSS, utilice un endpoint interno. Para obtener más información sobre las regiones y los endpoints de OSS, consulte Regions and endpoints.
En este tema, las credenciales de acceso se obtienen de variables de entorno. Para obtener más información sobre cómo configurar las credenciales de acceso, consulte Configure access credentials using OSS SDK for Python 1.0.
Este tema muestra cómo crear una instancia de OSSClient con un endpoint de OSS. Para conocer configuraciones alternativas, como usar un dominio personalizado o autenticarse con credenciales de Security Token Service (STS), consulte Initialization.
Validación MD5
Cuando incluye Content-MD5 en una solicitud de carga, OSS compara el hash MD5 de los datos recibidos con el valor proporcionado. Si no coinciden, OSS devuelve un error InvalidDigest y debe volver a cargar el objeto.
Configure la verificación MD5 en una operación PutObject:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the Endpoint for the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region that corresponds to the Endpoint, for example, cn-hangzhou. Note that this parameter is required for v4 signatures.
region = "cn-hangzhou"
# Replace examplebucket with the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# Specify the full path of the object. The full path cannot include the bucket name. For example, exampledir/exampleobject.txt.
object_name = 'exampledir/exampleobject.txt'
# Specify the local path of the file to upload. The value of this variable is transferred to OSS as the content to upload. The file can be of any type, such as text, image, video, or audio.
with open('/Users/test/Desktop/demo.txt', 'rb') as file:
content = file.read()
# Calculate the MD5 hash of the content to be uploaded.
content_md5 = oss2.utils.content_md5(content)
print('content_md5', content_md5)
# Include the 'Content-MD5' header in the upload request. The server verifies the MD5 hash of the uploaded content to ensure its integrity and correctness.
headers = dict()
headers['Content-MD5'] = content_md5
bucket.put_object(object_name, content, headers=headers)
La validación MD5 es compatible con put_object, append_object, post_object y upload_part.
Validación CRC-64
Cuando utilice la comprobación de redundancia cíclica (CRC) para validar datos, tenga en cuenta lo siguiente:
La validación CRC-64 es compatible con put_object, get_object, append_object y upload_part. La validación CRC está habilitada de forma predeterminada para las cargas de archivos. Si el valor CRC calculado por el cliente no coincide con el valor CRC devuelto por el servidor, se lanza una excepción InconsistentError.
Las descargas por rangos no admiten la validación CRC-64.
La validación CRC-64 consume recursos de CPU y puede afectar la velocidad de carga y descarga.
-
Validación CRC-64 para descargas
A continuación se muestra cómo validar la integridad de los datos con CRC-64 al descargar un archivo:
# -*- coding: utf-8 -*- import oss2 from oss2.credentials import EnvironmentVariableCredentialsProvider # Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider()) # Specify the Endpoint for the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. endpoint = "https://oss-cn-hangzhou.aliyuncs.com" # Specify the region that corresponds to the Endpoint, for example, cn-hangzhou. Note that this parameter is required for v4 signatures. region = "cn-hangzhou" # Replace examplebucket with the name of your bucket. bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region) # Specify the full path of the object. The full path cannot include the bucket name. object_name = 'yourObjectName' # Check whether CRC validation is enabled by default. print('bucket.enable-crc:', bucket.enable_crc) # The return value of bucket.get_object is a file-like object and is also iterable. object_stream = bucket.get_object(object_name) print(object_stream.read()) # Because the get_object operation returns a stream, you must call read() before you can calculate the CRC checksum of the returned object data. Therefore, perform CRC validation after you call this operation. if object_stream.client_crc != object_stream.server_crc: print("The CRC checksum between client and server is inconsistent!") -
Validación CRC-64 para cargas por anexión
En las cargas por anexión, si especifica el parámetro init_crc, la validación CRC-64 se habilita de forma predeterminada.
# -*- coding: utf-8 -*- import oss2 from oss2.credentials import EnvironmentVariableCredentialsProvider # Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider()) # Specify the Endpoint for the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. endpoint = "https://oss-cn-hangzhou.aliyuncs.com" # Specify the region that corresponds to the Endpoint, for example, cn-hangzhou. Note that this parameter is required for v4 signatures. region = "cn-hangzhou" # Replace examplebucket with the name of your bucket. bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region) object_name = "yourAppendObjectName" first_content = "yourFirstContent" second_content = "yourSecondContent" # First append upload. # If init_crc is specified, the SDK performs CRC validation on the returned result by default. result = bucket.append_object(object_name, 0, first_content, init_crc=0) # Second append upload. # Set init_crc to the CRC value of the uploaded data. result = bucket.append_object(object_name, result.next_position, second_content, init_crc=result.crc)
Referencias
Para consultar el código de ejemplo completo sobre la validación de datos, consulte el ejemplo de GitHub.