Object Storage Service (OSS) lets you attach tags to objects to categorize and manage your storage. This topic shows how to add, query, and delete object tags using the OSS iOS SDK.
Prerequisites
Before you begin, ensure that you have an OSSClient instance initialized using a custom domain name or Security Token Service (STS). For details, see Initialization.
Add tags to an object
Add tags when uploading an object
Tags are attached at upload time via the x-oss-tagging header on an OSSPutObjectRequest. Use OSSUtil populateQueryStringFromParameter: to encode the tag dictionary into the required query-string format.
OSSPutObjectRequest *put = [OSSPutObjectRequest new];
// Specify the bucket name.
put.bucketName = @"examplebucket";
// Specify the full path of the object. Do not include the bucket name.
put.objectKey = @"exampledir/exampleobject.txt";
put.uploadingFileURL = [NSURL fileURLWithPath:@"<local-file-path>"];
// Attach tags using the x-oss-tagging header.
NSDictionary *tagging = @{@"owner": @"John"};
put.objectMeta = @{@"x-oss-tagging": [OSSUtil populateQueryStringFromParameter:tagging]};
// (Optional) Track upload progress.
put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
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 tags of an existing object
Use OSSPutObjectTaggingRequest to set or replace tags on an existing object.
OSSPutObjectTaggingRequest *putTaggingRequest = [OSSPutObjectTaggingRequest new];
// Specify the bucket name.
putTaggingRequest.bucketName = @"examplebucket";
// Specify the full path of the object. Do not include the bucket name.
putTaggingRequest.objectKey = @"exampledir/exampleobject.txt";
// Set tags as a key-value dictionary.
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 when copying an object
Set x-oss-tagging and x-oss-tagging-directive: Replace on an OSSCopyObjectRequest to apply new tags to the destination object, replacing any tags inherited from the source.
OSSCopyObjectRequest *copy = [OSSCopyObjectRequest new];
// Specify the source bucket and object.
copy.sourceBucketName = @"sourceBucketName";
copy.sourceObjectKey = @"sourceObjectKey";
// Specify the destination bucket and object.
copy.bucketName = @"bucketName";
copy.objectKey = @"objectKey";
// Apply new tags to the destination object, replacing any source tags.
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
Use OSSPutSymlinkRequest with the x-oss-tagging header to attach tags when creating a symbolic link.
OSSPutSymlinkRequest *putSymlinkRequest = [OSSPutSymlinkRequest new];
// Specify the bucket name.
putSymlinkRequest.bucketName = @"examplebucket";
// Specify the symbolic link name.
putSymlinkRequest.objectKey = @"examplesymlink";
// Specify the target object's full path. Do not include the bucket name.
putSymlinkRequest.targetObjectName = @"exampleobject.txt";
// Attach tags to the symbolic link.
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
OSSGetObjectTaggingRequest returns the tags as an NSDictionary in OSSGetObjectTaggingResult.tags. Iterate over the dictionary to read each key-value pair.
OSSGetObjectTaggingRequest *getTaggingRequest = [OSSGetObjectTaggingRequest new];
// Specify the bucket name.
getTaggingRequest.bucketName = @"examplebucket";
// Specify the full path of the object. Do not include the bucket name.
getTaggingRequest.objectKey = @"exampledir/exampleobject.txt";
OSSTask *getTask = [client getObjectTagging:getTaggingRequest];
[getTask continueWithBlock:^id _Nullable(OSSTask * _Nonnull task) {
if (!task.error) {
OSSGetObjectTaggingResult *result = task.result;
for (NSString *key in [result.tags allKeys]) {
NSLog(@"tag %@: %@", key, result.tags[key]);
}
} else {
NSLog(@"get object tagging failed, error: %@", task.error);
}
return nil;
}];
// [getTask waitUntilFinished];Delete object tags
Use OSSDeleteObjectTaggingRequest to remove tags from an object.
OSSDeleteObjectTaggingRequest *deleteTaggingRequest = [OSSDeleteObjectTaggingRequest new];
// Specify the bucket name.
deleteTaggingRequest.bucketName = @"examplebucket";
// Specify the full path of the object. Do not include the bucket name.
deleteTaggingRequest.objectKey = @"exampledir/exampleobject.txt";
OSSTask *deleteTask = [client deleteObjectTagging:deleteTaggingRequest];
[deleteTask continueWithBlock:^id _Nullable(OSSTask * _Nonnull task) {
if (!task.error) {
NSLog(@"delete object tagging success");
} else {
NSLog(@"delete object tagging failed, error: %@", task.error);
}
return nil;
}];
// [deleteTask waitUntilFinished];What's next
For the underlying API operations, see PutObjectTagging, GetObjectTagging, and DeleteObjectTagging.