Sample code for 30+ languages & platforms
Visual FoxPro Requires Chilkat v11.6.0+

Amazon S3: Download a Version or Remove a Delete Marker

See more S3 - REST Examples

The default download mode reads a selected data version without changing the current object. Requires s3:GetObjectVersion. Choose an opaque version ID from the versions listing, including the string null when appropriate. An archived version may first need restoration.

Recovery mode changes version history: set operation to remove-delete-marker and supply the particular marker ID. HEAD with that versionId must return 405 and x-amz-delete-marker=true before the code issues DELETE. A normal data version is never deleted by this branch. Requires s3:DeleteObjectVersion; MFA Delete can impose additional requirements not implemented here. Removing a current marker can expose an older version, but other remaining markers can still hide the object. List versions again; do not assume recovery is complete.

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 GetObject reference

Chilkat Visual FoxPro Downloads

Visual FoxPro
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 lcVersionId
LOCAL lcOperation
LOCAL lnSameText4
LOCAL lnSameText5
LOCAL lnSameText6
LOCAL lnMarkerStatus
LOCAL lcMarker
LOCAL lnSameText7
LOCAL lcResponseBody
LOCAL lnSameText8
LOCAL loFiles
LOCAL lnStatusCode
LOCAL lcErrorBody
LOCAL lnSameText9
LOCAL lcReturnedVersion
LOCAL lnSameText10
LOCAL lnSameText11
LOCAL lcExpectedLength
LOCAL lnSameText12
LOCAL loOutput
LOCAL lcActualLength
LOCAL lnSameText13

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

*  Select an ID from ListObjectVersions. Never parse it as a number.
lcVersionId = "YOUR_VERSION_ID"
lcOperation = "download"
loTextCompare.Clear()
loTextCompare.SetString(lcOperation)
lnSameText4 = loTextCompare.ContentsEqual("download",1)
loTextCompare.Clear()
loTextCompare.SetString(lcOperation)
lnSameText5 = loTextCompare.ContentsEqual("remove-delete-marker",1)
IF ((lnSameText4 <> 1) AND (lnSameText5 <> 1)) THEN
    ? "Choose download or remove-delete-marker."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    CANCEL
ENDIF

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

loTextCompare.Clear()
loTextCompare.SetString(lcOperation)
lnSameText6 = loTextCompare.ContentsEqual("remove-delete-marker",1)
IF (lnSameText6 = 1) THEN
    *  This permanently removes ONLY the selected marker, not a data version.
    lnSuccess = loRest.SendReqNoBody("HEAD",loObjectPath.GetAsString())
    IF (lnSuccess <> 1) THEN
        ? "Could not send HEAD request."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        RELEASE loObjectPath
        CANCEL
    ENDIF

    lnMarkerStatus = loRest.ReadResponseHeader()
    IF (lnMarkerStatus < 0) THEN
        ? "Could not read the response header."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        RELEASE loObjectPath
        CANCEL
    ENDIF

    *  HEAD has no response body; do not call a body-reading method.
    lcMarker = loRest.ResponseHdrByName("x-amz-delete-marker")
    IF (loRest.LastMethodSuccess <> 1) THEN
        lcMarker = ""
    ENDIF

    loTextCompare.Clear()
    loTextCompare.SetString(lcMarker)
    lnSameText7 = loTextCompare.ContentsEqual("true",1)
    IF ((lnMarkerStatus <> 405) OR (lnSameText7 <> 1)) THEN
        ? "Selected ID was not confirmed as a delete marker; nothing was deleted."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        RELEASE loObjectPath
        CANCEL
    ENDIF

    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)
        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

    ? "Selected delete marker removed. List versions again to inspect the current entry."
    *  Other markers can still hide the object. Do not automatically delete those.
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    CANCEL
ENDIF

loFiles = CreateObject('Chilkat.FileAccess')
IF (loFiles.FileExists("s3-version-download.part") = 1) THEN
    ? "Choose an unused output path."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loFiles
    CANCEL
ENDIF

lnSuccess = loRest.SendReqNoBody("GET",loObjectPath.GetAsString())
IF (lnSuccess <> 1) THEN
    ? "Could not send download request."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loFiles
    CANCEL
ENDIF

lnStatusCode = loRest.ReadResponseHeader()
IF (lnStatusCode < 0) THEN
    ? "Could not read download headers."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loFiles
    CANCEL
ENDIF

IF (lnStatusCode <> 200) THEN
    ? "Download HTTP status: " + STR(lnStatusCode)
    lcErrorBody = loRest.ReadRespBodyString()
    IF (loRest.LastMethodSuccess = 1) THEN
        lnSuccess = loXml.LoadXml(lcErrorBody)
        loTextCompare.Clear()
        loTextCompare.SetString(loXml.Tag)
        lnSameText9 = loTextCompare.ContentsEqual("Error",1)
        IF ((lnSuccess = 1) AND (lnSameText9 = 1)) THEN
            ? "S3 error code: " + loXml.GetChildContent("Code")
        ENDIF

    ENDIF

    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loFiles
    CANCEL
ENDIF

lcReturnedVersion = loRest.ResponseHdrByName("x-amz-version-id")
loTextCompare.Clear()
loTextCompare.SetString(lcReturnedVersion)
lnSameText10 = loTextCompare.ContentsEqual("",1)
IF ((loRest.LastMethodSuccess <> 1) OR (lnSameText10 = 1)) THEN
    ? "Missing response header: x-amz-version-id"
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loFiles
    CANCEL
ENDIF

loTextCompare.Clear()
loTextCompare.SetString(lcReturnedVersion)
lnSameText11 = loTextCompare.ContentsEqual(lcVersionId,1)
IF (lnSameText11 <> 1) THEN
    ? "Unexpected version returned; output was not written."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loFiles
    CANCEL
ENDIF

lcExpectedLength = loRest.ResponseHdrByName("Content-Length")
loTextCompare.Clear()
loTextCompare.SetString(lcExpectedLength)
lnSameText12 = loTextCompare.ContentsEqual("",1)
IF ((loRest.LastMethodSuccess <> 1) OR (lnSameText12 = 1)) THEN
    ? "Missing response header: Content-Length"
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loFiles
    CANCEL
ENDIF

loOutput = CreateObject('Chilkat.Stream')
loOutput.SinkFile = "s3-version-download.part"
lnSuccess = loRest.ReadRespBodyStream(loOutput,1)
IF (lnSuccess <> 1) THEN
    ? "Download incomplete; do not publish the partial file."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loFiles
    RELEASE loOutput
    CANCEL
ENDIF

lcActualLength = loFiles.FileSizeStr("s3-version-download.part")
loTextCompare.Clear()
loTextCompare.SetString(lcActualLength)
lnSameText13 = loTextCompare.ContentsEqual(lcExpectedLength,1)
IF ((loFiles.LastMethodSuccess <> 1) OR (lnSameText13 <> 1)) THEN
    ? "Downloaded length mismatch."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loFiles
    RELEASE loOutput
    CANCEL
ENDIF

? "Selected version downloaded to s3-version-download.part."

RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
RELEASE loFiles
RELEASE loOutput