Todos os produtos
Search
Central de documentação

Object Storage Service:Object tagging (iOS SDK)

Última atualização: Sep 22, 2026

O Object Storage Service (OSS) permite configurar tags para classificar objetos. Este tópico descreve como adicionar, consultar e excluir tags 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 a utilização de um nome de domínio personalizado ou o Security Token Service (STS). Para mais informações, consulte Initialization.

Adicionar tags a um objeto

Adicionar tags a um objeto durante o upload

O código de exemplo a seguir mostra como adicionar tags a um objeto durante o upload:

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>"];
// Add tags. For example, set the key of the tag to owner and the value of the tag to John. 
NSDictionary *tagging = @{@"owner": @"John"};
put.objectMeta = @{@"x-oss-tagging": [OSSUtil populateQueryStringFromParameter:tagging]};

// (Optional) Configure an upload progress indicator. 
put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
    // Specify the number of bytes that are being uploaded, the total number of bytes that are uploaded, and the total number of bytes that you want to upload. 
    NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
};
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;
}];
// [putTask waitUntilFinished];
// [put cancel];

Adicionar ou modificar tags de um objeto existente

O código de exemplo a seguir mostra como adicionar ou modificar as tags de um objeto existente:

OSSPutObjectTaggingRequest *putTaggingRequest = [OSSPutObjectTaggingRequest new];
// Specify the name of the bucket. Example: examplebucket. 
putTaggingRequest.bucketName = @"examplebucket";
// Specify the full path of the object. Example: exampledir/exampleobject.txt. Do not include the bucket name in the full path. 
putTaggingRequest.objectKey = @"exampledir/exampleobject.txt";
// Create and add tags. 
NSDictionary *tags = @{@"key1":@"value1", @"key2":@"value2"};
putTaggingRequest.tags = tags;
OSSTask * putTask= [client putObjectTagging:putTaggingRequest];
[putTask continueWithBlock:^id _Nullable(OSSTask * _Nonnull task) {
    if (task.error) {
        NSLog(@"error: %@", task.error);
    } else {
        NSLog(@"put tagging success");
    }
    return nil;
}];
// [putTask waitUntilFinished];

Adicionar tags a um objeto durante a cópia

O código de exemplo a seguir mostra como adicionar tags a um objeto durante a cópia:

OSSCopyObjectRequest * copy = [OSSCopyObjectRequest new];
// Specify the name of the source bucket. 
copy.sourceBucketName = @"sourceBucketName";
// Specify the key of the source object. 
copy.sourceObjectKey = @"sourceObjectKey";
// Specify the name of the destination bucket. 
copy.bucketName = @"bucketName";
// Specify the name of the destination object. 
copy.objectKey = @"objectKey";
// Add tags to the object. 
NSDictionary *tagging = @{@"owner": @"John"};
copy.objectMeta = @{
    @"x-oss-tagging": [OSSUtil populateQueryStringFromParameter:tagging],
    @"x-oss-tagging-directive": @"Replace"
};

OSSTask * task = [client copyObject:copy];
[task continueWithBlock:^id(OSSTask *task) {
    if (task.error) {
        NSLog(@"error: %@", task.error);
    } else {
        NSLog(@"copy object success");
    }
    return nil;
}];
// [task waitUntilFinished];

Adicionar tags a um link simbólico

O código de exemplo a seguir mostra como adicionar tags a um link simbólico:

OSSPutSymlinkRequest * putSymlinkRequest = [OSSPutSymlinkRequest new];
// Specify the name of the bucket. 
putSymlinkRequest.bucketName = @"examplebucket";
// Specify the name of the symbolic link. 
putSymlinkRequest.objectKey = @"examplesymlink";
// Specify the full path of the object to which the symbolic link points. Do not include the bucket name in the full path. 
putSymlinkRequest.targetObjectName = @"exampleobject.txt";
// Add tags to the object. 
NSDictionary *tagging = @{@"owner": @"John"};
putSymlinkRequest.objectMeta = @{
    @"x-oss-tagging": [OSSUtil populateQueryStringFromParameter:tagging],
};

OSSTask * putSymlinktask = [client putSymlink:putSymlinkRequest];
[putSymlinktask continueWithBlock:^id(OSSTask *task) {
    if (!task.error) {
        NSLog(@"put symlink success");
    } else {
        NSLog(@"put symlink failed, error: %@", task.error);
    }
    return nil;
}];
// [putSymlinktask waitUntilFinished];

Consultar tags de um objeto

O código de exemplo a seguir mostra como consultar as tags de um objeto:

OSSGetObjectTaggingRequest *getTaggingRequest = [OSSGetObjectTaggingRequest new];
// Specify the name of the bucket. 
getTaggingRequest.bucketName = @"examplebucket";
// Specify the name of the object. 
getTaggingRequest.objectKey = @"exampledir/exampleobject.txt";
OSSTask * getTask = [client getObjectTagging:getTaggingRequest];
// Query the tags of the object. 
[getTask continueWithBlock:^id _Nullable(OSSTask * _Nonnull task) {
    if (!task.error) {
        OSSGetObjectTaggingResult *result = task.result;
        for (NSString *key in [result.tags allKeys]) {
            NSLog(@"tagging %@: %@", key, result.tags[key]);
        }
    } else {
        NSLog(@"get object tagging fail!  error: %@", task.error);
    }

    return nil;
}];
// [getTask waitUntilFinished];

Excluir tags de um objeto

O código de exemplo a seguir mostra como excluir as tags de um objeto:

OSSDeleteObjectTaggingRequest *deleteTaggingRequest = [OSSDeleteObjectTaggingRequest new];
// Specify the name of the bucket. 
deleteTaggingRequest.bucketName = @"examplebucket";
// Specify the name of the object. 
deleteTaggingRequest.objectKey = @"exampledir/exampleobject.txt";
OSSTask *deleteTask = [client deleteObjectTagging:deleteTaggingRequest];
// Delete the tags added to the object. 
[deleteTask continueWithBlock:^id _Nullable(OSSTask * _Nonnull task) {
    if (!task.error) {
        NSLog(@"delete object tagging success");
    } else {
        NSLog(@"delete object tagging fail!  error: %@", task.error);
    }
    return nil;
}];
// [deleteTask waitUntilFinished];

Referências

  • Para mais informações sobre a operação de API para adicionar tags a um objeto, consulte PutObjectTagging.

  • Para obter detalhes sobre a operação de API de consulta das tags de um objeto, consulte GetObjectTagging.

  • Para mais informações sobre a operação de API para excluir as tags de um objeto, acesse DeleteObjectTagging.