Tcl 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>'

Tcl Example


load ./chilkat.dll

set rest [new_CkRest]

set authAws [new_CkAuthAws]

CkAuthAws_put_AccessKey $authAws "AWS_ACCESS_KEY"
CkAuthAws_put_SecretKey $authAws "AWS_SECRET_KEY"
CkAuthAws_put_Region $authAws "us-east-1"
CkAuthAws_put_ServiceName $authAws "s3"
CkRest_SetAuthAws $rest $authAws

#  URL: https://chilkat100.s3.amazonaws.com/starfish.jpg?tagging
set bTls 1
set port 443
set bAutoReconnect 1
set success [CkRest_Connect $rest "chilkat100.s3.amazonaws.com" $port $bTls $bAutoReconnect]
if {[expr $success != 1]} then {
    puts "ConnectFailReason: [CkRest_ConnectFailReason $rest]"
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    delete_CkAuthAws $authAws
    exit
}

#  See the Online Tool for Generating XML Creation Code
set xml [new_CkXml]

CkXml_put_Tag $xml "Tagging"
CkXml_UpdateChildContent $xml "TagSet|Tag|Key" "animal"
CkXml_UpdateChildContent $xml "TagSet|Tag|Value" "starfish"

set sbRequestBody [new_CkStringBuilder]

CkXml_GetXmlSb $xml $sbRequestBody
set sbResponseBody [new_CkStringBuilder]

set success [CkRest_FullRequestSb $rest "PUT" "/starfish.jpg?tagging" $sbRequestBody $sbResponseBody]
if {[expr $success != 1]} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    delete_CkAuthAws $authAws
    delete_CkXml $xml
    delete_CkStringBuilder $sbRequestBody
    delete_CkStringBuilder $sbResponseBody
    exit
}

set respStatusCode [CkRest_ResponseStatusCode $rest]
if {[expr $respStatusCode >= 400]} then {
    puts "Response Status Code = $respStatusCode"
    puts "Response Header:"
    puts [CkRest_responseHeader $rest]
    puts "Response Body:"
    puts [CkStringBuilder_getAsString $sbResponseBody]
    delete_CkRest $rest
    delete_CkAuthAws $authAws
    delete_CkXml $xml
    delete_CkStringBuilder $sbRequestBody
    delete_CkStringBuilder $sbResponseBody
    exit
}


delete_CkRest $rest
delete_CkAuthAws $authAws
delete_CkXml $xml
delete_CkStringBuilder $sbRequestBody
delete_CkStringBuilder $sbResponseBody