Object Storage Service (OSS), nesneleri sınıflandırmak için nesne etiketleri kullanmanıza olanak tanır. Bu konuda, bir nesneye etiket ekleme, nesne etiketlerini sorgulama ve nesne etiketlerini silme işlemleri açıklanmaktadır.
Kullanım notları
Bu konudaki örnek kodu çalıştırmadan önce, özel etki alanı adı veya Security Token Service (STS) kullanma gibi yöntemlerle bir OSSClient örneği oluşturmanız gerekir. Daha fazla bilgi için bkz. Başlatma.
Bir nesneye etiket ekleyin
Nesneyi yüklerken nesneye etiket ekleyin
Aşağıdaki örnek kod, bir nesneyi yüklerken nesneye nasıl etiket ekleneceğini gösterir:
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];
Mevcut bir nesneye etiket ekleyin veya mevcut bir nesnenin etiketlerini değiştirin
Aşağıdaki örnek kod, mevcut bir nesneye nasıl etiket ekleneceğini veya mevcut bir nesnenin etiketlerinin nasıl değiştirileceğini gösterir:
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];
Nesneyi kopyalarken nesneye etiket ekleyin
Aşağıdaki örnek kod, bir nesneyi kopyalarken nesneye nasıl etiket ekleneceğini gösterir:
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];
Sembolik bağlantıya etiket ekleyin
Aşağıdaki örnek kod, sembolik bir bağlantıya nasıl etiket ekleneceğini gösterir:
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];
Nesne etiketlerini sorgulayın
Aşağıdaki örnek kod, bir nesnenin etiketlerinin nasıl sorgulanacağını gösterir:
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];
Nesne etiketlerini silin
Aşağıdaki örnek kod, bir nesnenin etiketlerinin nasıl silineceğini gösterir:
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];
İlgili kaynaklar
Bir nesneye etiket eklemek için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. PutObjectTagging.
Bir nesnenin etiketlerini sorgulamak için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. GetObjectTagging.
Bir nesnenin etiketlerini silmek için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. DeleteObjectTagging.