PureBasic AWS S3: Add a Set of Tags to an S3 Object

Back to Index

Adds a set of tags to an existing S3 object. This example adds tags to the object named "starfish.jpg" located in the bucket "chilkat100".

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

CURL Command

curl -X PUT https://chilkat100.s3.amazonaws.com/starfish.jpg?tagging \
  -d '<Tagging>
  <TagSet>
     <Tag>
       <Key>animal</Key>
       <Value>starfish</Value>
     </Tag>
  </TagSet>
</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

    ;  See the Online Tool for Generating XML Creation Code
    xml.i = CkXml::ckCreate()
    If xml.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::setCkTag(xml, "Tagging")
    CkXml::ckUpdateChildContent(xml,"TagSet|Tag|Key","animal")
    CkXml::ckUpdateChildContent(xml,"TagSet|Tag|Value","starfish")

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

    CkXml::ckGetXmlSb(xml,sbRequestBody)
    sbResponseBody.i = CkStringBuilder::ckCreate()
    If sbResponseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkRest::ckFullRequestSb(rest,"PUT","/starfish.jpg?tagging",sbRequestBody,sbResponseBody)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkAuthAws::ckDispose(authAws)
        CkXml::ckDispose(xml)
        CkStringBuilder::ckDispose(sbRequestBody)
        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)
        CkXml::ckDispose(xml)
        CkStringBuilder::ckDispose(sbRequestBody)
        CkStringBuilder::ckDispose(sbResponseBody)
        ProcedureReturn
    EndIf



    CkRest::ckDispose(rest)
    CkAuthAws::ckDispose(authAws)
    CkXml::ckDispose(xml)
    CkStringBuilder::ckDispose(sbRequestBody)
    CkStringBuilder::ckDispose(sbResponseBody)


    ProcedureReturn
EndProcedure