Object Storage Service (OSS) allows you to configure object tags to classify objects. This topic describes how to add tags to an object, query the tags of an object, and delete the tags of an object.
Usage notes
Before you run the sample code in this topic, you must create an OSSClient instance by using methods such as using a custom domain name or Security Token Service (STS). For more information, see Initialization.
Add tags to an object
Add tags to an object when you upload the object
The following sample code provides an example on how to add tags to an object when you upload the object:
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];
Add tags to or modify the tags of an existing object
The following sample code provides an example on how to add tags to or modify the tags of an existing object:
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];
Add tags to an object when you copy the object
The following sample code provides an example on how to add tags to an object when you copy the object:
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];
Add tags to a symbolic link
The following sample code provides an example on how to add tags to a symbolic link:
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];
Query object tags
The following sample code provides an example on how to query the tags of an object:
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];
Delete object tags
The following sample code provides an example on how to delete the tags of an object:
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];
References
For more information about the API operation that you can call to add tags to an object, see PutObjectTagging.
For more information about the API operation that you can call to query the tags of an object, see GetObjectTagging.
For more information about the API operation that you can call to delete the tags of an object, see DeleteObjectTagging.