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

C# Example

Chilkat.Rest rest = new Chilkat.Rest();
bool success;

Chilkat.AuthAws authAws = new Chilkat.AuthAws();
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 = true;
int port = 443;
bool bAutoReconnect = true;
success = rest.Connect("chilkat100.s3.amazonaws.com",port,bTls,bAutoReconnect);
if (success != true) {
    Debug.WriteLine("ConnectFailReason: " + Convert.ToString(rest.ConnectFailReason));
    Debug.WriteLine(rest.LastErrorText);
    return;
}

Chilkat.StringBuilder sbResponseBody = new Chilkat.StringBuilder();
success = rest.FullRequestNoBodySb("GET","/starfish.jpg?tagging",sbResponseBody);
if (success != true) {
    Debug.WriteLine(rest.LastErrorText);
    return;
}

int respStatusCode = rest.ResponseStatusCode;
if (respStatusCode >= 400) {
    Debug.WriteLine("Response Status Code = " + Convert.ToString(respStatusCode));
    Debug.WriteLine("Response Header:");
    Debug.WriteLine(rest.ResponseHeader);
    Debug.WriteLine("Response Body:");
    Debug.WriteLine(sbResponseBody.GetAsString());
    return;
}

Chilkat.Xml xmlResponse = new Chilkat.Xml();
xmlResponse.LoadSb(sbResponseBody,true);

//  See the Online Tool for Generating XML Parse Code
string Tagging_xmlns;
int i;
int count_i;
string tagPath;
string Key;
string Value;

Tagging_xmlns = xmlResponse.GetAttrValue("xmlns");
i = 0;
count_i = xmlResponse.NumChildrenHavingTag("TagSet|Tag");
while (i < count_i) {
    xmlResponse.I = 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>