S3 Http: Download an Object Byte Range
See more S3 - HTTP Examples
Uses Chilkat.Http with Amazon S3 general purpose buckets and Signature Version 4. These examples do not cover directory buckets/S3 Express, access-point ARNs, Outposts, or other specialized endpoints. They target the commercial AWS partition; other partitions need their documented endpoint suffix.
Load private qa_data/aws/s3.json containing nonempty string fields access_key and secret_key, plus session_token for temporary credentials. Prefer short-lived role credentials. These samples do not discover AWS profiles or refresh credentials. Set the bucket's actual region and replace sample bucket names and object keys. TLS certificate verification is explicitly enabled. Keep detailed HTTP diagnostics private because they can contain credentials or presigned URLs.
Uses explicit signed Http requests to preserve query parameters, conditional headers, and full responses. The included SigV4 block signs the exact path, sorted encoded query and headers used by this example; it is not a general-purpose HTTP signing library. Keep the clock accurate and do not change signed data after signing. Code requires Chilkat 11.6.0 or later; locally tested with 11.8.0.
HEAD selects the current object and captures its ETag and version ID when available. GET requests bytes=0-1048575 using If-Match and that version. Requires HTTP 206, exact Content-Range/Content-Length, matching ETag, and the actual 1 MiB file length. HTTP 200 (Range ignored), 412, and 416 never promote output. Use an object at least 1 MiB in size and unused output paths.
Ranges are inclusive object offsets. This example downloads a range of an S3 object directly; it is not a standalone Glacier retrieval job. A whole-object checksum is not a checksum of a range. Requires s3:GetObject/s3:GetObjectVersion and applicable KMS permissions; archived objects must already be restored.
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoStringCompare
Handle hoCredentials
Boolean iStringMatch1
Boolean iStringMatch2
Handle hoHttp
Boolean iStringMatch3
String sRegion
Handle hoEndpoint
String sBucketName
String sObjectKey
Variant vResponse
Handle hoResponse
Handle hoSigner
Handle hoNow
Handle hoTimestamp
Handle hoCanonical
Handle hoCanonicalHeaders
Handle hoSignedHeaders
Handle hoScope
Handle hoStringToSign
Handle hoSigningKey
Handle hoAuthorization
Handle hoUrl
Handle hoQuery
Handle hoNumber
Handle hoHost
Handle hoUri
Handle hoXml
String sAmzDate
String sDateStamp
String sPayloadHash
String sCanonicalHash
String sDerivedKey
String sSignature
Integer iReplaceCount
String sVersionId
String sEtag
String sExpectedSize
String sReturnedVersion
Handle hoFiles
String sFinalPath
String sPartialPath
Boolean iStringMatch4
Boolean iStringMatch5
Boolean iStringMatch6
Boolean iStringMatch7
Boolean iStringMatch8
Boolean iStringMatch9
Handle hoReceived
String sActualRange
String sActualEtag
String sReceivedLength
Handle hoExpectedRange
Boolean iStringMatch10
Boolean iStringMatch11
Boolean iStringMatch12
Boolean iStringMatch13
Boolean iStringMatch14
Boolean iStringMatch15
String sActualSize
Boolean iStringMatch16
String sTemp1
Integer iTemp1
Integer iTemp2
Boolean bTemp1
Boolean bTemp2
Boolean bTemp3
Move False To iSuccess
Get Create (RefClass(cComChilkatStringBuilder)) To hoStringCompare
If (Not(IsComObjectCreated(hoStringCompare))) Begin
Send CreateComObject of hoStringCompare
End
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Load private credentials; never put access keys or session tokens in source.
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 S3 credentials."
Procedure_Return
End
Get ComStringOf Of hoCredentials "access_key" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch1
Get ComJsonTypeOf Of hoCredentials "access_key" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch1 = True)) Begin
Showln "Missing or invalid access_key."
Procedure_Return
End
Get ComStringOf Of hoCredentials "secret_key" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch2
Get ComJsonTypeOf Of hoCredentials "secret_key" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch2 = True)) Begin
Showln "Missing or invalid secret_key."
Procedure_Return
End
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Get ComStringOf Of hoCredentials "access_key" To sTemp1
Set ComAwsAccessKey Of hoHttp To sTemp1
Get ComStringOf Of hoCredentials "secret_key" To sTemp1
Set ComAwsSecretKey Of hoHttp To sTemp1
Get ComHasMember Of hoCredentials "session_token" To bTemp1
If (bTemp1 = True) Begin
Get ComStringOf Of hoCredentials "session_token" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch3
Get ComJsonTypeOf Of hoCredentials "session_token" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch3 = True)) Begin
Showln "session_token must be a nonempty string when supplied."
Procedure_Return
End
Get ComStringOf Of hoCredentials "session_token" To sTemp1
Set ComAwsSessionToken Of hoHttp To sTemp1
End
// Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
// Set the actual bucket region, not the caller's location.
Move "us-west-2" To sRegion
Set ComAwsRegion Of hoHttp To sRegion
Get Create (RefClass(cComChilkatStringBuilder)) To hoEndpoint
If (Not(IsComObjectCreated(hoEndpoint))) Begin
Send CreateComObject of hoEndpoint
End
Get ComAppend Of hoEndpoint "s3." To iSuccess
Get ComAppend Of hoEndpoint sRegion To iSuccess
Get ComAppend Of hoEndpoint ".amazonaws.com" To iSuccess
Get ComGetAsString Of hoEndpoint To sTemp1
Set ComAwsEndpoint Of hoHttp To sTemp1
Set ComRequireSslCertVerify Of hoHttp To True
Set ComFollowRedirects Of hoHttp To False
// A region redirect is not success. Correct the endpoint and signing region.
Move "your-bucket-name" To sBucketName
Move "examples/archive.bin" To sObjectKey
Get Create (RefClass(cComChilkatHttpResponse)) To hoResponse
If (Not(IsComObjectCreated(hoResponse))) Begin
Send CreateComObject of hoResponse
End
Get Create (RefClass(cComChilkatCrypt2)) To hoSigner
If (Not(IsComObjectCreated(hoSigner))) Begin
Send CreateComObject of hoSigner
End
Set ComCharset Of hoSigner To "utf-8"
Set ComHashAlgorithm Of hoSigner To "sha256"
Set ComEncodingMode Of hoSigner To "hexlower"
Set ComMacAlgorithm Of hoSigner To "hmac"
Get Create (RefClass(cComCkDateTime)) To hoNow
If (Not(IsComObjectCreated(hoNow))) Begin
Send CreateComObject of hoNow
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoTimestamp
If (Not(IsComObjectCreated(hoTimestamp))) Begin
Send CreateComObject of hoTimestamp
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoCanonical
If (Not(IsComObjectCreated(hoCanonical))) Begin
Send CreateComObject of hoCanonical
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoCanonicalHeaders
If (Not(IsComObjectCreated(hoCanonicalHeaders))) Begin
Send CreateComObject of hoCanonicalHeaders
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoSignedHeaders
If (Not(IsComObjectCreated(hoSignedHeaders))) Begin
Send CreateComObject of hoSignedHeaders
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoScope
If (Not(IsComObjectCreated(hoScope))) Begin
Send CreateComObject of hoScope
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoStringToSign
If (Not(IsComObjectCreated(hoStringToSign))) Begin
Send CreateComObject of hoStringToSign
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoSigningKey
If (Not(IsComObjectCreated(hoSigningKey))) Begin
Send CreateComObject of hoSigningKey
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoAuthorization
If (Not(IsComObjectCreated(hoAuthorization))) Begin
Send CreateComObject of hoAuthorization
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoUrl
If (Not(IsComObjectCreated(hoUrl))) Begin
Send CreateComObject of hoUrl
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoQuery
If (Not(IsComObjectCreated(hoQuery))) Begin
Send CreateComObject of hoQuery
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoNumber
If (Not(IsComObjectCreated(hoNumber))) Begin
Send CreateComObject of hoNumber
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoHost
If (Not(IsComObjectCreated(hoHost))) Begin
Send CreateComObject of hoHost
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoUri
If (Not(IsComObjectCreated(hoUri))) Begin
Send CreateComObject of hoUri
End
Get Create (RefClass(cComChilkatXml)) To hoXml
If (Not(IsComObjectCreated(hoXml))) Begin
Send CreateComObject of hoXml
End
Get ComAppend Of hoHost sBucketName To iSuccess
Get ComAppend Of hoHost "." To iSuccess
Get ComGetAsString Of hoEndpoint To sTemp1
Get ComAppend Of hoHost sTemp1 To iSuccess
// Encode the original key exactly once, preserving S3 path separators.
Get ComUrlEncode Of hoHttp sObjectKey To sTemp1
Get ComSetString Of hoUri sTemp1 To iSuccess
Get ComReplaceNoCase Of hoUri "%2f" "/" To iReplaceCount
Get ComStartsWith Of hoUri "/" True To bTemp1
Get ComContains Of hoUri "/../" True To bTemp2
Get ComContains Of hoUri "/./" True To bTemp3
If ((bTemp1 = True) Or (bTemp2 = True) Or (bTemp3 = True)) Begin
Showln "Use a key without a leading slash or dot path segments for these examples."
Procedure_Return
End
Get ComPrepend Of hoUri "/" To iSuccess
Move "" To sVersionId
Get Create (RefClass(cComCkFileAccess)) To hoFiles
If (Not(IsComObjectCreated(hoFiles))) Begin
Send CreateComObject of hoFiles
End
Move "qa_output/s3-download.bin" To sFinalPath
Move "qa_output/s3-download.bin.part" To sPartialPath
Get ComFileExists Of hoFiles sFinalPath To bTemp1
Get ComFileExists Of hoFiles sPartialPath To bTemp2
If ((bTemp1 = True) Or (bTemp2 = True)) Begin
Showln "Choose unused output paths."
Procedure_Return
End
Send ComClear To hoQuery
Get ComSetString Of hoStringCompare sVersionId To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch4
If (iStringMatch4 <> True) Begin
Get ComLength Of hoQuery To iTemp1
If (iTemp1 > 0) Begin
Get ComAppend Of hoQuery "&" To iSuccess
End
Get ComAppend Of hoQuery "versionId=" To iSuccess
Get ComUrlEncode Of hoHttp sVersionId To sTemp1
Get ComAppend Of hoQuery sTemp1 To iSuccess
End
Send ComClearHeaders To hoHttp
Get ComSetFromCurrentSystemTime Of hoNow To iSuccess
If (iSuccess <> True) Begin
Showln "Could not read system time."
Procedure_Return
End
Get ComGetAsTimestamp Of hoNow False To sTemp1
Get ComSetString Of hoTimestamp sTemp1 To iSuccess
Get ComReplace Of hoTimestamp "-" "" To iReplaceCount
Get ComReplace Of hoTimestamp ":" "" To iReplaceCount
Get ComGetAsString Of hoTimestamp To sAmzDate
Get ComGetRange Of hoTimestamp 0 8 False To sDateStamp
Get ComHashStringENC Of hoSigner "" To sPayloadHash
Get ComLastMethodSuccess Of hoSigner To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not hash payload."
Procedure_Return
End
Send ComClear To hoCanonicalHeaders
Send ComClear To hoSignedHeaders
Get ComLength Of hoSignedHeaders To iTemp1
If (iTemp1 > 0) Begin
Get ComAppend Of hoSignedHeaders ";" To iSuccess
End
Get ComAppend Of hoSignedHeaders "host" To iSuccess
Get ComAppend Of hoCanonicalHeaders "host:" To iSuccess
Get ComGetAsString Of hoHost To sTemp1
Get ComAppend Of hoCanonicalHeaders sTemp1 To iSuccess
Get ComAppend Of hoCanonicalHeaders (character(10)) To iSuccess
Get ComGetAsString Of hoHost To sTemp1
Send ComSetRequestHeader To hoHttp "host" sTemp1
Get ComLength Of hoSignedHeaders To iTemp1
If (iTemp1 > 0) Begin
Get ComAppend Of hoSignedHeaders ";" To iSuccess
End
Get ComAppend Of hoSignedHeaders "x-amz-content-sha256" To iSuccess
Get ComAppend Of hoCanonicalHeaders "x-amz-content-sha256:" To iSuccess
Get ComAppend Of hoCanonicalHeaders sPayloadHash To iSuccess
Get ComAppend Of hoCanonicalHeaders (character(10)) To iSuccess
Send ComSetRequestHeader To hoHttp "x-amz-content-sha256" sPayloadHash
Get ComLength Of hoSignedHeaders To iTemp1
If (iTemp1 > 0) Begin
Get ComAppend Of hoSignedHeaders ";" To iSuccess
End
Get ComAppend Of hoSignedHeaders "x-amz-date" To iSuccess
Get ComAppend Of hoCanonicalHeaders "x-amz-date:" To iSuccess
Get ComAppend Of hoCanonicalHeaders sAmzDate To iSuccess
Get ComAppend Of hoCanonicalHeaders (character(10)) To iSuccess
Send ComSetRequestHeader To hoHttp "x-amz-date" sAmzDate
Get ComHasMember Of hoCredentials "session_token" To bTemp1
If (bTemp1 = True) Begin
Get ComLength Of hoSignedHeaders To iTemp1
If (iTemp1 > 0) Begin
Get ComAppend Of hoSignedHeaders ";" To iSuccess
End
Get ComAppend Of hoSignedHeaders "x-amz-security-token" To iSuccess
Get ComAppend Of hoCanonicalHeaders "x-amz-security-token:" To iSuccess
Get ComStringOf Of hoCredentials "session_token" To sTemp1
Get ComAppend Of hoCanonicalHeaders sTemp1 To iSuccess
Get ComAppend Of hoCanonicalHeaders (character(10)) To iSuccess
Get ComStringOf Of hoCredentials "session_token" To sTemp1
Send ComSetRequestHeader To hoHttp "x-amz-security-token" sTemp1
End
Send ComClear To hoCanonical
Get ComAppend Of hoCanonical "HEAD" + (character(10)) To iSuccess
Get ComGetAsString Of hoUri To sTemp1
Get ComAppend Of hoCanonical sTemp1 To iSuccess
Get ComAppend Of hoCanonical (character(10)) To iSuccess
Get ComGetAsString Of hoQuery To sTemp1
Get ComAppend Of hoCanonical sTemp1 To iSuccess
Get ComAppend Of hoCanonical (character(10)) To iSuccess
Get ComGetAsString Of hoCanonicalHeaders To sTemp1
Get ComAppend Of hoCanonical sTemp1 To iSuccess
Get ComAppend Of hoCanonical (character(10)) To iSuccess
Get ComGetAsString Of hoSignedHeaders To sTemp1
Get ComAppend Of hoCanonical sTemp1 To iSuccess
Get ComAppend Of hoCanonical (character(10)) To iSuccess
Get ComAppend Of hoCanonical sPayloadHash To iSuccess
Get ComGetAsString Of hoCanonical To sTemp1
Get ComHashStringENC Of hoSigner sTemp1 To sCanonicalHash
Get ComLastMethodSuccess Of hoSigner To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not hash canonical request."
Procedure_Return
End
Send ComClear To hoScope
Get ComAppend Of hoScope sDateStamp To iSuccess
Get ComAppend Of hoScope "/" To iSuccess
Get ComAppend Of hoScope sRegion To iSuccess
Get ComAppend Of hoScope "/s3/aws4_request" To iSuccess
Send ComClear To hoStringToSign
Get ComAppend Of hoStringToSign "AWS4-HMAC-SHA256" + (character(10)) To iSuccess
Get ComAppend Of hoStringToSign sAmzDate To iSuccess
Get ComAppend Of hoStringToSign (character(10)) To iSuccess
Get ComGetAsString Of hoScope To sTemp1
Get ComAppend Of hoStringToSign sTemp1 To iSuccess
Get ComAppend Of hoStringToSign (character(10)) To iSuccess
Get ComAppend Of hoStringToSign sCanonicalHash To iSuccess
Get ComSetString Of hoSigningKey "AWS4" To iSuccess
Get ComStringOf Of hoCredentials "secret_key" To sTemp1
Get ComAppend Of hoSigningKey sTemp1 To iSuccess
Get ComGetAsString Of hoSigningKey To sTemp1
Get ComSetMacKeyString Of hoSigner sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set signing key."
Procedure_Return
End
Get ComMacStringENC Of hoSigner sDateStamp To sDerivedKey
Get ComLastMethodSuccess Of hoSigner To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not derive signing key."
Procedure_Return
End
Get ComSetMacKeyEncoded Of hoSigner sDerivedKey "hex" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set signing key."
Procedure_Return
End
Get ComMacStringENC Of hoSigner sRegion To sDerivedKey
Get ComLastMethodSuccess Of hoSigner To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not derive signing key."
Procedure_Return
End
Get ComSetMacKeyEncoded Of hoSigner sDerivedKey "hex" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set signing key."
Procedure_Return
End
Get ComMacStringENC Of hoSigner "s3" To sDerivedKey
Get ComLastMethodSuccess Of hoSigner To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not derive signing key."
Procedure_Return
End
Get ComSetMacKeyEncoded Of hoSigner sDerivedKey "hex" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set signing key."
Procedure_Return
End
Get ComMacStringENC Of hoSigner "aws4_request" To sDerivedKey
Get ComLastMethodSuccess Of hoSigner To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not derive signing key."
Procedure_Return
End
Get ComSetMacKeyEncoded Of hoSigner sDerivedKey "hex" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set signing key."
Procedure_Return
End
Get ComGetAsString Of hoStringToSign To sTemp1
Get ComMacStringENC Of hoSigner sTemp1 To sSignature
Get ComLastMethodSuccess Of hoSigner To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not sign request."
Procedure_Return
End
Send ComClear To hoAuthorization
Get ComAppend Of hoAuthorization "AWS4-HMAC-SHA256 Credential=" To iSuccess
Get ComStringOf Of hoCredentials "access_key" To sTemp1
Get ComAppend Of hoAuthorization sTemp1 To iSuccess
Get ComAppend Of hoAuthorization "/" To iSuccess
Get ComGetAsString Of hoScope To sTemp1
Get ComAppend Of hoAuthorization sTemp1 To iSuccess
Get ComAppend Of hoAuthorization ", SignedHeaders=" To iSuccess
Get ComGetAsString Of hoSignedHeaders To sTemp1
Get ComAppend Of hoAuthorization sTemp1 To iSuccess
Get ComAppend Of hoAuthorization ", Signature=" To iSuccess
Get ComAppend Of hoAuthorization sSignature To iSuccess
Get ComGetAsString Of hoAuthorization To sTemp1
Send ComSetRequestHeader To hoHttp "Authorization" sTemp1
Get ComSetString Of hoUrl "https://" To iSuccess
Get ComGetAsString Of hoHost To sTemp1
Get ComAppend Of hoUrl sTemp1 To iSuccess
Get ComGetAsString Of hoUri To sTemp1
Get ComAppend Of hoUrl sTemp1 To iSuccess
Get ComLength Of hoQuery To iTemp1
If (iTemp1 > 0) Begin
Get ComAppend Of hoUrl "?" To iSuccess
Get ComGetAsString Of hoQuery To sTemp1
Get ComAppend Of hoUrl sTemp1 To iSuccess
End
Get ComGetAsString Of hoUrl To sTemp1
Get pvComObject of hoResponse to vResponse
Get ComHttpNoBody Of hoHttp "HEAD" sTemp1 vResponse To iSuccess
Get ComStatusCode Of hoResponse To iTemp1
If ((iSuccess <> True) Or (iTemp1 <> 200)) Begin
Get ComStatusCode Of hoResponse To iTemp1
Showln "Request failed or returned unexpected HTTP status: " iTemp1
// Preserve progress. Inspect full diagnostics privately; do not log secrets.
Procedure_Return
End
Get ComGetHeaderField Of hoResponse "ETag" To sEtag
Get ComSetString Of hoStringCompare sEtag To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch5
Get ComLastMethodSuccess Of hoResponse To bTemp1
If ((bTemp1 <> True) Or (iStringMatch5 = True)) Begin
Showln "Missing required response header ETag."
Procedure_Return
End
Get ComGetHeaderField Of hoResponse "Content-Length" To sExpectedSize
Get ComSetString Of hoStringCompare sExpectedSize To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch6
Get ComLastMethodSuccess Of hoResponse To bTemp1
If ((bTemp1 <> True) Or (iStringMatch6 = True)) Begin
Showln "Missing required response header Content-Length."
Procedure_Return
End
Get ComGetHeaderField Of hoResponse "x-amz-version-id" To sReturnedVersion
Get ComLastMethodSuccess Of hoResponse To bTemp1
If (bTemp1 <> True) Begin
Move "" To sReturnedVersion
End
Get ComSetString Of hoStringCompare sVersionId To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch7
If (iStringMatch7 = True) Begin
Move sReturnedVersion To sVersionId
End
Else Begin
Get ComSetString Of hoStringCompare sReturnedVersion To iSuccess
Get ComContentsEqual Of hoStringCompare sVersionId True To iStringMatch8
If (iStringMatch8 <> True) Begin
Showln "Unexpected object version."
Procedure_Return
End
End
// Download the first 1 MiB of an object containing at least that many bytes.
Get ComSetString Of hoNumber sExpectedSize To iSuccess
Get ComLength Of hoNumber To iTemp1
Get ComIntValue Of hoNumber To iTemp2
If ((iTemp1 < 8) And (iTemp2 < 1048576)) Begin
Showln "Choose an object of at least 1 MiB for this fixed range example."
Procedure_Return
End
Send ComClear To hoQuery
Get ComSetString Of hoStringCompare sVersionId To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch9
If (iStringMatch9 <> True) Begin
Get ComLength Of hoQuery To iTemp1
If (iTemp1 > 0) Begin
Get ComAppend Of hoQuery "&" To iSuccess
End
Get ComAppend Of hoQuery "versionId=" To iSuccess
Get ComUrlEncode Of hoHttp sVersionId To sTemp1
Get ComAppend Of hoQuery sTemp1 To iSuccess
End
Send ComClearHeaders To hoHttp
Get ComSetFromCurrentSystemTime Of hoNow To iSuccess
If (iSuccess <> True) Begin
Showln "Could not read system time."
Procedure_Return
End
Get ComGetAsTimestamp Of hoNow False To sTemp1
Get ComSetString Of hoTimestamp sTemp1 To iSuccess
Get ComReplace Of hoTimestamp "-" "" To iReplaceCount
Get ComReplace Of hoTimestamp ":" "" To iReplaceCount
Get ComGetAsString Of hoTimestamp To sAmzDate
Get ComGetRange Of hoTimestamp 0 8 False To sDateStamp
Get ComHashStringENC Of hoSigner "" To sPayloadHash
Get ComLastMethodSuccess Of hoSigner To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not hash payload."
Procedure_Return
End
Send ComClear To hoCanonicalHeaders
Send ComClear To hoSignedHeaders
Get ComLength Of hoSignedHeaders To iTemp1
If (iTemp1 > 0) Begin
Get ComAppend Of hoSignedHeaders ";" To iSuccess
End
Get ComAppend Of hoSignedHeaders "host" To iSuccess
Get ComAppend Of hoCanonicalHeaders "host:" To iSuccess
Get ComGetAsString Of hoHost To sTemp1
Get ComAppend Of hoCanonicalHeaders sTemp1 To iSuccess
Get ComAppend Of hoCanonicalHeaders (character(10)) To iSuccess
Get ComGetAsString Of hoHost To sTemp1
Send ComSetRequestHeader To hoHttp "host" sTemp1
Get ComLength Of hoSignedHeaders To iTemp1
If (iTemp1 > 0) Begin
Get ComAppend Of hoSignedHeaders ";" To iSuccess
End
Get ComAppend Of hoSignedHeaders "if-match" To iSuccess
Get ComAppend Of hoCanonicalHeaders "if-match:" To iSuccess
Get ComAppend Of hoCanonicalHeaders sEtag To iSuccess
Get ComAppend Of hoCanonicalHeaders (character(10)) To iSuccess
Send ComSetRequestHeader To hoHttp "if-match" sEtag
Get ComLength Of hoSignedHeaders To iTemp1
If (iTemp1 > 0) Begin
Get ComAppend Of hoSignedHeaders ";" To iSuccess
End
Get ComAppend Of hoSignedHeaders "range" To iSuccess
Get ComAppend Of hoCanonicalHeaders "range:" To iSuccess
Get ComAppend Of hoCanonicalHeaders "bytes=0-1048575" To iSuccess
Get ComAppend Of hoCanonicalHeaders (character(10)) To iSuccess
Send ComSetRequestHeader To hoHttp "range" "bytes=0-1048575"
Get ComLength Of hoSignedHeaders To iTemp1
If (iTemp1 > 0) Begin
Get ComAppend Of hoSignedHeaders ";" To iSuccess
End
Get ComAppend Of hoSignedHeaders "x-amz-content-sha256" To iSuccess
Get ComAppend Of hoCanonicalHeaders "x-amz-content-sha256:" To iSuccess
Get ComAppend Of hoCanonicalHeaders sPayloadHash To iSuccess
Get ComAppend Of hoCanonicalHeaders (character(10)) To iSuccess
Send ComSetRequestHeader To hoHttp "x-amz-content-sha256" sPayloadHash
Get ComLength Of hoSignedHeaders To iTemp1
If (iTemp1 > 0) Begin
Get ComAppend Of hoSignedHeaders ";" To iSuccess
End
Get ComAppend Of hoSignedHeaders "x-amz-date" To iSuccess
Get ComAppend Of hoCanonicalHeaders "x-amz-date:" To iSuccess
Get ComAppend Of hoCanonicalHeaders sAmzDate To iSuccess
Get ComAppend Of hoCanonicalHeaders (character(10)) To iSuccess
Send ComSetRequestHeader To hoHttp "x-amz-date" sAmzDate
Get ComHasMember Of hoCredentials "session_token" To bTemp1
If (bTemp1 = True) Begin
Get ComLength Of hoSignedHeaders To iTemp1
If (iTemp1 > 0) Begin
Get ComAppend Of hoSignedHeaders ";" To iSuccess
End
Get ComAppend Of hoSignedHeaders "x-amz-security-token" To iSuccess
Get ComAppend Of hoCanonicalHeaders "x-amz-security-token:" To iSuccess
Get ComStringOf Of hoCredentials "session_token" To sTemp1
Get ComAppend Of hoCanonicalHeaders sTemp1 To iSuccess
Get ComAppend Of hoCanonicalHeaders (character(10)) To iSuccess
Get ComStringOf Of hoCredentials "session_token" To sTemp1
Send ComSetRequestHeader To hoHttp "x-amz-security-token" sTemp1
End
Send ComClear To hoCanonical
Get ComAppend Of hoCanonical "GET" + (character(10)) To iSuccess
Get ComGetAsString Of hoUri To sTemp1
Get ComAppend Of hoCanonical sTemp1 To iSuccess
Get ComAppend Of hoCanonical (character(10)) To iSuccess
Get ComGetAsString Of hoQuery To sTemp1
Get ComAppend Of hoCanonical sTemp1 To iSuccess
Get ComAppend Of hoCanonical (character(10)) To iSuccess
Get ComGetAsString Of hoCanonicalHeaders To sTemp1
Get ComAppend Of hoCanonical sTemp1 To iSuccess
Get ComAppend Of hoCanonical (character(10)) To iSuccess
Get ComGetAsString Of hoSignedHeaders To sTemp1
Get ComAppend Of hoCanonical sTemp1 To iSuccess
Get ComAppend Of hoCanonical (character(10)) To iSuccess
Get ComAppend Of hoCanonical sPayloadHash To iSuccess
Get ComGetAsString Of hoCanonical To sTemp1
Get ComHashStringENC Of hoSigner sTemp1 To sCanonicalHash
Get ComLastMethodSuccess Of hoSigner To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not hash canonical request."
Procedure_Return
End
Send ComClear To hoScope
Get ComAppend Of hoScope sDateStamp To iSuccess
Get ComAppend Of hoScope "/" To iSuccess
Get ComAppend Of hoScope sRegion To iSuccess
Get ComAppend Of hoScope "/s3/aws4_request" To iSuccess
Send ComClear To hoStringToSign
Get ComAppend Of hoStringToSign "AWS4-HMAC-SHA256" + (character(10)) To iSuccess
Get ComAppend Of hoStringToSign sAmzDate To iSuccess
Get ComAppend Of hoStringToSign (character(10)) To iSuccess
Get ComGetAsString Of hoScope To sTemp1
Get ComAppend Of hoStringToSign sTemp1 To iSuccess
Get ComAppend Of hoStringToSign (character(10)) To iSuccess
Get ComAppend Of hoStringToSign sCanonicalHash To iSuccess
Get ComSetString Of hoSigningKey "AWS4" To iSuccess
Get ComStringOf Of hoCredentials "secret_key" To sTemp1
Get ComAppend Of hoSigningKey sTemp1 To iSuccess
Get ComGetAsString Of hoSigningKey To sTemp1
Get ComSetMacKeyString Of hoSigner sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set signing key."
Procedure_Return
End
Get ComMacStringENC Of hoSigner sDateStamp To sDerivedKey
Get ComLastMethodSuccess Of hoSigner To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not derive signing key."
Procedure_Return
End
Get ComSetMacKeyEncoded Of hoSigner sDerivedKey "hex" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set signing key."
Procedure_Return
End
Get ComMacStringENC Of hoSigner sRegion To sDerivedKey
Get ComLastMethodSuccess Of hoSigner To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not derive signing key."
Procedure_Return
End
Get ComSetMacKeyEncoded Of hoSigner sDerivedKey "hex" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set signing key."
Procedure_Return
End
Get ComMacStringENC Of hoSigner "s3" To sDerivedKey
Get ComLastMethodSuccess Of hoSigner To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not derive signing key."
Procedure_Return
End
Get ComSetMacKeyEncoded Of hoSigner sDerivedKey "hex" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set signing key."
Procedure_Return
End
Get ComMacStringENC Of hoSigner "aws4_request" To sDerivedKey
Get ComLastMethodSuccess Of hoSigner To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not derive signing key."
Procedure_Return
End
Get ComSetMacKeyEncoded Of hoSigner sDerivedKey "hex" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set signing key."
Procedure_Return
End
Get ComGetAsString Of hoStringToSign To sTemp1
Get ComMacStringENC Of hoSigner sTemp1 To sSignature
Get ComLastMethodSuccess Of hoSigner To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not sign request."
Procedure_Return
End
Send ComClear To hoAuthorization
Get ComAppend Of hoAuthorization "AWS4-HMAC-SHA256 Credential=" To iSuccess
Get ComStringOf Of hoCredentials "access_key" To sTemp1
Get ComAppend Of hoAuthorization sTemp1 To iSuccess
Get ComAppend Of hoAuthorization "/" To iSuccess
Get ComGetAsString Of hoScope To sTemp1
Get ComAppend Of hoAuthorization sTemp1 To iSuccess
Get ComAppend Of hoAuthorization ", SignedHeaders=" To iSuccess
Get ComGetAsString Of hoSignedHeaders To sTemp1
Get ComAppend Of hoAuthorization sTemp1 To iSuccess
Get ComAppend Of hoAuthorization ", Signature=" To iSuccess
Get ComAppend Of hoAuthorization sSignature To iSuccess
Get ComGetAsString Of hoAuthorization To sTemp1
Send ComSetRequestHeader To hoHttp "Authorization" sTemp1
Get ComSetString Of hoUrl "https://" To iSuccess
Get ComGetAsString Of hoHost To sTemp1
Get ComAppend Of hoUrl sTemp1 To iSuccess
Get ComGetAsString Of hoUri To sTemp1
Get ComAppend Of hoUrl sTemp1 To iSuccess
Get ComLength Of hoQuery To iTemp1
If (iTemp1 > 0) Begin
Get ComAppend Of hoUrl "?" To iSuccess
Get ComGetAsString Of hoQuery To sTemp1
Get ComAppend Of hoUrl sTemp1 To iSuccess
End
Get ComGetAsString Of hoUrl To sTemp1
Get ComDownload Of hoHttp sTemp1 sPartialPath To iSuccess
Get ComLastStatus Of hoHttp To iTemp1
If (iTemp1 = 412) Begin
Showln "Object changed; range was not downloaded."
Procedure_Return
End
Get ComLastStatus Of hoHttp To iTemp1
If (iTemp1 = 416) Begin
Showln "Requested range is not satisfiable; inspect object size and offsets."
Procedure_Return
End
Get ComLastStatus Of hoHttp To iTemp1
If ((iSuccess <> True) Or (iTemp1 <> 206)) Begin
Get ComLastStatus Of hoHttp To iTemp1
Showln "Request failed or returned unexpected HTTP status: " iTemp1
// Preserve progress. Inspect full diagnostics privately; do not log secrets.
Procedure_Return
End
Get Create (RefClass(cComChilkatMime)) To hoReceived
If (Not(IsComObjectCreated(hoReceived))) Begin
Send CreateComObject of hoReceived
End
Get ComLastResponseHeader Of hoHttp To sTemp1
Get ComLoadMime Of hoReceived sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not parse download headers."
Procedure_Return
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoExpectedRange
If (Not(IsComObjectCreated(hoExpectedRange))) Begin
Send CreateComObject of hoExpectedRange
End
Get ComAppend Of hoExpectedRange "bytes 0-1048575/" To iSuccess
Get ComAppend Of hoExpectedRange sExpectedSize To iSuccess
Get ComGetHeaderField Of hoReceived "Content-Range" To sActualRange
Get ComSetString Of hoStringCompare sActualRange To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch10
Get ComLastMethodSuccess Of hoReceived To bTemp1
If ((bTemp1 <> True) Or (iStringMatch10 = True)) Begin
Showln "Missing download header Content-Range."
Procedure_Return
End
Get ComGetHeaderField Of hoReceived "Content-Length" To sReceivedLength
Get ComSetString Of hoStringCompare sReceivedLength To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch11
Get ComLastMethodSuccess Of hoReceived To bTemp1
If ((bTemp1 <> True) Or (iStringMatch11 = True)) Begin
Showln "Missing download header Content-Length."
Procedure_Return
End
Get ComGetHeaderField Of hoReceived "ETag" To sActualEtag
Get ComSetString Of hoStringCompare sActualEtag To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch12
Get ComLastMethodSuccess Of hoReceived To bTemp1
If ((bTemp1 <> True) Or (iStringMatch12 = True)) Begin
Showln "Missing download header ETag."
Procedure_Return
End
Get ComSetString Of hoStringCompare sActualRange To iSuccess
Get ComGetAsString Of hoExpectedRange To sTemp1
Get ComContentsEqual Of hoStringCompare sTemp1 True To iStringMatch13
Get ComSetString Of hoStringCompare sReceivedLength To iSuccess
Get ComContentsEqual Of hoStringCompare "1048576" True To iStringMatch14
Get ComSetString Of hoStringCompare sActualEtag To iSuccess
Get ComContentsEqual Of hoStringCompare sEtag True To iStringMatch15
If ((iStringMatch13 <> True) Or (iStringMatch14 <> True) Or (iStringMatch15 <> True)) Begin
Showln "Range response does not match the requested object bytes."
Procedure_Return
End
Get ComFileSizeStr Of hoFiles sPartialPath To sActualSize
Get ComSetString Of hoStringCompare sActualSize To iSuccess
Get ComContentsEqual Of hoStringCompare "1048576" True To iStringMatch16
Get ComLastMethodSuccess Of hoFiles To bTemp1
If ((bTemp1 <> True) Or (iStringMatch16 <> True)) Begin
Showln "Range byte count mismatch."
Procedure_Return
End
Get ComFileRename Of hoFiles sPartialPath sFinalPath To iSuccess
If (iSuccess <> True) Begin
Showln "Range remains at the temporary path."
Procedure_Return
End
Showln "Range download succeeded; byte count checked."
// A whole-object checksum cannot be used to verify this partial range.
End_Procedure