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

Amazon Glacier: Resume a Multipart Upload

See more Amazon Glacier Examples

For existing Amazon Glacier vault customers. The standalone vault service is closed to new customers. These examples use its REST API version 2012-06-01, not the S3 bucket APIs used by S3 Glacier storage classes. See AWS service guidance.

Uses fixed 8 MiB parts, at most 10,000 parts (78.125 GiB), and buffers one part at a time. The API also allows other power-of-two MiB sizes up to 4 GiB; this portable example deliberately fixes the size. The last part may be smaller. Use a nonempty, immutable file and one writer per upload. Create qa_output first. Byte offsets and archive sizes stay exact in languages with 32-bit integers.

The private checkpoint stores the upload ID, region, vault, file path, size, part size, and full-file tree hash. Preserve it across failures. It contains no AWS credentials. A separate completion receipt records the archive ID. A lost initiation response requires inspecting List Multipart Uploads; starting a new upload is not a retry of the old one. Inactive uploads can expire after 24 hours. Do not abort automatically on transient errors.

Verifies the local file against its checkpoint, reads every List Parts page, and compares each exact range and tree hash. Matching parts are skipped; missing or mismatched parts are uploaded. Unexpected server ranges prevent completion. Keep the upload exclusive to this process. A missing upload may be completed, aborted, or expired: do not automatically start over. For an uncertain completion use retry completion.

Uses AWS Signature Version 4 with service glacier. Set the region once; the example builds the commercial AWS endpoint. Other partitions require the appropriate endpoint suffix. /-/ selects the signing account; change the vault name and IDs to your own resources. Load private qa_data/aws/glacier.json with string fields access_key, secret_key, and session_token for temporary credentials. Prefer short-lived role credentials. This sample does not discover profiles or refresh credentials. TLS certificate verification is enabled explicitly.

Checks transport completion, the operation-specific HTTP status, and parsed data separately. Inspect full diagnostics privately. Renew expired credentials; use bounded backoff with jitter for retryable reads. Uploads and job initiation can succeed before a connection fails, so do not blindly resubmit them. Configure IAM permissions for only the required operations and vault resources.

