Object Storage Service (OSS) vous permet de configurer des balises pour classer vos objets. Cette rubrique explique comment ajouter, interroger et supprimer les balises d'un objet.
Notes d'utilisation
Avant d'exécuter l'exemple de code de cette rubrique, créez une instance OSSClient, par exemple en utilisant un nom de domaine personnalisé ou le Security Token Service (STS). Pour plus d'informations, consultez Initialisation.
Ajouter des balises à un objet
Ajouter des balises à un objet lors du chargement
L'exemple de code suivant montre comment ajouter des balises à un objet lors de son chargement :
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];
Ajouter ou modifier les balises d'un objet existant
L'exemple de code suivant montre comment ajouter des balises à un objet existant ou modifier ses balises :
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];
Ajouter des balises à un objet lors de sa copie
L'exemple de code suivant montre comment ajouter des balises à un objet lors de sa copie :
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];
Ajouter des balises à un lien symbolique
L'exemple de code suivant montre comment ajouter des balises à un lien symbolique :
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];
Interroger les balises d'un objet
L'exemple de code suivant montre comment interroger les balises d'un objet :
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];
Supprimer les balises d'un objet
L'exemple de code suivant montre comment supprimer les balises d'un objet :
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];
Références
Pour plus d'informations sur l'opération API permettant d'ajouter des balises à un objet, consultez PutObjectTagging.
Pour plus d'informations sur l'opération API permettant d'interroger les balises d'un objet, consultez GetObjectTagging.
Pour plus d'informations sur l'opération API permettant de supprimer les balises d'un objet, consultez DeleteObjectTagging.