Objective-C AWS S3: Get S3 Object Tags

Back to Index

Returns the tags associated with an object. This example gets the tags for the object named "starfish.jpg" located in the bucket "chilkat100".

Documentation: https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectGETtagging.html

CURL Command

curl -X GET https://chilkat100.s3.amazonaws.com/starfish.jpg?tagging

Objective-C Example

#import <CkoRest.h>
#import <CkoAuthAws.h>
#import <CkoStringBuilder.h>
#import <CkoXml.h>
#import <NSString.h>

CkoRest *rest = [[CkoRest alloc] init];
BOOL success;

CkoAuthAws *authAws = [[CkoAuthAws alloc] init];
authAws.AccessKey = @"AWS_ACCESS_KEY";
authAws.SecretKey = @"AWS_SECRET_KEY";
authAws.Region = @"us-east-1";
authAws.ServiceName = @"s3";
[rest SetAuthAws: authAws];

//  URL: https://chilkat100.s3.amazonaws.com/starfish.jpg?tagging
BOOL bTls = YES;
int port = 443;
BOOL bAutoReconnect = YES;
success = [rest Connect: @"chilkat100.s3.amazonaws.com" port: [NSNumber numberWithInt: port] tls: bTls autoReconnect: bAutoReconnect];
if (success != YES) {
    NSLog(@"%@%d",@"ConnectFailReason: ",[rest.ConnectFailReason intValue]);
    NSLog(@"%@",rest.LastErrorText);
    return;
}

CkoStringBuilder *sbResponseBody = [[CkoStringBuilder alloc] init];
success = [rest FullRequestNoBodySb: @"GET" uriPath: @"/starfish.jpg?tagging" sb: sbResponseBody];
if (success != YES) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

int respStatusCode = [rest.ResponseStatusCode intValue];
if (respStatusCode >= 400) {
    NSLog(@"%@%d",@"Response Status Code = ",respStatusCode);
    NSLog(@"%@",@"Response Header:");
    NSLog(@"%@",rest.ResponseHeader);
    NSLog(@"%@",@"Response Body:");
    NSLog(@"%@",[sbResponseBody GetAsString]);
    return;
}

CkoXml *xmlResponse = [[CkoXml alloc] init];
[xmlResponse LoadSb: sbResponseBody autoTrim: YES];

//  See the Online Tool for Generating XML Parse Code
NSString *Tagging_xmlns = 0;
int i;
int count_i;
NSString *tagPath = 0;
NSString *Key = 0;
NSString *Value = 0;

Tagging_xmlns = [xmlResponse GetAttrValue: @"xmlns"];
i = 0;
count_i = [[xmlResponse NumChildrenHavingTag: @"TagSet|Tag"] intValue];
while (i < count_i) {
    xmlResponse.I = [NSNumber numberWithInt: i];
    Key = [xmlResponse GetChildContent: @"TagSet|Tag[i]|Key"];
    Value = [xmlResponse GetChildContent: @"TagSet|Tag[i]|Value"];
    i = i + 1;
}

Sample XML Response Body

<?xml version="1.0" encoding="UTF-8" ?>
<Tagging xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <TagSet>
        <Tag>
            <Key>habitat</Key>
            <Value>ocean</Value>
        </Tag>
        <Tag>
            <Key>phylum</Key>
            <Value>Echinodermata</Value>
        </Tag>
        <Tag>
            <Key>animal</Key>
            <Value>starfish</Value>
        </Tag>
    </TagSet>
</Tagging>