O Object Storage Service (OSS) oferece as seguintes classes de armazenamento para atender a diversos cenários, desde dados quentes até dados frios: Standard, Infrequent Access (IA), Archive, Cold Archive e Deep Cold Archive. Este tópico descreve como converter a classe de armazenamento de um objeto.
Observações de uso
Antes de executar o código de exemplo deste tópico, crie uma instância do OSSClient usando métodos como nome de domínio personalizado ou Security Token Service (STS). Para mais informações, consulte Inicialização.
Exemplos
-
Converter a classe de armazenamento de um objeto de Standard ou IA para Archive
O código de exemplo a seguir demonstra como converter a classe de armazenamento de um objeto chamado exampleobject.txt de Standard ou IA para Archive. Neste exemplo, o objeto está armazenado no diretório raiz de um bucket chamado examplebucket.
OSSCopyObjectRequest * copy = [OSSCopyObjectRequest new]; copy.sourceBucketName = @"examplebucket"; copy.sourceObjectKey = @"exampleobject.txt"; copy.bucketName = @"examplebucket"; copy.objectKey = @"exampleobject.txt"; // Set the storage class of the exampleobject.txt object to Archive. copy.objectMeta = @{@"x-oss-storage-class" : @"Archive"}; OSSTask * task = [client copyObject:copy]; [task continueWithBlock:^id(OSSTask *task) { if (!task.error) { NSLog(@"copy object success!"); } else { NSLog(@"copy object failed, error: %@" , task.error); } return nil; }]; // Implement synchronous blocking to wait for the task to complete. // [task waitUntilFinished]; -
Converter a classe de armazenamento de um objeto de Archive, Cold Archive ou Deep Cold Archive para IA
O código de exemplo a seguir ilustra como converter a classe de armazenamento de um objeto chamado exampleobject.txt de Archive, Cold Archive ou Deep Cold Archive para IA. Neste cenário, o objeto reside no diretório raiz de um bucket denominado examplebucket.
NSString *bucketName = @"examplebucket"; NSString *objectKey = @"exampleobject.txt"; // The following lines check whether the storage class of the object is Archive. If the storage class of the object is Archive, you must restore the object before you can convert the storage class of the object. // To check whether the storage class of the object is Cold Archive or Deep Cold Archive, replace isArchived with isColdArchived or isDeepColdArchived. __block bool isArchived = false; OSSHeadObjectRequest *headRq = [OSSHeadObjectRequest new]; headRq.bucketName = bucketName; headRq.objectKey = objectKey; [[[client headObject:headRq] continueWithBlock:^id _Nullable(OSSTask * _Nonnull task) { if (!task.error) { OSSHeadObjectResult *headRs = task.result; isArchived = [headRs.httpResponseHeaderFields[@"x-oss-storage-class"] isEqualToString:@"Archive"]; } else { NSLog(@"head object fail! error: %@", task.error); } return nil; }] waitUntilFinished]; if (isArchived) { // Restore the object. OSSRestoreObjectRequest *restoreRequest = [OSSRestoreObjectRequest new]; restoreRequest.bucketName = bucketName; restoreRequest.objectKey = objectKey; [[[client restoreObject:restoreRequest] continueWithBlock:^id _Nullable(OSSTask * _Nonnull task) { if (!task.error) { NSLog(@"restore object success!"); } else { NSLog(@"restore object fail! error: %@", task.error); } return nil; }] waitUntilFinished]; // Wait until the object is restored. __block bool isRestored = false; do { sleep(1); headRq = [OSSHeadObjectRequest new]; headRq.bucketName = bucketName; headRq.objectKey = objectKey; [[[client headObject:headRq] continueWithBlock:^id _Nullable(OSSTask * _Nonnull task) { if (!task.error) { OSSHeadObjectResult *headRs = task.result; isRestored = (headRs.httpResponseHeaderFields[@"x-oss-restore"] != nil && ![headRs.httpResponseHeaderFields[@"x-oss-restore"] isEqualToString:@"ongoing-request=\"true\""]); } else { NSLog(@"head object fail! error: %@", task.error); } return nil; }] waitUntilFinished]; } while (!isRestored); } // Set the storage class of the exampleobject.txt object to IA. OSSCopyObjectRequest *copy = [OSSCopyObjectRequest new]; copy.bucketName = bucketName; copy.objectKey = objectKey; copy.sourceBucketName = bucketName; copy.sourceObjectKey = objectKey; copy.objectMeta = @{@"x-oss-storage-class": @"IA"}; [[[client copyObject:copy] continueWithBlock:^id(OSSTask *task) { if (!task.error) { NSLog(@"copy object success!"); } else { NSLog(@"copy object failed, error: %@" , task.error); } return nil; }] waitUntilFinished];
Referências
Para mais detalhes sobre a operação de API utilizada na conversão da classe de armazenamento de um objeto, consulte CopyObject.
Saiba mais sobre como inicializar uma instância do OSSClient em Inicialização.