Perl 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

Perl Example

use chilkat();

$rest = chilkat::CkRest->new();

$authAws = chilkat::CkAuthAws->new();
$authAws->put_AccessKey("AWS_ACCESS_KEY");
$authAws->put_SecretKey("AWS_SECRET_KEY");
$authAws->put_Region("us-east-1");
$authAws->put_ServiceName("s3");
$rest->SetAuthAws($authAws);

#  URL: https://chilkat100.s3.amazonaws.com/starfish.jpg?tagging
$bTls = 1;
$port = 443;
$bAutoReconnect = 1;
$success = $rest->Connect("chilkat100.s3.amazonaws.com",$port,$bTls,$bAutoReconnect);
if ($success != 1) {
    print "ConnectFailReason: " . $rest->get_ConnectFailReason() . "\r\n";
    print $rest->lastErrorText() . "\r\n";
    exit;
}

$sbResponseBody = chilkat::CkStringBuilder->new();
$success = $rest->FullRequestNoBodySb("GET","/starfish.jpg?tagging",$sbResponseBody);
if ($success != 1) {
    print $rest->lastErrorText() . "\r\n";
    exit;
}

$respStatusCode = $rest->get_ResponseStatusCode();
if ($respStatusCode >= 400) {
    print "Response Status Code = " . $respStatusCode . "\r\n";
    print "Response Header:" . "\r\n";
    print $rest->responseHeader() . "\r\n";
    print "Response Body:" . "\r\n";
    print $sbResponseBody->getAsString() . "\r\n";
    exit;
}

$xmlResponse = chilkat::CkXml->new();
$xmlResponse->LoadSb($sbResponseBody,1);

#  See the Online Tool for Generating XML Parse Code

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