AWS reference

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoCredentials
    String sRegion
    Handle hoEndpoint
    Variant vAws
    Handle hoAws
    Handle hoRest
    Variant vConnection
    Handle hoConnection
    Handle hoBody
    Handle hoResult
    Handle hoError
    Handle hoRequestBody
    Variant vResponseBody
    Handle hoResponseBody
    Handle hoFiles
    String sStatePath
    String sReceiptPath
    Handle hoState
    Variant vSaved
    Handle hoSaved
    String sUploadId
    String sFilePath
    String sFileSize
    Handle hoCrypt
    String sTreeHash
    Handle hoRemoteParts
    Handle hoSeenMarkers
    String sMarker
    Boolean iMore
    Integer iPage
    Integer i
    String sRemoteRange
    Integer iPartSize
    Integer iNumParts
    Integer iByteHigh
    Integer iByteLow
    Integer iEndHigh
    Integer iEndLow
    Handle hoRange
    Handle hoDigits
    Variant vPart
    Handle hoPart
    Integer iPartIndex
    Integer iMatchedRemoteParts
    String sRangeKey
    String sPartHash
    Boolean iSendPart
    String sLinearHash
    String sConfirmedHash
    String sArchiveId
    String sLocation
    String sReturnedHash
    Handle hoReceipt
    String sTemp1
    String sTemp2
    String sTemp3
    Integer iTemp1
    Integer iTemp2
    Boolean bTemp1

    //  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 Glacier 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/glacier.json" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not load private AWS credentials."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoCredentials "access_key" To iTemp1
    Get ComStringOf Of hoCredentials "access_key" To sTemp1
    If ((iTemp1 <> 1) Or (sTemp1 = "")) Begin
        Showln "Missing or invalid access_key."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoCredentials "secret_key" To iTemp1
    Get ComStringOf Of hoCredentials "secret_key" To sTemp1
    If ((iTemp1 <> 1) Or (sTemp1 = "")) Begin
        Showln "Missing or invalid secret_key."
        Procedure_Return
    End

    //  Set this once to the region containing the vault.
    Move "us-west-2" To sRegion
    Get Create (RefClass(cComChilkatStringBuilder)) To hoEndpoint
    If (Not(IsComObjectCreated(hoEndpoint))) Begin
        Send CreateComObject of hoEndpoint
    End
    Get ComAppend Of hoEndpoint "glacier." To iSuccess
    Get ComAppend Of hoEndpoint sRegion To iSuccess
    Get ComAppend Of hoEndpoint ".amazonaws.com" To iSuccess
    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 "glacier"
    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 IAM/STS credentials require their session token on every signed request.
    //  Include the token in the signed query. Keep request URLs and diagnostics private.
    //  Leave session_token absent or empty only for credentials that do not have one.
    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

        Get ComStringOf Of hoCredentials "session_token" To sTemp1
        If (sTemp1 <> "") 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 set the session-token parameter."
                Procedure_Return
            End

        End

    End

    //  Supply a Socket to explicitly enable TLS certificate verification.
    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 ComAddHeader Of hoRest "x-amz-glacier-version" "2012-06-01" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not set x-amz-glacier-version."
        Procedure_Return
    End

    //  A hyphen selects the account belonging to the signing credentials.
    //  Use the original, URL-safe vault name and IDs returned by Glacier.
    Get ComAddPathParam Of hoRest "{vault}" "chilkat" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not set vault name."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoBody
    If (Not(IsComObjectCreated(hoBody))) Begin
        Send CreateComObject of hoBody
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoResult
    If (Not(IsComObjectCreated(hoResult))) Begin
        Send CreateComObject of hoResult
    End
    Set ComStrict Of hoResult To True
    Get Create (RefClass(cComChilkatJsonObject)) To hoError
    If (Not(IsComObjectCreated(hoError))) Begin
        Send CreateComObject of hoError
    End
    Set ComStrict Of hoError To True
    Get Create (RefClass(cComChilkatStringBuilder)) To hoRequestBody
    If (Not(IsComObjectCreated(hoRequestBody))) Begin
        Send CreateComObject of hoRequestBody
    End
    Get Create (RefClass(cComChilkatStringBuilder)) To hoResponseBody
    If (Not(IsComObjectCreated(hoResponseBody))) Begin
        Send CreateComObject of hoResponseBody
    End
    Get Create (RefClass(cComCkFileAccess)) To hoFiles
    If (Not(IsComObjectCreated(hoFiles))) Begin
        Send CreateComObject of hoFiles
    End
    Move "qa_output/glacier-multipart.json" To sStatePath
    Move "qa_output/glacier-multipart-completed.json" To sReceiptPath
    Get Create (RefClass(cComChilkatJsonObject)) To hoState
    If (Not(IsComObjectCreated(hoState))) Begin
        Send CreateComObject of hoState
    End
    Set ComStrict Of hoState To True
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSaved
    If (Not(IsComObjectCreated(hoSaved))) Begin
        Send CreateComObject of hoSaved
    End
    Get ComLoadFile Of hoState sStatePath To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not load the private multipart checkpoint."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoState "UploadId" To iTemp1
    If (iTemp1 <> 1) Begin
        Showln "Missing or invalid UploadId."
        Procedure_Return
    End

    Get ComStringOf Of hoState "UploadId" To sTemp1
    If (sTemp1 = "") Begin
        Showln "Empty UploadId."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoState "Region" To iTemp1
    If (iTemp1 <> 1) Begin
        Showln "Missing or invalid Region."
        Procedure_Return
    End

    Get ComStringOf Of hoState "Region" To sTemp1
    If (sTemp1 = "") Begin
        Showln "Empty Region."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoState "VaultName" To iTemp1
    If (iTemp1 <> 1) Begin
        Showln "Missing or invalid VaultName."
        Procedure_Return
    End

    Get ComStringOf Of hoState "VaultName" To sTemp1
    If (sTemp1 = "") Begin
        Showln "Empty VaultName."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoState "FilePath" To iTemp1
    If (iTemp1 <> 1) Begin
        Showln "Missing or invalid FilePath."
        Procedure_Return
    End

    Get ComStringOf Of hoState "FilePath" To sTemp1
    If (sTemp1 = "") Begin
        Showln "Empty FilePath."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoState "ArchiveSize" To iTemp1
    If (iTemp1 <> 1) Begin
        Showln "Missing or invalid ArchiveSize."
        Procedure_Return
    End

    Get ComStringOf Of hoState "ArchiveSize" To sTemp1
    If (sTemp1 = "") Begin
        Showln "Empty ArchiveSize."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoState "TreeHash" To iTemp1
    If (iTemp1 <> 1) Begin
        Showln "Missing or invalid TreeHash."
        Procedure_Return
    End

    Get ComStringOf Of hoState "TreeHash" To sTemp1
    If (sTemp1 = "") Begin
        Showln "Empty TreeHash."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoState "PartSize" To iTemp1
    If (iTemp1 <> 1) Begin
        Showln "Missing or invalid PartSize."
        Procedure_Return
    End

    Get ComStringOf Of hoState "PartSize" To sTemp1
    If (sTemp1 = "") Begin
        Showln "Empty PartSize."
        Procedure_Return
    End

    Get ComStringOf Of hoState "Region" To sTemp1
    Get ComStringOf Of hoState "VaultName" To sTemp2
    If ((sTemp1 <> sRegion) Or (sTemp2 <> "chilkat")) Begin
        Showln "Checkpoint region/vault differs from this example configuration."
        Procedure_Return
    End

    Get ComStringOf Of hoState "PartSize" To sTemp1
    If (sTemp1 <> "8388608") Begin
        Showln "This workflow requires the original 8 MiB part size."
        Procedure_Return
    End

    Get ComStringOf Of hoState "UploadId" To sUploadId
    Get ComAddPathParam Of hoRest "{upload}" sUploadId To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not set upload ID."
        Procedure_Return
    End

    Get ComFileExists Of hoFiles sReceiptPath To bTemp1
    If (bTemp1 = True) Begin
        Showln "A completion receipt already exists; this upload has finished."
        Procedure_Return
    End

    Get ComStringOf Of hoState "FilePath" To sFilePath
    Get ComFileSizeStr Of hoFiles sFilePath To sFileSize
    Get ComLastMethodSuccess Of hoFiles To bTemp1
    Get ComStringOf Of hoState "ArchiveSize" To sTemp1
    If ((bTemp1 <> True) Or (sFileSize <> sTemp1)) Begin
        Showln "Local file size differs from the checkpoint."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatCrypt2)) To hoCrypt
    If (Not(IsComObjectCreated(hoCrypt))) Begin
        Send CreateComObject of hoCrypt
    End
    Set ComHashAlgorithm Of hoCrypt To "sha256-tree-hash"
    Set ComEncodingMode Of hoCrypt To "hexlower"
    Get ComHashFileENC Of hoCrypt sFilePath To sTreeHash
    Get ComLastMethodSuccess Of hoCrypt To bTemp1
    Get ComStringOf Of hoState "TreeHash" To sTemp1
    If ((bTemp1 <> True) Or (sTreeHash <> sTemp1)) Begin
        Showln "Local file checksum differs from the checkpoint; do not resume with a different file."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatHashtable)) To hoRemoteParts
    If (Not(IsComObjectCreated(hoRemoteParts))) Begin
        Send CreateComObject of hoRemoteParts
    End
    Get ComAddQueryParam Of hoRest "limit" "50" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not set page limit."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatHashtable)) To hoSeenMarkers
    If (Not(IsComObjectCreated(hoSeenMarkers))) Begin
        Send CreateComObject of hoSeenMarkers
    End
    Move "" To sMarker
    Move True To iMore
    Move 0 To iPage
    While (iMore = True)
        If (iPage = 1000) Begin
            Showln "Page limit reached; listing is incomplete."
            Procedure_Return
        End

        Move (iPage + 1) To iPage
        If (sMarker <> "") Begin
            Get ComAddQueryParam Of hoRest "marker" sMarker To iSuccess
            If (iSuccess <> True) Begin
                Showln "Could not set marker."
                Procedure_Return
            End

        End

        Get pvComObject of hoResponseBody to vResponseBody
        Get ComFullRequestNoBodySb Of hoRest "GET" "/-/vaults/{vault}/multipart-uploads/{upload}" vResponseBody To iSuccess
        If (iSuccess <> True) Begin
            Showln "Request did not complete; inspect diagnostics privately. Check state before retrying a change."
            Procedure_Return
        End

        Get ComResponseStatusCode Of hoRest To iTemp1
        If (iTemp1 <> 200) Begin
            Get ComResponseStatusCode Of hoRest To iTemp1
            Showln "Glacier HTTP status: " iTemp1
            Get pvComObject of hoResponseBody to vResponseBody
            Get ComLoadSb Of hoError vResponseBody To iSuccess
            If (iSuccess = True) Begin
                Get ComJsonTypeOf Of hoError "code" To iTemp1
                If (iTemp1 = 1) Begin
                    Get ComStringOf Of hoError "code" To sTemp1
                    Showln "AWS error code: " sTemp1
                End

            End

            Get ComResponseHdrByName Of hoRest "x-amzn-RequestId" To sTemp1
            Showln "AWS request ID: " sTemp1
            //  Inspect full errors privately. An HTTP error is not a successful operation.
            Procedure_Return
        End

        Get pvComObject of hoResponseBody to vResponseBody
        Get ComLoadSb Of hoResult vResponseBody To iSuccess
        If (iSuccess <> True) Begin
            Showln "Expected valid JSON response."
            Procedure_Return
        End

        Get ComJsonTypeOf Of hoResult "MultipartUploadId" To iTemp1
        If (iTemp1 <> 1) Begin
            Showln "Missing or invalid MultipartUploadId."
            Procedure_Return
        End

        Get ComStringOf Of hoResult "MultipartUploadId" To sTemp1
        If (sTemp1 = "") Begin
            Showln "Empty MultipartUploadId."
            Procedure_Return
        End

        Get ComJsonTypeOf Of hoResult "PartSizeInBytes" To iTemp1
        If (iTemp1 <> 2) Begin
            Showln "Missing or invalid PartSizeInBytes."
            Procedure_Return
        End

        Get ComStringOf Of hoResult "MultipartUploadId" To sTemp1
        Get ComStringOf Of hoResult "PartSizeInBytes" To sTemp2
        Get ComStringOf Of hoState "PartSize" To sTemp3
        If ((sTemp1 <> sUploadId) Or (sTemp2 <> sTemp3)) Begin
            Showln "Server upload identity or part size differs from checkpoint."
            Procedure_Return
        End

        Get ComJsonTypeOf Of hoResult "Parts" To iTemp1
        If (iTemp1 <> 4) Begin
            Showln "Missing or invalid Parts."
            Procedure_Return
        End

        Move 0 To i
        While (i < (ComSizeOfArray(hoResult, "Parts")))
            Set ComI Of hoResult To i
            Get ComJsonTypeOf Of hoResult "Parts[i].RangeInBytes" To iTemp1
            If (iTemp1 <> 1) Begin
                Showln "Missing or invalid Parts[i].RangeInBytes."
                Procedure_Return
            End

            Get ComStringOf Of hoResult "Parts[i].RangeInBytes" To sTemp1
            If (sTemp1 = "") Begin
                Showln "Empty Parts[i].RangeInBytes."
                Procedure_Return
            End

            Get ComJsonTypeOf Of hoResult "Parts[i].SHA256TreeHash" To iTemp1
            If (iTemp1 <> 1) Begin
                Showln "Missing or invalid Parts[i].SHA256TreeHash."
                Procedure_Return
            End

            Get ComStringOf Of hoResult "Parts[i].SHA256TreeHash" To sTemp1
            If (sTemp1 = "") Begin
                Showln "Empty Parts[i].SHA256TreeHash."
                Procedure_Return
            End

            Get ComStringOf Of hoResult "Parts[i].RangeInBytes" To sRemoteRange
            Get ComContains Of hoRemoteParts sRemoteRange To bTemp1
            If (bTemp1 = True) Begin
                Showln "Duplicate server range; listing is inconsistent."
                Procedure_Return
            End

            Get ComStringOf Of hoResult "Parts[i].SHA256TreeHash" To sTemp1
            Get ComAddStr Of hoRemoteParts sRemoteRange sTemp1 To iSuccess
            If (iSuccess <> True) Begin
                Showln "Could not record server part."
                Procedure_Return
            End

            Move (i + 1) To i
        Loop

        Move False To iMore
        Get ComJsonTypeOf Of hoResult "Marker" To iTemp1
        If (iTemp1 = 1) Begin
            Get ComStringOf Of hoResult "Marker" To sMarker
            If (sMarker = "") Begin
                Showln "Empty marker; listing is incomplete."
                Procedure_Return
            End

            Get ComContains Of hoSeenMarkers sMarker To bTemp1
            If (bTemp1 = True) Begin
                Showln "Repeated marker; listing is incomplete."
                Procedure_Return
            End

            Get ComAddStr Of hoSeenMarkers sMarker "seen" To iSuccess
            If (iSuccess <> True) Begin
                Showln "Could not track pagination."
                Procedure_Return
            End

            Move True To iMore
        End
        Else Begin
            Get ComJsonTypeOf Of hoResult "Marker" To iTemp1
            If (iTemp1 <> 6) Begin
                Showln "Missing or invalid Marker; listing is incomplete."
                Procedure_Return
            End

        End

    Loop

    Get ComRemoveQueryParam Of hoRest "limit" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not clear limit."
        Procedure_Return
    End

    If (sMarker <> "") Begin
        Get ComRemoveQueryParam Of hoRest "marker" To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not clear marker."
            Procedure_Return
        End

    End

    Get ComOpenForRead Of hoFiles sFilePath To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not open the immutable source file."
        Send ComFileClose To hoFiles
        Procedure_Return
    End

    Move 8388608 To iPartSize
    Get ComGetNumBlocks Of hoFiles iPartSize To iNumParts
    If ((iNumParts < 1) Or (iNumParts > 10000)) Begin
        Showln "This 8 MiB example supports nonempty files of at most 10,000 parts."
        Send ComFileClose To hoFiles
        Procedure_Return
    End

    //  Two base-1,000,000,000 components avoid 32-bit overflow in COM languages.
    Move 0 To iByteHigh
    Move 0 To iByteLow

    Get Create (RefClass(cComChilkatStringBuilder)) To hoRange
    If (Not(IsComObjectCreated(hoRange))) Begin
        Send CreateComObject of hoRange
    End
    Get Create (RefClass(cComChilkatStringBuilder)) To hoDigits
    If (Not(IsComObjectCreated(hoDigits))) Begin
        Send CreateComObject of hoDigits
    End
    Get Create (RefClass(cComChilkatBinData)) To hoPart
    If (Not(IsComObjectCreated(hoPart))) Begin
        Send CreateComObject of hoPart
    End
    Move 0 To iPartIndex
    Move 0 To iMatchedRemoteParts
    While (iPartIndex < iNumParts)
        Get ComClear Of hoPart To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not clear part buffer."
            Send ComFileClose To hoFiles
            Procedure_Return
        End

        Get pvComObject of hoPart to vPart
        Get ComReadBlockBd Of hoFiles iPartIndex iPartSize vPart To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not read the source part."
            Send ComFileClose To hoFiles
            Procedure_Return
        End

        Get ComNumBytes Of hoPart To iTemp1
        Get ComNumBytes Of hoPart To iTemp2
        If ((iTemp1 = 0) Or ((iPartIndex < iNumParts - 1) And (iTemp2 <> iPartSize))) Begin
            Showln "Source file changed or a part was read incompletely."
            Send ComFileClose To hoFiles
            Procedure_Return
        End

        Send ComClear To hoRange
        Send ComClear To hoDigits
        Get ComAppendInt Of hoDigits iByteLow To iSuccess
        If (iByteHigh > 0) Begin
            While ((ComLength(hoDigits)) < 9)
                Get ComPrepend Of hoDigits "0" To iSuccess
            Loop

            Get ComAppendInt Of hoRange iByteHigh To iSuccess
        End

        Get ComGetAsString Of hoDigits To sTemp1
        Get ComAppend Of hoRange sTemp1 To iSuccess
        Get ComAppend Of hoRange "-" To iSuccess
        Move iByteHigh To iEndHigh
        Get ComNumBytes Of hoPart To iTemp1
        Move (iByteLow + iTemp1 - 1) To iEndLow
        If (iEndLow >= 1000000000) Begin
            Move (iEndLow - 1000000000) To iEndLow
            Move (iEndHigh + 1) To iEndHigh
        End

        Send ComClear To hoDigits
        Get ComAppendInt Of hoDigits iEndLow To iSuccess
        If (iEndHigh > 0) Begin
            While ((ComLength(hoDigits)) < 9)
                Get ComPrepend Of hoDigits "0" To iSuccess
            Loop

            Get ComAppendInt Of hoRange iEndHigh To iSuccess
        End

        Get ComGetAsString Of hoDigits To sTemp1
        Get ComAppend Of hoRange sTemp1 To iSuccess
        Get ComGetAsString Of hoRange To sRangeKey
        Set ComHashAlgorithm Of hoCrypt To "sha256-tree-hash"
        Get pvComObject of hoPart to vPart
        Get ComHashBdENC Of hoCrypt vPart To sPartHash
        Get ComLastMethodSuccess Of hoCrypt To bTemp1
        If (bTemp1 <> True) Begin
            Showln "Could not hash source part."
            Send ComFileClose To hoFiles
            Procedure_Return
        End

        Move True To iSendPart
        Get ComContains Of hoRemoteParts sRangeKey To bTemp1
        If (bTemp1 = True) Begin
            Move (iMatchedRemoteParts + 1) To iMatchedRemoteParts
            Get ComLookupStr Of hoRemoteParts sRangeKey To sTemp1
            If (sTemp1 = sPartHash) Begin
                Move False To iSendPart
                Showln "Verified existing part: " sRangeKey
            End

        End

        If (iSendPart = True) Begin
            Set ComHashAlgorithm Of hoCrypt To "sha256"
            Get pvComObject of hoPart to vPart
            Get ComHashBdENC Of hoCrypt vPart To sLinearHash
            Get ComLastMethodSuccess Of hoCrypt To bTemp1
            If (bTemp1 <> True) Begin
                Showln "Could not hash the part payload."
                Send ComFileClose To hoFiles
                Procedure_Return
            End

            Set ComPrecomputedSha256 Of hoAws To sLinearHash
            Get pvComObject of hoAws to vAws
            Get ComSetAuthAws Of hoRest vAws To iSuccess
            If (iSuccess <> True) Begin
                Showln "Could not configure part signing."
                Send ComFileClose To hoFiles
                Procedure_Return
            End

            Get ComAddHeader Of hoRest "x-amz-content-sha256" sLinearHash To iSuccess
            If (iSuccess <> True) Begin
                Showln "Could not set x-amz-content-sha256."
                Send ComFileClose To hoFiles
                Procedure_Return
            End

            Get ComAddHeader Of hoRest "x-amz-sha256-tree-hash" sPartHash To iSuccess
            If (iSuccess <> True) Begin
                Showln "Could not set x-amz-sha256-tree-hash."
                Send ComFileClose To hoFiles
                Procedure_Return
            End

            Get ComPrepend Of hoRange "bytes " To iSuccess
            Get ComAppend Of hoRange "/*" To iSuccess
            Get ComGetAsString Of hoRange To sTemp1
            Get ComAddHeader Of hoRest "Content-Range" sTemp1 To iSuccess
            If (iSuccess <> True) Begin
                Showln "Could not set Content-Range."
                Send ComFileClose To hoFiles
                Procedure_Return
            End

            Get ComAddHeader Of hoRest "Content-Type" "application/octet-stream" To iSuccess
            If (iSuccess <> True) Begin
                Showln "Could not set Content-Type."
                Send ComFileClose To hoFiles
                Procedure_Return
            End

            Get pvComObject of hoPart to vPart
            Get pvComObject of hoResponseBody to vResponseBody
            Get ComFullRequestBd Of hoRest "PUT" "/-/vaults/{vault}/multipart-uploads/{upload}" vPart vResponseBody To iSuccess
            If (iSuccess <> True) Begin
                Showln "Part request did not complete. Keep the checkpoint and run the resume example."
                Send ComFileClose To hoFiles
                Procedure_Return
            End

            Get ComResponseStatusCode Of hoRest To iTemp1
            If (iTemp1 <> 204) Begin
                Get ComResponseStatusCode Of hoRest To iTemp1
                Showln "Glacier HTTP status: " iTemp1
                Get pvComObject of hoResponseBody to vResponseBody
                Get ComLoadSb Of hoError vResponseBody To iSuccess
                If (iSuccess = True) Begin
                    Get ComJsonTypeOf Of hoError "code" To iTemp1
                    If (iTemp1 = 1) Begin
                        Get ComStringOf Of hoError "code" To sTemp1
                        Showln "AWS error code: " sTemp1
                    End

                End

                Get ComResponseHdrByName Of hoRest "x-amzn-RequestId" To sTemp1
                Showln "AWS request ID: " sTemp1
                //  Inspect full errors privately. An HTTP error is not a successful operation.
                Send ComFileClose To hoFiles
                Procedure_Return
            End

            Get ComResponseHdrByName Of hoRest "x-amz-sha256-tree-hash" To sConfirmedHash
            If (sConfirmedHash <> sPartHash) Begin
                Showln "Part response checksum mismatch. Keep the checkpoint and inspect List Parts."
                Send ComFileClose To hoFiles
                Procedure_Return
            End

            Showln "Uploaded part: " sRangeKey
        End

        Get ComNumBytes Of hoPart To iTemp1
        Move (iByteLow + iTemp1) To iByteLow
        If (iByteLow >= 1000000000) Begin
            Move (iByteLow - 1000000000) To iByteLow
            Move (iByteHigh + 1) To iByteHigh
        End

        Move (iPartIndex + 1) To iPartIndex
    Loop

    Send ComFileClose To hoFiles
    Get ComCount Of hoRemoteParts To iTemp1
    If (iMatchedRemoteParts <> iTemp1) Begin
        Showln "Server contains unexpected ranges. Do not complete; inspect the upload and source."
        Send ComFileClose To hoFiles
        Procedure_Return
    End

    //  Recheck the source before completion. Keep the file immutable throughout.
    Get ComFileSizeStr Of hoFiles sFilePath To sFileSize
    Get ComLastMethodSuccess Of hoFiles To bTemp1
    Get ComStringOf Of hoState "ArchiveSize" To sTemp1
    If ((bTemp1 <> True) Or (sFileSize <> sTemp1)) Begin
        Showln "Source size changed during upload; do not complete."
        Send ComFileClose To hoFiles
        Procedure_Return
    End

    Set ComHashAlgorithm Of hoCrypt To "sha256-tree-hash"
    Get ComHashFileENC Of hoCrypt sFilePath To sTreeHash
    Get ComLastMethodSuccess Of hoCrypt To bTemp1
    Get ComStringOf Of hoState "TreeHash" To sTemp1
    If ((bTemp1 <> True) Or (sTreeHash <> sTemp1)) Begin
        Showln "Source checksum changed during upload; do not complete."
        Send ComFileClose To hoFiles
        Procedure_Return
    End

    Get ComStringOf Of hoState "TreeHash" To sTemp1
    Get ComAddHeader Of hoRest "x-amz-sha256-tree-hash" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not set x-amz-sha256-tree-hash."
        Send ComFileClose To hoFiles
        Procedure_Return
    End

    Get ComStringOf Of hoState "ArchiveSize" To sTemp1
    Get ComAddHeader Of hoRest "x-amz-archive-size" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not set x-amz-archive-size."
        Send ComFileClose To hoFiles
        Procedure_Return
    End

    //  Completion has NO body: never reuse a part's precomputed payload hash.
    Set ComPrecomputedSha256 Of hoAws To ""
    Get pvComObject of hoAws to vAws
    Get ComSetAuthAws Of hoRest vAws To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not reset signing."
        Send ComFileClose To hoFiles
        Procedure_Return
    End

    Get ComRemoveHeader Of hoRest "x-amz-content-sha256" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not clear part payload hash."
        Send ComFileClose To hoFiles
        Procedure_Return
    End

    Get ComRemoveHeader Of hoRest "Content-Range" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not clear part range."
        Send ComFileClose To hoFiles
        Procedure_Return
    End

    Get ComRemoveHeader Of hoRest "Content-Type" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not clear content type."
        Send ComFileClose To hoFiles
        Procedure_Return
    End

    Get pvComObject of hoResponseBody to vResponseBody
    Get ComFullRequestNoBodySb Of hoRest "POST" "/-/vaults/{vault}/multipart-uploads/{upload}" vResponseBody To iSuccess
    If (iSuccess <> True) Begin
        Showln "Request did not complete; inspect diagnostics privately. Check state before retrying a change."
        Send ComFileClose To hoFiles
        Procedure_Return
    End

    Get ComResponseStatusCode Of hoRest To iTemp1
    If (iTemp1 <> 201) Begin
        Get ComResponseStatusCode Of hoRest To iTemp1
        Showln "Glacier HTTP status: " iTemp1
        Get pvComObject of hoResponseBody to vResponseBody
        Get ComLoadSb Of hoError vResponseBody To iSuccess
        If (iSuccess = True) Begin
            Get ComJsonTypeOf Of hoError "code" To iTemp1
            If (iTemp1 = 1) Begin
                Get ComStringOf Of hoError "code" To sTemp1
                Showln "AWS error code: " sTemp1
            End

        End

        Get ComResponseHdrByName Of hoRest "x-amzn-RequestId" To sTemp1
        Showln "AWS request ID: " sTemp1
        //  Inspect full errors privately. An HTTP error is not a successful operation.
        Send ComFileClose To hoFiles
        Procedure_Return
    End

    Get ComResponseHdrByName Of hoRest "x-amz-archive-id" To sArchiveId
    Get ComLastMethodSuccess Of hoRest To bTemp1
    If ((bTemp1 <> True) Or (sArchiveId = "")) Begin
        Showln "Completion response has no archive ID. Preserve the checkpoint and retry identical completion promptly."
        Send ComFileClose To hoFiles
        Procedure_Return
    End

    Showln "Archive ID: " sArchiveId
    Get ComResponseHdrByName Of hoRest "Location" To sLocation
    Showln "Location: " sLocation
    Get ComResponseHdrByName Of hoRest "x-amz-sha256-tree-hash" To sReturnedHash
    Get ComLastMethodSuccess Of hoRest To bTemp1
    Get ComStringOf Of hoState "TreeHash" To sTemp1
    If ((bTemp1 = True) And (sReturnedHash <> sTemp1)) Begin
        Showln "Completion checksum mismatch; preserve the printed archive ID and investigate."
        Send ComFileClose To hoFiles
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoReceipt
    If (Not(IsComObjectCreated(hoReceipt))) Begin
        Send CreateComObject of hoReceipt
    End
    Get ComUpdateString Of hoReceipt "UploadId" sUploadId To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not set UploadId."
        Send ComFileClose To hoFiles
        Procedure_Return
    End

    Get ComUpdateString Of hoReceipt "ArchiveId" sArchiveId To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not set ArchiveId."
        Send ComFileClose To hoFiles
        Procedure_Return
    End

    Get ComUpdateString Of hoReceipt "Location" sLocation To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not set Location."
        Send ComFileClose To hoFiles
        Procedure_Return
    End

    Get ComStringOf Of hoState "TreeHash" To sTemp1
    Get ComUpdateString Of hoReceipt "TreeHash" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not set TreeHash."
        Send ComFileClose To hoFiles
        Procedure_Return
    End

    Send ComClear To hoSaved
    Get pvComObject of hoSaved to vSaved
    Get ComEmitSb Of hoReceipt vSaved To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not serialize receipt; preserve the printed archive ID."
        Send ComFileClose To hoFiles
        Procedure_Return
    End

    Get ComWriteFile Of hoSaved sReceiptPath "utf-8" False To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not save receipt; preserve the printed archive ID."
        Send ComFileClose To hoFiles
        Procedure_Return
    End

    Showln "Completion receipt saved. Keep it in your archive catalog."


End_Procedure