Amazon S3: Read, Merge, and Remove Object Tags
See more S3 - REST Examples
Select read, merge, remove-one, or delete-all. Merge adds or replaces one named tag while preserving other tags from GetObjectTagging. Remove-one rebuilds the set without that key; delete-all uses DeleteObjectTagging. An empty tag value is valid. The replacement has at most 10 tags and includes a SHA-256 request checksum.
Tagging is read/modify/write, not an atomic conditional update. Coordinate concurrent tag writers; an ETag condition on the object is not a tag-set compare-and-swap. The returned version ID is reused when available to target that data version. For unversioned objects or the mutable null version, coordinate object writers as well as tag writers. Requires the corresponding Get/Put/DeleteObjectTagging permissions and their version-specific variants when versionId is used. Tags are distinct from user metadata.
For general purpose S3 buckets, using regional HTTPS endpoints and AWS Signature Version 4. Directory buckets (S3 Express), access points, Outposts, and other AWS partitions need their own endpoint/authentication rules. Use a bucket name without dots for these virtual-hosted TLS examples; see the path-style example for dotted names.
Provide current IAM credentials in a private qa_data/aws/s3.json file with string fields access_key, secret_key, and session_token for temporary credentials. Prefer short-lived role credentials. This file is explicitly loaded; these examples do not discover profiles or refresh credentials. Keep it outside public content and source control. The session token is included in the signed request. TLS certificate verification is enabled on the Socket used by Rest.
Transport success and HTTP success are different. Keep full diagnostics private; signed requests and presigned URLs can contain credentials or grant access. For retryable reads use bounded backoff with jitter. Before retrying a write with an uncertain outcome, check the resource state. Keep keys, upload IDs, version IDs, ETags, and large sizes as strings.
AWS GetObjectTagging reference
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoTextCompare
Handle hoCredentials
Boolean iSameText1
Boolean iSameText2
String sRegion
String sBucketName
Handle hoEndpoint
Variant vAws
Handle hoAws
Handle hoRest
Boolean iSameText3
Variant vConnection
Handle hoConnection
Handle hoXml
String sObjectKey
Handle hoObjectPath
Integer iRestoredSlashes
String sOperation
String sTagKey
String sTagValue
Boolean iSameText4
Boolean iSameText5
Boolean iSameText6
Boolean iSameText7
String sResponseBody
Boolean iSameText8
Boolean iSameText9
String sVersionId
Boolean iSameText10
Boolean iSameText11
Boolean iSameText12
Boolean iSameText13
Handle hoReplacement
Handle hoSeenKeys
Integer i
Integer iRetained
Boolean iSameText14
String sExistingKey
Boolean iSameText15
Boolean iSameText16
String sRequestBody
Handle hoDigest
String sChecksumBase64
Boolean iSameText17
String sTemp1
Integer iTemp1
Boolean bTemp1
Move False To iSuccess
// StringBuilder compares text by value in every generated language.
Get Create (RefClass(cComChilkatStringBuilder)) To hoTextCompare
If (Not(IsComObjectCreated(hoTextCompare))) Begin
Send CreateComObject of hoTextCompare
End
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Move False To iSuccess
// Private configuration, not a file in public web content or source control.
// Load current IAM credentials authorized for the S3 operation.
Get Create (RefClass(cComChilkatJsonObject)) To hoCredentials
If (Not(IsComObjectCreated(hoCredentials))) Begin
Send CreateComObject of hoCredentials
End
Set ComStrict Of hoCredentials To True
Get ComLoadFile Of hoCredentials "qa_data/aws/s3.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not load private AWS credentials."
Procedure_Return
End
Send ComClear To hoTextCompare
Get ComStringOf Of hoCredentials "access_key" To sTemp1
Get ComSetString Of hoTextCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoTextCompare "" True To iSameText1
Get ComJsonTypeOf Of hoCredentials "access_key" To iTemp1
If ((iTemp1 <> 1) Or (iSameText1 = True)) Begin
Showln "Missing or invalid access_key."
Procedure_Return
End
Send ComClear To hoTextCompare
Get ComStringOf Of hoCredentials "secret_key" To sTemp1
Get ComSetString Of hoTextCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoTextCompare "" True To iSameText2
Get ComJsonTypeOf Of hoCredentials "secret_key" To iTemp1
If ((iTemp1 <> 1) Or (iSameText2 = True)) Begin
Showln "Missing or invalid secret_key."
Procedure_Return
End
// General purpose bucket in the commercial AWS partition.
Move "us-west-2" To sRegion
Move "amzn-s3-demo-bucket" To sBucketName
Get Create (RefClass(cComChilkatStringBuilder)) To hoEndpoint
If (Not(IsComObjectCreated(hoEndpoint))) Begin
Send CreateComObject of hoEndpoint
End
Get ComAppend Of hoEndpoint sBucketName To iSuccess
If (iSuccess <> True) Begin
Showln "Could not build text."
Procedure_Return
End
Get ComAppend Of hoEndpoint "." To iSuccess
If (iSuccess <> True) Begin
Showln "Could not build text."
Procedure_Return
End
Get ComAppend Of hoEndpoint "s3." To iSuccess
If (iSuccess <> True) Begin
Showln "Could not build text."
Procedure_Return
End
Get ComAppend Of hoEndpoint sRegion To iSuccess
If (iSuccess <> True) Begin
Showln "Could not build text."
Procedure_Return
End
Get ComAppend Of hoEndpoint ".amazonaws.com" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not build text."
Procedure_Return
End
Get Create (RefClass(cComChilkatAuthAws)) To hoAws
If (Not(IsComObjectCreated(hoAws))) Begin
Send CreateComObject of hoAws
End
Get ComStringOf Of hoCredentials "access_key" To sTemp1
Set ComAccessKey Of hoAws To sTemp1
Get ComStringOf Of hoCredentials "secret_key" To sTemp1
Set ComSecretKey Of hoAws To sTemp1
Set ComRegion Of hoAws To sRegion
Set ComServiceName Of hoAws To "s3"
Set ComSignatureVersion Of hoAws To 4
Get Create (RefClass(cComChilkatRest)) To hoRest
If (Not(IsComObjectCreated(hoRest))) Begin
Send CreateComObject of hoRest
End
Set ComConnectTimeoutMs Of hoRest To 20000
Set ComIdleTimeoutMs Of hoRest To 60000
Get pvComObject of hoAws to vAws
Get ComSetAuthAws Of hoRest vAws To iSuccess
If (iSuccess <> True) Begin
Showln "Could not configure AWS signing."
Procedure_Return
End
// Temporary credentials require this token in every signed query.
// Keep request URLs private. Do not clear this parameter between requests.
Get ComHasMember Of hoCredentials "session_token" To bTemp1
If (bTemp1 = True) Begin
Get ComJsonTypeOf Of hoCredentials "session_token" To iTemp1
If (iTemp1 <> 1) Begin
Showln "session_token must be a string."
Procedure_Return
End
Send ComClear To hoTextCompare
Get ComStringOf Of hoCredentials "session_token" To sTemp1
Get ComSetString Of hoTextCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoTextCompare "" True To iSameText3
If (iSameText3 <> True) Begin
Get ComStringOf Of hoCredentials "session_token" To sTemp1
Get ComAddQueryParam Of hoRest "X-Amz-Security-Token" sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not prepare the request."
Procedure_Return
End
End
End
// Connect to the SAME hostname used for TLS verification and request signing.
Get Create (RefClass(cComChilkatSocket)) To hoConnection
If (Not(IsComObjectCreated(hoConnection))) Begin
Send CreateComObject of hoConnection
End
Set ComRequireSslCertVerify Of hoConnection To True
Set ComMaxReadIdleMs Of hoConnection To 60000
Set ComMaxSendIdleMs Of hoConnection To 60000
Get ComGetAsString Of hoEndpoint To sTemp1
Get ComConnect Of hoConnection sTemp1 443 True 20000 To iSuccess
If (iSuccess <> True) Begin
Showln "TLS connection failed; inspect diagnostics privately."
Procedure_Return
End
Get pvComObject of hoConnection to vConnection
Get ComUseConnection Of hoRest vConnection True To iSuccess
If (iSuccess <> True) Begin
Showln "Could not attach the TLS connection."
Procedure_Return
End
Get Create (RefClass(cComChilkatXml)) To hoXml
If (Not(IsComObjectCreated(hoXml))) Begin
Send CreateComObject of hoXml
End
Set ComStrict Of hoXml To True
// Supply an unencoded object key, not a URL. Preserve slashes in the key.
Move "documents/example.txt" To sObjectKey
Get Create (RefClass(cComChilkatStringBuilder)) To hoObjectPath
If (Not(IsComObjectCreated(hoObjectPath))) Begin
Send CreateComObject of hoObjectPath
End
Get ComAppend Of hoObjectPath sObjectKey To iSuccess
If (iSuccess <> True) Begin
Showln "Could not build text."
Procedure_Return
End
Get ComEncode Of hoObjectPath "url_rfc3986" "utf-8" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not encode the object key."
Procedure_Return
End
Get ComReplace Of hoObjectPath "%2F" "/" To iRestoredSlashes
Get ComPrepend Of hoObjectPath "/" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not prepare the request."
Procedure_Return
End
Move "merge" To sOperation
Move "Environment" To sTagKey
Move "test" To sTagValue
Send ComClear To hoTextCompare
Get ComSetString Of hoTextCompare sOperation To iSuccess
Get ComContentsEqual Of hoTextCompare "read" True To iSameText4
Send ComClear To hoTextCompare
Get ComSetString Of hoTextCompare sOperation To iSuccess
Get ComContentsEqual Of hoTextCompare "merge" True To iSameText5
Send ComClear To hoTextCompare
Get ComSetString Of hoTextCompare sOperation To iSuccess
Get ComContentsEqual Of hoTextCompare "remove-one" True To iSameText6
Send ComClear To hoTextCompare
Get ComSetString Of hoTextCompare sOperation To iSuccess
Get ComContentsEqual Of hoTextCompare "delete-all" True To iSameText7
If ((iSameText4 <> True) And (iSameText5 <> True) And (iSameText6 <> True) And (iSameText7 <> True)) Begin
Showln "Choose read, merge, remove-one, or delete-all."
Procedure_Return
End
Get ComAddQueryParam Of hoRest "tagging" "" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not prepare the request."
Procedure_Return
End
Get ComGetAsString Of hoObjectPath To sTemp1
Get ComFullRequestNoBody Of hoRest "GET" sTemp1 To sResponseBody
Get ComLastMethodSuccess Of hoRest To bTemp1
If (bTemp1 <> True) Begin
Showln "Request did not complete; inspect diagnostics privately."
Procedure_Return
End
Get ComResponseStatusCode Of hoRest To iTemp1
If (iTemp1 <> 200) Begin
Get ComResponseStatusCode Of hoRest To iTemp1
Showln "S3 HTTP status: " iTemp1
Get ComLoadXml Of hoXml sResponseBody To iSuccess
Send ComClear To hoTextCompare
Get ComTag Of hoXml To sTemp1
Get ComSetString Of hoTextCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoTextCompare "Error" True To iSameText8
If ((iSuccess = True) And (iSameText8 = True)) Begin
Get ComGetChildContent Of hoXml "Code" To sTemp1
Showln "S3 error code: " sTemp1
End
// Keep signed headers, credentials, URLs, and full diagnostics private.
// A timeout may leave a write's outcome uncertain. Check before retrying.
Procedure_Return
End
Get ComLoadXml Of hoXml sResponseBody To iSuccess
If (iSuccess <> True) Begin
Showln "Expected well-formed XML."
Procedure_Return
End
Send ComClear To hoTextCompare
Get ComTag Of hoXml To sTemp1
Get ComSetString Of hoTextCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoTextCompare "Tagging" True To iSameText9
If (iSameText9 <> True) Begin
Showln "Unexpected S3 response element; this is not a successful result."
Procedure_Return
End
Get ComHasChildWithTag Of hoXml "TagSet" To bTemp1
If (bTemp1 <> True) Begin
Showln "Missing TagSet."
Procedure_Return
End
// Pin the version that was read when S3 returns its version ID.
Get ComResponseHdrByName Of hoRest "x-amz-version-id" To sVersionId
Get ComLastMethodSuccess Of hoRest To bTemp1
If (bTemp1 <> True) Begin
Move "" To sVersionId
End
Send ComClear To hoTextCompare
Get ComSetString Of hoTextCompare sVersionId To iSuccess
Get ComContentsEqual Of hoTextCompare "" True To iSameText10
If (iSameText10 <> True) Begin
Get ComAddQueryParam Of hoRest "versionId" sVersionId To iSuccess
If (iSuccess <> True) Begin
Showln "Could not prepare the request."
Procedure_Return
End
End
Send ComClear To hoTextCompare
Get ComSetString Of hoTextCompare sOperation To iSuccess
Get ComContentsEqual Of hoTextCompare "read" True To iSameText11
If (iSameText11 = True) Begin
Get ComGetXml Of hoXml To sTemp1
Showln sTemp1
Procedure_Return
End
Send ComClear To hoTextCompare
Get ComSetString Of hoTextCompare sOperation To iSuccess
Get ComContentsEqual Of hoTextCompare "delete-all" True To iSameText12
If (iSameText12 = True) Begin
Get ComGetAsString Of hoObjectPath To sTemp1
Get ComFullRequestNoBody Of hoRest "DELETE" sTemp1 To sResponseBody
Get ComLastMethodSuccess Of hoRest To bTemp1
If (bTemp1 <> True) Begin
Showln "Request did not complete; inspect diagnostics privately."
Procedure_Return
End
Get ComResponseStatusCode Of hoRest To iTemp1
If (iTemp1 <> 204) Begin
Get ComResponseStatusCode Of hoRest To iTemp1
Showln "S3 HTTP status: " iTemp1
Get ComLoadXml Of hoXml sResponseBody To iSuccess
Send ComClear To hoTextCompare
Get ComTag Of hoXml To sTemp1
Get ComSetString Of hoTextCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoTextCompare "Error" True To iSameText13
If ((iSuccess = True) And (iSameText13 = True)) Begin
Get ComGetChildContent Of hoXml "Code" To sTemp1
Showln "S3 error code: " sTemp1
End
// Keep signed headers, credentials, URLs, and full diagnostics private.
// A timeout may leave a write's outcome uncertain. Check before retrying.
Procedure_Return
End
Showln "All object tags removed."
Procedure_Return
End
Get Create (RefClass(cComChilkatXml)) To hoReplacement
If (Not(IsComObjectCreated(hoReplacement))) Begin
Send CreateComObject of hoReplacement
End
Set ComTag Of hoReplacement To "Tagging"
Get ComAddAttribute Of hoReplacement "xmlns" "http://s3.amazonaws.com/doc/2006-03-01/" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not prepare the request."
Procedure_Return
End
Send ComNewChild2 To hoReplacement "TagSet" ""
Get Create (RefClass(cComChilkatHashtable)) To hoSeenKeys
If (Not(IsComObjectCreated(hoSeenKeys))) Begin
Send CreateComObject of hoSeenKeys
End
Move 0 To i
Move 0 To iRetained
While (i < (ComNumChildrenAt(hoXml, "TagSet")))
Set ComI Of hoXml To i
Send ComClear To hoTextCompare
Get ComGetChildContent Of hoXml "TagSet|Tag[i]|Key" To sTemp1
Get ComSetString Of hoTextCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoTextCompare "" True To iSameText14
Get ComHasChildWithTag Of hoXml "TagSet|Tag[i]|Key" To bTemp1
If ((bTemp1 <> True) Or (iSameText14 = True)) Begin
Showln "Missing response field: TagSet|Tag[i]|Key"
Procedure_Return
End
Get ComHasChildWithTag Of hoXml "TagSet|Tag[i]|Value" To bTemp1
If (bTemp1 <> True) Begin
Showln "Missing tag value."
Procedure_Return
End
Get ComGetChildContent Of hoXml "TagSet|Tag[i]|Key" To sExistingKey
Get ComContains Of hoSeenKeys sExistingKey To bTemp1
If (bTemp1 = True) Begin
Showln "Duplicate key in returned tag set."
Procedure_Return
End
Get ComAddInt Of hoSeenKeys sExistingKey 1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not prepare the request."
Procedure_Return
End
Send ComClear To hoTextCompare
Get ComSetString Of hoTextCompare sExistingKey To iSuccess
Get ComContentsEqual Of hoTextCompare sTagKey True To iSameText15
If (iSameText15 <> True) Begin
Set ComI Of hoReplacement To iRetained
Send ComUpdateChildContent To hoReplacement "TagSet|Tag[i]|Key" sExistingKey
Get ComGetChildContent Of hoXml "TagSet|Tag[i]|Value" To sTemp1
Send ComUpdateChildContent To hoReplacement "TagSet|Tag[i]|Value" sTemp1
Move (iRetained + 1) To iRetained
End
Move (i + 1) To i
Loop
Send ComClear To hoTextCompare
Get ComSetString Of hoTextCompare sOperation To iSuccess
Get ComContentsEqual Of hoTextCompare "merge" True To iSameText16
If (iSameText16 = True) Begin
Set ComI Of hoReplacement To iRetained
Send ComUpdateChildContent To hoReplacement "TagSet|Tag[i]|Key" sTagKey
Send ComUpdateChildContent To hoReplacement "TagSet|Tag[i]|Value" sTagValue
Move (iRetained + 1) To iRetained
End
If (iRetained > 10) Begin
Showln "The resulting tag set exceeds 10 tags; no change was sent."
Procedure_Return
End
Get ComGetXml Of hoReplacement To sRequestBody
Get Create (RefClass(cComChilkatCrypt2)) To hoDigest
If (Not(IsComObjectCreated(hoDigest))) Begin
Send CreateComObject of hoDigest
End
Set ComCharset Of hoDigest To "utf-8"
Set ComHashAlgorithm Of hoDigest To "sha256"
Set ComEncodingMode Of hoDigest To "base64"
Get ComHashStringENC Of hoDigest sRequestBody To sChecksumBase64
Get ComLastMethodSuccess Of hoDigest To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not calculate the content checksum."
Procedure_Return
End
Get ComAddHeader Of hoRest "x-amz-checksum-sha256" sChecksumBase64 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not prepare the request."
Procedure_Return
End
Get ComAddHeader Of hoRest "Content-Type" "application/xml" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not prepare the request."
Procedure_Return
End
Get ComGetAsString Of hoObjectPath To sTemp1
Get ComFullRequestString Of hoRest "PUT" sTemp1 sRequestBody To sResponseBody
Get ComLastMethodSuccess Of hoRest To bTemp1
If (bTemp1 <> True) Begin
Showln "Request did not complete; inspect diagnostics privately."
Procedure_Return
End
Get ComResponseStatusCode Of hoRest To iTemp1
If (iTemp1 <> 200) Begin
Get ComResponseStatusCode Of hoRest To iTemp1
Showln "S3 HTTP status: " iTemp1
Get ComLoadXml Of hoXml sResponseBody To iSuccess
Send ComClear To hoTextCompare
Get ComTag Of hoXml To sTemp1
Get ComSetString Of hoTextCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoTextCompare "Error" True To iSameText17
If ((iSuccess = True) And (iSameText17 = True)) Begin
Get ComGetChildContent Of hoXml "Code" To sTemp1
Showln "S3 error code: " sTemp1
End
// Keep signed headers, credentials, URLs, and full diagnostics private.
// A timeout may leave a write's outcome uncertain. Check before retrying.
Procedure_Return
End
Showln "Updated tag set saved; unrelated tags were preserved from the read."
End_Procedure