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

Amazon S3: Restore an Archived Object and Check Readiness

See more S3 - REST Examples

For GLACIER (S3 Glacier Flexible Retrieval) or DEEP_ARCHIVE objects, inspect HEAD first. Request mode starts a Standard restoration with a seven-day restored-copy lifetime if neither a ready nor in-progress copy is reported. Check mode never sends a restore request. Restoration can take hours, so the example returns instead of polling continuously.

Requires s3:RestoreObject and permission to read object metadata. Retrieval and restored-copy storage can incur charges. A 202 is acceptance, not readiness; 200 can mean an already restored object, and RestoreAlreadyInProgress is a specific 409 response. Use x-amz-restore ongoing-request=false and its expiry to decide when to use the download examples. A version returned by HEAD is pinned on the request.

Restoration creates a temporary accessible copy, not a permanent storage-class change. Lifecycle expiration can shorten availability. Intelligent-Tiering archive tiers have different restoration semantics (including no Days value) and are intentionally outside this example. Glacier Instant Retrieval does not require this restore workflow.

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

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

;  StringBuilder compares text by value in every generated language.
$oTextCompare = ObjCreate("Chilkat.StringBuilder")
;  Assumes Chilkat is unlocked. See Global Unlock Sample.
$bSuccess = False
;  Private configuration, not a file in public web content or source control.
;  Load current IAM credentials authorized for the S3 operation.
$oCredentials = ObjCreate("Chilkat.JsonObject")
$oCredentials.Strict = True
$bSuccess = $oCredentials.LoadFile("qa_data/aws/s3.json")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not load private AWS credentials." & @CRLF)
    Exit
EndIf

$oTextCompare.Clear 
$oTextCompare.SetString($oCredentials.StringOf("access_key"))
Local $bSameText1 = $oTextCompare.ContentsEqual("",True)
If (($oCredentials.JsonTypeOf("access_key") <> 1) Or ($bSameText1 = True)) Then
    ConsoleWrite("Missing or invalid access_key." & @CRLF)
    Exit
EndIf

$oTextCompare.Clear 
$oTextCompare.SetString($oCredentials.StringOf("secret_key"))
Local $bSameText2 = $oTextCompare.ContentsEqual("",True)
If (($oCredentials.JsonTypeOf("secret_key") <> 1) Or ($bSameText2 = True)) Then
    ConsoleWrite("Missing or invalid secret_key." & @CRLF)
    Exit
EndIf

;  General purpose bucket in the commercial AWS partition.
Local $sRegion = "us-west-2"
Local $sBucketName = "amzn-s3-demo-bucket"
$oEndpoint = ObjCreate("Chilkat.StringBuilder")
$bSuccess = $oEndpoint.Append($sBucketName)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not build text." & @CRLF)
    Exit
EndIf

$bSuccess = $oEndpoint.Append(".")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not build text." & @CRLF)
    Exit
EndIf

$bSuccess = $oEndpoint.Append("s3.")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not build text." & @CRLF)
    Exit
EndIf

$bSuccess = $oEndpoint.Append($sRegion)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not build text." & @CRLF)
    Exit
EndIf

$bSuccess = $oEndpoint.Append(".amazonaws.com")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not build text." & @CRLF)
    Exit
EndIf

$oAws = ObjCreate("Chilkat.AuthAws")
$oAws.AccessKey = $oCredentials.StringOf("access_key")
$oAws.SecretKey = $oCredentials.StringOf("secret_key")
$oAws.Region = $sRegion
$oAws.ServiceName = "s3"
$oAws.SignatureVersion = 4
$oRest = ObjCreate("Chilkat.Rest")
$oRest.ConnectTimeoutMs = 20000
$oRest.IdleTimeoutMs = 60000
$bSuccess = $oRest.SetAuthAws($oAws)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not configure AWS signing." & @CRLF)
    Exit
EndIf

;  Temporary credentials require this token in every signed query.
;  Keep request URLs private. Do not clear this parameter between requests.
If ($oCredentials.HasMember("session_token") = True) Then
    If ($oCredentials.JsonTypeOf("session_token") <> 1) Then
        ConsoleWrite("session_token must be a string." & @CRLF)
        Exit
    EndIf

    $oTextCompare.Clear 
    $oTextCompare.SetString($oCredentials.StringOf("session_token"))
