O OSS SDK for Java usa verificação MD5 e CRC-64 para garantir a integridade dos dados durante uploads, downloads e cópias de objetos.
Observações
Este tópico usa o endpoint público da região China (Hangzhou). Para acessar o OSS a partir de outros serviços da Alibaba Cloud na mesma região, use um endpoint interno. Para obter mais informações sobre regiões e endpoints do OSS, consulte Regiões e endpoints.
Neste tópico, as credenciais de acesso são obtidas de variáveis de ambiente. Para saber como configurar credenciais de acesso, consulte Configurar credenciais de acesso (Python SDK V1).
Este tópico demonstra como criar uma instância OSSClient com um endpoint do OSS. Para configurações alternativas, como uso de domínio personalizado ou autenticação com credenciais do Security Token Service (STS), consulte Inicialização.
Validação MD5
Ao incluir Content-MD5 em uma solicitação de upload, o OSS compara o hash MD5 dos dados recebidos com o valor fornecido. Se não houver correspondência, o OSS retorna um erro InvalidDigest e você deve reenviar o objeto.
Configure a verificação MD5 em uma operação 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)
A validação MD5 é compatível com put_object, append_object, post_object e upload_part.
Validação CRC-64
Ao usar a verificação de redundância cíclica (CRC) para validar dados, observe os seguintes pontos:
A validação CRC-64 é compatível com put_object, get_object, append_object e upload_part. A validação CRC é ativada por padrão para uploads de arquivos. Se o valor CRC calculado pelo cliente não corresponder ao valor retornado pelo servidor, o sistema lançará uma exceção InconsistentError.
Downloads parciais (range downloads) não suportam validação CRC-64.
A validação CRC-64 consome recursos de CPU e pode afetar as velocidades de upload e download.
-
Validação CRC-64 para downloads
O código a seguir mostra como executar a validação de integridade de dados CRC-64 ao baixar um arquivo:
# -*- 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!") -
Validação CRC-64 para uploads de anexos
Em uploads de anexos, se você especificar o parâmetro init_crc, a validação CRC-64 será ativada por padrão.
# -*- 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)
Referências
Para obter o código de exemplo completo sobre validação de dados, consulte o exemplo no GitHub.