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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loTextCompare
LOCAL loCredentials
LOCAL lnSameText1
LOCAL lnSameText2
LOCAL lcRegion
LOCAL lcBucketName
LOCAL loEndpoint
LOCAL loAws
LOCAL loRest
LOCAL lnSameText3
LOCAL loConnection
LOCAL loXml
LOCAL lcObjectKey
LOCAL loObjectPath
LOCAL lnRestoredSlashes
LOCAL lcOperation
LOCAL lcTagKey
LOCAL lcTagValue
LOCAL lnSameText4
LOCAL lnSameText5
LOCAL lnSameText6
LOCAL lnSameText7
LOCAL lcResponseBody
LOCAL lnSameText8
LOCAL lnSameText9
LOCAL lcVersionId
LOCAL lnSameText10
LOCAL lnSameText11
LOCAL lnSameText12
LOCAL lnSameText13
LOCAL loReplacement
LOCAL loSeenKeys
LOCAL i
LOCAL lnRetained
LOCAL lnSameText14
LOCAL lcExistingKey
LOCAL lnSameText15
LOCAL lnSameText16
LOCAL lcRequestBody
LOCAL loDigest
LOCAL lcChecksumBase64
LOCAL lnSameText17
lnSuccess = 0
* StringBuilder compares text by value in every generated language.
loTextCompare = CreateObject('Chilkat.StringBuilder')
* Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0
* Private configuration, not a file in public web content or source control.
* Load current IAM credentials authorized for the S3 operation.
loCredentials = CreateObject('Chilkat.JsonObject')
loCredentials.Strict = 1
lnSuccess = loCredentials.LoadFile("qa_data/aws/s3.json")
IF (lnSuccess <> 1) THEN
? "Could not load private AWS credentials."
RELEASE loTextCompare
RELEASE loCredentials
CANCEL
ENDIF
loTextCompare.Clear()
loTextCompare.SetString(loCredentials.StringOf("access_key"))
lnSameText1 = loTextCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("access_key") <> 1) OR (lnSameText1 = 1)) THEN
? "Missing or invalid access_key."
RELEASE loTextCompare
RELEASE loCredentials
CANCEL
ENDIF
loTextCompare.Clear()
loTextCompare.SetString(loCredentials.StringOf("secret_key"))
lnSameText2 = loTextCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("secret_key") <> 1) OR (lnSameText2 = 1)) THEN
? "Missing or invalid secret_key."
RELEASE loTextCompare
RELEASE loCredentials
CANCEL
ENDIF
* General purpose bucket in the commercial AWS partition.
lcRegion = "us-west-2"
lcBucketName = "amzn-s3-demo-bucket"
loEndpoint = CreateObject('Chilkat.StringBuilder')
lnSuccess = loEndpoint.Append(lcBucketName)
IF (lnSuccess <> 1) THEN
? "Could not build text."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
CANCEL
ENDIF
lnSuccess = loEndpoint.Append(".")
IF (lnSuccess <> 1) THEN
? "Could not build text."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
CANCEL
ENDIF
lnSuccess = loEndpoint.Append("s3.")
IF (lnSuccess <> 1) THEN
? "Could not build text."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
CANCEL
ENDIF
lnSuccess = loEndpoint.Append(lcRegion)
IF (lnSuccess <> 1) THEN
? "Could not build text."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
CANCEL
ENDIF
lnSuccess = loEndpoint.Append(".amazonaws.com")
IF (lnSuccess <> 1) THEN
? "Could not build text."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
CANCEL
ENDIF
loAws = CreateObject('Chilkat.AuthAws')
loAws.AccessKey = loCredentials.StringOf("access_key")
loAws.SecretKey = loCredentials.StringOf("secret_key")
loAws.Region = lcRegion
loAws.ServiceName = "s3"
loAws.SignatureVersion = 4
loRest = CreateObject('Chilkat.Rest')
loRest.ConnectTimeoutMs = 20000
loRest.IdleTimeoutMs = 60000
lnSuccess = loRest.SetAuthAws(loAws)
IF (lnSuccess <> 1) THEN
? "Could not configure AWS signing."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
CANCEL
ENDIF
* Temporary credentials require this token in every signed query.
* Keep request URLs private. Do not clear this parameter between requests.
IF (loCredentials.HasMember("session_token") = 1) THEN
IF (loCredentials.JsonTypeOf("session_token") <> 1) THEN
? "session_token must be a string."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
CANCEL
ENDIF
loTextCompare.Clear()
loTextCompare.SetString(loCredentials.StringOf("session_token"))
lnSameText3 = loTextCompare.ContentsEqual("",1)
IF (lnSameText3 <> 1) THEN
lnSuccess = loRest.AddQueryParam("X-Amz-Security-Token",loCredentials.StringOf("session_token"))
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
CANCEL
ENDIF
ENDIF
ENDIF
* Connect to the SAME hostname used for TLS verification and request signing.
loConnection = CreateObject('Chilkat.Socket')
loConnection.RequireSslCertVerify = 1
loConnection.MaxReadIdleMs = 60000
loConnection.MaxSendIdleMs = 60000
lnSuccess = loConnection.Connect(loEndpoint.GetAsString(),443,1,20000)
IF (lnSuccess <> 1) THEN
? "TLS connection failed; inspect diagnostics privately."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
CANCEL
ENDIF
lnSuccess = loRest.UseConnection(loConnection,1)
IF (lnSuccess <> 1) THEN
? "Could not attach the TLS connection."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
CANCEL
ENDIF
loXml = CreateObject('Chilkat.Xml')
loXml.Strict = 1
* Supply an unencoded object key, not a URL. Preserve slashes in the key.
lcObjectKey = "documents/example.txt"
loObjectPath = CreateObject('Chilkat.StringBuilder')
lnSuccess = loObjectPath.Append(lcObjectKey)
IF (lnSuccess <> 1) THEN
? "Could not build text."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
CANCEL
ENDIF
lnSuccess = loObjectPath.Encode("url_rfc3986","utf-8")
IF (lnSuccess <> 1) THEN
? "Could not encode the object key."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
CANCEL
ENDIF
lnRestoredSlashes = loObjectPath.Replace("%2F","/")
lnSuccess = loObjectPath.Prepend("/")
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
CANCEL
ENDIF
lcOperation = "merge"
lcTagKey = "Environment"
lcTagValue = "test"
loTextCompare.Clear()
loTextCompare.SetString(lcOperation)
lnSameText4 = loTextCompare.ContentsEqual("read",1)
loTextCompare.Clear()
loTextCompare.SetString(lcOperation)
lnSameText5 = loTextCompare.ContentsEqual("merge",1)
loTextCompare.Clear()
loTextCompare.SetString(lcOperation)
lnSameText6 = loTextCompare.ContentsEqual("remove-one",1)
loTextCompare.Clear()
loTextCompare.SetString(lcOperation)
lnSameText7 = loTextCompare.ContentsEqual("delete-all",1)
IF ((lnSameText4 <> 1) AND (lnSameText5 <> 1) AND (lnSameText6 <> 1) AND (lnSameText7 <> 1)) THEN
? "Choose read, merge, remove-one, or delete-all."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
CANCEL
ENDIF
lnSuccess = loRest.AddQueryParam("tagging","")
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
CANCEL
ENDIF
lcResponseBody = loRest.FullRequestNoBody("GET",loObjectPath.GetAsString())
IF (loRest.LastMethodSuccess <> 1) THEN
? "Request did not complete; inspect diagnostics privately."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
CANCEL
ENDIF
IF (loRest.ResponseStatusCode <> 200) THEN
? "S3 HTTP status: " + STR(loRest.ResponseStatusCode)
lnSuccess = loXml.LoadXml(lcResponseBody)
loTextCompare.Clear()
loTextCompare.SetString(loXml.Tag)
lnSameText8 = loTextCompare.ContentsEqual("Error",1)
IF ((lnSuccess = 1) AND (lnSameText8 = 1)) THEN
? "S3 error code: " + loXml.GetChildContent("Code")
ENDIF
* Keep signed headers, credentials, URLs, and full diagnostics private.
* A timeout may leave a write's outcome uncertain. Check before retrying.
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
CANCEL
ENDIF
lnSuccess = loXml.LoadXml(lcResponseBody)
IF (lnSuccess <> 1) THEN
? "Expected well-formed XML."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
CANCEL
ENDIF
loTextCompare.Clear()
loTextCompare.SetString(loXml.Tag)
lnSameText9 = loTextCompare.ContentsEqual("Tagging",1)
IF (lnSameText9 <> 1) THEN
? "Unexpected S3 response element; this is not a successful result."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
CANCEL
ENDIF
IF (loXml.HasChildWithTag("TagSet") <> 1) THEN
? "Missing TagSet."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
CANCEL
ENDIF
* Pin the version that was read when S3 returns its version ID.
lcVersionId = loRest.ResponseHdrByName("x-amz-version-id")
IF (loRest.LastMethodSuccess <> 1) THEN
lcVersionId = ""
ENDIF
loTextCompare.Clear()
loTextCompare.SetString(lcVersionId)
lnSameText10 = loTextCompare.ContentsEqual("",1)
IF (lnSameText10 <> 1) THEN
lnSuccess = loRest.AddQueryParam("versionId",lcVersionId)
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
CANCEL
ENDIF
ENDIF
loTextCompare.Clear()
loTextCompare.SetString(lcOperation)
lnSameText11 = loTextCompare.ContentsEqual("read",1)
IF (lnSameText11 = 1) THEN
? loXml.GetXml()
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
CANCEL
ENDIF
loTextCompare.Clear()
loTextCompare.SetString(lcOperation)
lnSameText12 = loTextCompare.ContentsEqual("delete-all",1)
IF (lnSameText12 = 1) THEN
lcResponseBody = loRest.FullRequestNoBody("DELETE",loObjectPath.GetAsString())
IF (loRest.LastMethodSuccess <> 1) THEN
? "Request did not complete; inspect diagnostics privately."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
CANCEL
ENDIF
IF (loRest.ResponseStatusCode <> 204) THEN
? "S3 HTTP status: " + STR(loRest.ResponseStatusCode)
lnSuccess = loXml.LoadXml(lcResponseBody)
loTextCompare.Clear()
loTextCompare.SetString(loXml.Tag)
lnSameText13 = loTextCompare.ContentsEqual("Error",1)
IF ((lnSuccess = 1) AND (lnSameText13 = 1)) THEN
? "S3 error code: " + loXml.GetChildContent("Code")
ENDIF
* Keep signed headers, credentials, URLs, and full diagnostics private.
* A timeout may leave a write's outcome uncertain. Check before retrying.
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
CANCEL
ENDIF
? "All object tags removed."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
CANCEL
ENDIF
loReplacement = CreateObject('Chilkat.Xml')
loReplacement.Tag = "Tagging"
lnSuccess = loReplacement.AddAttribute("xmlns","http://s3.amazonaws.com/doc/2006-03-01/")
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
RELEASE loReplacement
CANCEL
ENDIF
loReplacement.NewChild2("TagSet","")
loSeenKeys = CreateObject('Chilkat.Hashtable')
i = 0
lnRetained = 0
DO WHILE (i < loXml.NumChildrenAt("TagSet"))
loXml.I = i
loTextCompare.Clear()
loTextCompare.SetString(loXml.GetChildContent("TagSet|Tag[i]|Key"))
lnSameText14 = loTextCompare.ContentsEqual("",1)
IF ((loXml.HasChildWithTag("TagSet|Tag[i]|Key") <> 1) OR (lnSameText14 = 1)) THEN
? "Missing response field: TagSet|Tag[i]|Key"
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
RELEASE loReplacement
RELEASE loSeenKeys
CANCEL
ENDIF
IF (loXml.HasChildWithTag("TagSet|Tag[i]|Value") <> 1) THEN
? "Missing tag value."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
RELEASE loReplacement
RELEASE loSeenKeys
CANCEL
ENDIF
lcExistingKey = loXml.GetChildContent("TagSet|Tag[i]|Key")
IF (loSeenKeys.Contains(lcExistingKey) = 1) THEN
? "Duplicate key in returned tag set."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
RELEASE loReplacement
RELEASE loSeenKeys
CANCEL
ENDIF
lnSuccess = loSeenKeys.AddInt(lcExistingKey,1)
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
RELEASE loReplacement
RELEASE loSeenKeys
CANCEL
ENDIF
loTextCompare.Clear()
loTextCompare.SetString(lcExistingKey)
lnSameText15 = loTextCompare.ContentsEqual(lcTagKey,1)
IF (lnSameText15 <> 1) THEN
loReplacement.I = lnRetained
loReplacement.UpdateChildContent("TagSet|Tag[i]|Key",lcExistingKey)
loReplacement.UpdateChildContent("TagSet|Tag[i]|Value",loXml.GetChildContent("TagSet|Tag[i]|Value"))
lnRetained = lnRetained + 1
ENDIF
i = i + 1
ENDDO
loTextCompare.Clear()
loTextCompare.SetString(lcOperation)
lnSameText16 = loTextCompare.ContentsEqual("merge",1)
IF (lnSameText16 = 1) THEN
loReplacement.I = lnRetained
loReplacement.UpdateChildContent("TagSet|Tag[i]|Key",lcTagKey)
loReplacement.UpdateChildContent("TagSet|Tag[i]|Value",lcTagValue)
lnRetained = lnRetained + 1
ENDIF
IF (lnRetained > 10) THEN
? "The resulting tag set exceeds 10 tags; no change was sent."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
RELEASE loReplacement
RELEASE loSeenKeys
CANCEL
ENDIF
lcRequestBody = loReplacement.GetXml()
loDigest = CreateObject('Chilkat.Crypt2')
loDigest.Charset = "utf-8"
loDigest.HashAlgorithm = "sha256"
loDigest.EncodingMode = "base64"
lcChecksumBase64 = loDigest.HashStringENC(lcRequestBody)
IF (loDigest.LastMethodSuccess <> 1) THEN
? "Could not calculate the content checksum."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
RELEASE loReplacement
RELEASE loSeenKeys
RELEASE loDigest
CANCEL
ENDIF
lnSuccess = loRest.AddHeader("x-amz-checksum-sha256",lcChecksumBase64)
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
RELEASE loReplacement
RELEASE loSeenKeys
RELEASE loDigest
CANCEL
ENDIF
lnSuccess = loRest.AddHeader("Content-Type","application/xml")
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
RELEASE loReplacement
RELEASE loSeenKeys
RELEASE loDigest
CANCEL
ENDIF
lcResponseBody = loRest.FullRequestString("PUT",loObjectPath.GetAsString(),lcRequestBody)
IF (loRest.LastMethodSuccess <> 1) THEN
? "Request did not complete; inspect diagnostics privately."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
RELEASE loReplacement
RELEASE loSeenKeys
RELEASE loDigest
CANCEL
ENDIF
IF (loRest.ResponseStatusCode <> 200) THEN
? "S3 HTTP status: " + STR(loRest.ResponseStatusCode)
lnSuccess = loXml.LoadXml(lcResponseBody)
loTextCompare.Clear()
loTextCompare.SetString(loXml.Tag)
lnSameText17 = loTextCompare.ContentsEqual("Error",1)
IF ((lnSuccess = 1) AND (lnSameText17 = 1)) THEN
? "S3 error code: " + loXml.GetChildContent("Code")
ENDIF
* Keep signed headers, credentials, URLs, and full diagnostics private.
* A timeout may leave a write's outcome uncertain. Check before retrying.
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
RELEASE loReplacement
RELEASE loSeenKeys
RELEASE loDigest
CANCEL
ENDIF
? "Updated tag set saved; unrelated tags were preserved from the read."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
RELEASE loReplacement
RELEASE loSeenKeys
RELEASE loDigest