Local $bSameText3 = $oTextCompare.ContentsEqual("",True)
    If ($bSameText3 <> True) Then
        $bSuccess = $oRest.AddQueryParam("X-Amz-Security-Token",$oCredentials.StringOf("session_token"))
        If ($bSuccess <> True) Then
            ConsoleWrite("Could not prepare the request." & @CRLF)
            Exit
        EndIf

    EndIf

EndIf

;  Connect to the SAME hostname used for TLS verification and request signing.
$oConnection = ObjCreate("Chilkat.Socket")
$oConnection.RequireSslCertVerify = True
$oConnection.MaxReadIdleMs = 60000
$oConnection.MaxSendIdleMs = 60000
$bSuccess = $oConnection.Connect($oEndpoint.GetAsString(),443,True,20000)
If ($bSuccess <> True) Then
    ConsoleWrite("TLS connection failed; inspect diagnostics privately." & @CRLF)
    Exit
EndIf

$bSuccess = $oRest.UseConnection($oConnection,True)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not attach the TLS connection." & @CRLF)
    Exit
EndIf

$oXml = ObjCreate("Chilkat.Xml")
$oXml.Strict = True
;  Supply an unencoded object key, not a URL. Preserve slashes in the key.
Local $sObjectKey = "documents/example.txt"
$oObjectPath = ObjCreate("Chilkat.StringBuilder")
$bSuccess = $oObjectPath.Append($sObjectKey)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not build text." & @CRLF)
    Exit
EndIf

$bSuccess = $oObjectPath.Encode("url_rfc3986","utf-8")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not encode the object key." & @CRLF)
    Exit
EndIf

Local $iRestoredSlashes = $oObjectPath.Replace("%2F","/")
$bSuccess = $oObjectPath.Prepend("/")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not prepare the request." & @CRLF)
    Exit
EndIf

Local $sOperation = "request"
$oTextCompare.Clear 
$oTextCompare.SetString($sOperation)
Local $bSameText4 = $oTextCompare.ContentsEqual("request",True)
$oTextCompare.Clear 
$oTextCompare.SetString($sOperation)
Local $bSameText5 = $oTextCompare.ContentsEqual("check",True)
If (($bSameText4 <> True) And ($bSameText5 <> True)) Then
    ConsoleWrite("Choose request or check." & @CRLF)
    Exit
EndIf

$bSuccess = $oRest.SendReqNoBody("HEAD",$oObjectPath.GetAsString())
If ($bSuccess <> True) Then
    ConsoleWrite("Could not send HEAD request." & @CRLF)
    Exit
EndIf

Local $iStatusCode = $oRest.ReadResponseHeader()
If ($iStatusCode < 0) Then
    ConsoleWrite("Could not read the response header." & @CRLF)
    Exit
EndIf

If ($iStatusCode <> 200) Then
    ConsoleWrite("HEAD did not return HTTP 200. A 403 is not proof that an object is missing." & @CRLF)
    Exit
EndIf

;  HEAD has no response body; do not call a body-reading method.
Local $storageClass = $oRest.ResponseHdrByName("x-amz-storage-class")
If ($oRest.LastMethodSuccess <> True) Then
    $storageClass = ""
EndIf

Local $sRestoreStatus = $oRest.ResponseHdrByName("x-amz-restore")
If ($oRest.LastMethodSuccess <> True) Then
    $sRestoreStatus = ""
EndIf

$oRestoreHeader = ObjCreate("Chilkat.StringBuilder")
$bSuccess = $oRestoreHeader.Append($sRestoreStatus)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not build text." & @CRLF)
    Exit
EndIf

If ($oRestoreHeader.Contains("ongoing-request=""false""",True) = True) Then
    ConsoleWrite("Restored copy is available. Check its expiry before downloading." & @CRLF)
    ConsoleWrite($sRestoreStatus & @CRLF)
    Exit
EndIf

If ($oRestoreHeader.Contains("ongoing-request=""true""",True) = True) Then
    ConsoleWrite("Restoration is in progress. Run check mode later." & @CRLF)
    Exit
EndIf

