Node.js 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

Node.js Example

var os = require('os');
if (os.platform() == 'win32') {  
    var chilkat = require('chilkat_node6_win32'); 
} else if (os.platform() == 'linux') {
    if (os.arch() == 'arm') {
        var chilkat = require('chilkat_node6_arm');
    } else if (os.arch() == 'x86') {
        var chilkat = require('chilkat_node6_linux32');
    } else {
        var chilkat = require('chilkat_node6_linux64');
    }
} else if (os.platform() == 'darwin') {
    var chilkat = require('chilkat_node6_macosx');
}

function chilkatExample() {

    var rest = new chilkat.Rest();
    var success;

    var 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
    var bTls = true;
    var port = 443;
    var bAutoReconnect = true;
    success = rest.Connect("chilkat100.s3.amazonaws.com",port,bTls,bAutoReconnect);
    if (success !== true) {
        console.log("ConnectFailReason: " + rest.ConnectFailReason);
        console.log(rest.LastErrorText);
        return;
    }

    var sbResponseBody = new chilkat.StringBuilder();
    success = rest.FullRequestNoBodySb("GET","/starfish.jpg?tagging",sbResponseBody);
    if (success !== true) {
        console.log(rest.LastErrorText);
        return;
    }

    var respStatusCode = rest.ResponseStatusCode;
    if (respStatusCode >= 400) {
        console.log("Response Status Code = " + respStatusCode);
        console.log("Response Header:");
        console.log(rest.ResponseHeader);
        console.log("Response Body:");
        console.log(sbResponseBody.GetAsString());
        return;
    }

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

    //  See the Online Tool for Generating XML Parse Code
    var Tagging_xmlns;
    var i;
    var count_i;
    var tagPath;
    var Key;
    var 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;
    }


}

chilkatExample();

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>