O SDK do Object Storage Service (OSS) para iOS oferece verificações de integridade de dados para garantir a segurança durante uploads e downloads.
Verificações de integridade de dados em uploads
Devido à complexidade das redes móveis, podem ocorrer erros durante a transferência de dados entre o cliente e o servidor. O OSS fornece verificações de integridade de dados ponta a ponta com base nos algoritmos MD5 e CRC-64.
-
Verificação de MD5
Se você especificar o cabeçalho Content-MD5 na solicitação ao fazer upload de um objeto, o OSS executará a verificação de MD5 no objeto para garantir sua integridade. O upload do objeto só será concluído se o hash MD5 do objeto recebido corresponder ao valor Content-MD5 definido na solicitação.
OSSPutObjectRequest * put = [OSSPutObjectRequest new]; // Specify the name of the bucket. Example: examplebucket. put.bucketName = @"examplebucket"; // Specify the full path of the object. Example: exampledir/exampleobject.txt. Do not include the bucket name in the full path. put.objectKey = @"exampledir/exampleobject.txt"; put.uploadingFileURL = [NSURL fileURLWithPath:@"<filePath>"]; // put.uploadingData = <NSData *>; // Directly upload NSData. // Specify Content-MD5. put.contentMd5 = [OSSUtil base64Md5ForFilePath:@"<filePath>"]; OSSTask * putTask = [client putObject:put]; [putTask continueWithBlock:^id(OSSTask *task) { if (!task.error) { NSLog(@"upload object success!"); } else { NSLog(@"upload object failed, error: %@" , task.error); } return nil; }]; // waitUntilFinished blocks execution of the current thread but does not block the task progress. // [putTask waitUntilFinished]; -
Verificação de CRC
Você pode calcular o valor CRC de 64 bits de um objeto durante o upload. O código de exemplo a seguir demonstra como verificar o CRC ao fazer upload de um objeto:
OSSPutObjectRequest * put = [OSSPutObjectRequest new]; // Specify the name of the bucket. Example: examplebucket. put.bucketName = @"examplebucket"; // Specify the full path of the object. Example: exampledir/exampleobject.txt. Do not include the bucket name in the full path. put.objectKey = @"exampledir/exampleobject.txt"; put.uploadingFileURL = [NSURL fileURLWithPath:@"<filePath>"]; // put.uploadingData = <NSData *>; // Directly upload NSData. // Enable CRC verification. put.crcFlag = OSSRequestCRCOpen; OSSTask * putTask = [client putObject:put]; [putTask continueWithBlock:^id(OSSTask *task) { if (!task.error) { NSLog(@"upload object success!"); } else { NSLog(@"upload object failed, error: %@" , task.error); } return nil; }]; // waitUntilFinished blocks execution of the current thread but does not block the task progress. // [putTask waitUntilFinished];
Verificações de integridade de dados em downloads
O SDK do OSS para iOS oferece verificações de integridade de dados ponta a ponta baseadas em valores CRC de 64 bits durante os downloads.
Quando a verificação CRC está ativada, o OSS valida automaticamente a integridade dos dados após a leitura do fluxo.
O exemplo de código a seguir mostra como ativar a verificação CRC em downloads:
OSSGetObjectRequest * request = [OSSGetObjectRequest new];
request.bucketName = @"examplebucket";
request.objectKey = @"exampledir/exampleobject.txt";
// Specify the local path to which the object is downloaded.
request.downloadToFileURL = [NSURL fileURLWithPath:@"<filePath>"];
// Enable CRC verification.
request.crcFlag = OSSRequestCRCOpen;
OSSTask * task = [client getObject:request];
[task continueWithBlock:^id(OSSTask *task) {
if (!task.error) {
NSLog(@"download object success!");
} else {
NSLog(@"download object failed, error: %@" ,task.error);
}
return nil;
}];
// [task waitUntilFinished];
Caso o bloco onReceiveData esteja configurado, verifique manualmente se o valor CRC corresponde.
OSSGetObjectRequest * request = [OSSGetObjectRequest new];
request.bucketName = @"examplebucket";
request.objectKey = @"exampledir/exampleobject.txt";
// Specify the local path to which the object is downloaded.
request.downloadToFileURL = [NSURL fileURLWithPath:@"<filePath>"];
// Enable CRC verification.
request.crcFlag = OSSRequestCRCOpen;
__block uint64_t localCrc64 = 0;
NSMutableData *receivedData = [NSMutableData data];
request.onRecieveData = ^(NSData *data) {
if (data)
{
NSMutableData *mutableData = [data mutableCopy];
void *bytes = mutableData.mutableBytes;
localCrc64 = [OSSUtil crc64ecma:localCrc64 buffer:bytes length:data.length];
[receivedData appendData:data];
}
};
__block uint64_t remoteCrc64 = 0;
OSSTask * task = [client getObject:request];
[task continueWithBlock:^id(OSSTask *task) {
OSSGetObjectResult *result = task.result;
if (result.remoteCRC64ecma)
{
NSScanner *scanner = [NSScanner scannerWithString:result.remoteCRC64ecma];
[scanner scanUnsignedLongLong:&remoteCrc64];
if (remoteCrc64 == localCrc64)
{
NSLog(@"CRC-64 verification successful!");
}
else
{
NSLog(@"CRC-64 verification failed!!");
}
}
return nil;
}];
// waitUntilFinished blocks execution of the current thread but does not block the task progress.
// [task waitUntilFinished];