$oTextCompare.Clear 
$oTextCompare.SetString($storageClass)
Local $bSameText6 = $oTextCompare.ContentsEqual("GLACIER",True)
$oTextCompare.Clear 
$oTextCompare.SetString($storageClass)
Local $bSameText7 = $oTextCompare.ContentsEqual("DEEP_ARCHIVE",True)
If (($bSameText6 <> True) And ($bSameText7 <> True)) Then
    ConsoleWrite("This example targets Glacier Flexible Retrieval and Deep Archive objects only." & @CRLF)
    Exit
EndIf

$oTextCompare.Clear 
$oTextCompare.SetString($sOperation)
Local $bSameText8 = $oTextCompare.ContentsEqual("check",True)
If ($bSameText8 = True) Then
    ConsoleWrite("No available restored copy was reported. No restore request was sent." & @CRLF)
    Exit
EndIf

Local $sVersionId = $oRest.ResponseHdrByName("x-amz-version-id")
If ($oRest.LastMethodSuccess <> True) Then
    $sVersionId = ""
EndIf

$oTextCompare.Clear 
$oTextCompare.SetString($sVersionId)
Local $bSameText9 = $oTextCompare.ContentsEqual("",True)
If ($bSameText9 <> True) Then
    $bSuccess = $oRest.AddQueryParam("versionId",$sVersionId)
    If ($bSuccess <> True) Then
        ConsoleWrite("Could not prepare the request." & @CRLF)
        Exit
    EndIf

EndIf

$oXml.Tag = "RestoreRequest"
$bSuccess = $oXml.AddAttribute("xmlns","http://s3.amazonaws.com/doc/2006-03-01/")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not prepare the request." & @CRLF)
    Exit
EndIf

$oXml.UpdateChildContentInt "Days",7
$oXml.UpdateChildContent "GlacierJobParameters|Tier","Standard"
$bSuccess = $oRest.AddQueryParam("restore","")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not prepare the request." & @CRLF)
    Exit
EndIf

$bSuccess = $oRest.AddHeader("Content-Type","application/xml")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not prepare the request." & @CRLF)
    Exit
EndIf

Local $sResponseBody = $oRest.FullRequestString("POST",$oObjectPath.GetAsString(),$oXml.GetXml())
If ($oRest.LastMethodSuccess <> True) Then
    ConsoleWrite("Restore outcome is uncertain; check status before submitting again." & @CRLF)
    Exit
EndIf

If (($oRest.ResponseStatusCode = 200) Or ($oRest.ResponseStatusCode = 202)) Then
    ConsoleWrite("Restore request accepted. Run check mode later; acceptance is not download readiness." & @CRLF)
    Exit
EndIf

$bSuccess = $oXml.LoadXml($sResponseBody)
$oTextCompare.Clear 
$oTextCompare.SetString($oXml.Tag)
Local $bSameText10 = $oTextCompare.ContentsEqual("Error",True)
$oTextCompare.Clear 
$oTextCompare.SetString($oXml.GetChildContent("Code"))
Local $bSameText11 = $oTextCompare.ContentsEqual("RestoreAlreadyInProgress",True)
If (($oRest.ResponseStatusCode = 409) And ($bSuccess = True) And ($bSameText10 = True) And ($bSameText11 = True)) Then
    ConsoleWrite("Restoration is already in progress. Run check mode later." & @CRLF)
    Exit
EndIf

If ($oRest.ResponseStatusCode <> 200) Then
    ConsoleWrite("S3 HTTP status: " & $oRest.ResponseStatusCode & @CRLF)
    $bSuccess = $oXml.LoadXml($sResponseBody)
    $oTextCompare.Clear 
    $oTextCompare.SetString($oXml.Tag)
Local $bSameText12 = $oTextCompare.ContentsEqual("Error",True)
    If (($bSuccess = True) And ($bSameText12 = True)) Then
        ConsoleWrite("S3 error code: " & $oXml.GetChildContent("Code") & @CRLF)
    EndIf

    ;  Keep signed headers, credentials, URLs, and full diagnostics private.
    ;  A timeout may leave a write's outcome uncertain. Check before retrying.
    Exit
EndIf

ConsoleWrite("Unexpected restore result." & @CRLF)