PureBasic 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

PureBasic Example

IncludeFile "CkStringBuilder.pb"
IncludeFile "CkRest.pb"
IncludeFile "CkXml.pb"
IncludeFile "CkAuthAws.pb"

Procedure ChilkatExample()

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

    authAws.i = CkAuthAws::ckCreate()
    If authAws.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkAuthAws::setCkAccessKey(authAws, "AWS_ACCESS_KEY")
    CkAuthAws::setCkSecretKey(authAws, "AWS_SECRET_KEY")
    CkAuthAws::setCkRegion(authAws, "us-east-1")
    CkAuthAws::setCkServiceName(authAws, "s3")
    CkRest::ckSetAuthAws(rest,authAws)

    ;  URL: https://chilkat100.s3.amazonaws.com/starfish.jpg?tagging
    bTls.i = 1
    port.i = 443
    bAutoReconnect.i = 1
    success = CkRest::ckConnect(rest,"chilkat100.s3.amazonaws.com",port,bTls,bAutoReconnect)
    If success <> 1
        Debug "ConnectFailReason: " + Str(CkRest::ckConnectFailReason(rest))
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkAuthAws::ckDispose(authAws)
        ProcedureReturn
    EndIf

    sbResponseBody.i = CkStringBuilder::ckCreate()
    If sbResponseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkRest::ckFullRequestNoBodySb(rest,"GET","/starfish.jpg?tagging",sbResponseBody)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkAuthAws::ckDispose(authAws)
        CkStringBuilder::ckDispose(sbResponseBody)
        ProcedureReturn
    EndIf

    respStatusCode.i = CkRest::ckResponseStatusCode(rest)
    If respStatusCode >= 400
        Debug "Response Status Code = " + Str(respStatusCode)
        Debug "Response Header:"
        Debug CkRest::ckResponseHeader(rest)
        Debug "Response Body:"
        Debug CkStringBuilder::ckGetAsString(sbResponseBody)
        CkRest::ckDispose(rest)
        CkAuthAws::ckDispose(authAws)
        CkStringBuilder::ckDispose(sbResponseBody)
        ProcedureReturn
    EndIf

    xmlResponse.i = CkXml::ckCreate()
    If xmlResponse.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::ckLoadSb(xmlResponse,sbResponseBody,1)

    ;  See the Online Tool for Generating XML Parse Code
    Tagging_xmlns.s
    i.i
    count_i.i
    tagPath.s
    Key.s
    Value.s

    Tagging_xmlns = CkXml::ckGetAttrValue(xmlResponse,"xmlns")
    i = 0
    count_i = CkXml::ckNumChildrenHavingTag(xmlResponse,"TagSet|Tag")
    While i < count_i
        CkXml::setCkI(xmlResponse, i)
        Key = CkXml::ckGetChildContent(xmlResponse,"TagSet|Tag[i]|Key")
        Value = CkXml::ckGetChildContent(xmlResponse,"TagSet|Tag[i]|Value")
        i = i + 1
    Wend


    CkRest::ckDispose(rest)
    CkAuthAws::ckDispose(authAws)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkXml::ckDispose(xmlResponse)


    ProcedureReturn
EndProcedure

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>