Todos los productos
Search
Centro de documentos

Object Storage Service:Etiquetado de objetos (SDK para iOS)

Última actualización: Sep 22, 2026

Object Storage Service (OSS) le permite configurar etiquetas en los objetos para clasificarlos. En este tema se describe cómo agregar etiquetas a un objeto, consultar las etiquetas de un objeto y eliminar las etiquetas de un objeto.

Notas de uso

Antes de ejecutar el código de ejemplo de este tema, debe crear una instancia de OSSClient mediante métodos como el uso de un nombre de dominio personalizado o Security Token Service (STS). Para más información, consulte Initialization.

Agregar etiquetas a un objeto

Agregar etiquetas a un objeto durante la carga

A continuación se muestra un ejemplo de código sobre cómo agregar etiquetas a un objeto al momento de cargarlo:

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];

Agregar o modificar etiquetas de un objeto existente

A continuación se muestra un ejemplo de código sobre cómo agregar etiquetas a un objeto existente o modificar sus etiquetas:

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];

Agregar etiquetas a un objeto durante la copia

A continuación se muestra un ejemplo de código sobre cómo agregar etiquetas a un objeto al copiarlo:

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];

Agregar etiquetas a un enlace simbólico

A continuación se muestra un ejemplo de código sobre cómo agregar etiquetas a un enlace 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 las etiquetas de un objeto

A continuación se muestra un ejemplo de código sobre cómo consultar las etiquetas de un 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];

Eliminar las etiquetas de un objeto

A continuación se muestra un ejemplo de código sobre cómo eliminar las etiquetas de un 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];

Referencias

  • Para más información sobre la API que permite agregar etiquetas a un objeto, consulte PutObjectTagging.

  • Para más información sobre la API que permite consultar las etiquetas de un objeto, consulte GetObjectTagging.

  • Para más información sobre la API que permite eliminar las etiquetas de un objeto, consulte DeleteObjectTagging.