Amazon S3: Stream a File Upload with a Precomputed SHA-256
See more S3 - REST Examples
Upload one object with PutObject. Requires s3:PutObject; KMS-encrypted objects can need additional KMS permissions. An existing key is overwritten in an unversioned bucket or gets a new version when versioning is enabled. Successful PUT normally has an empty body. Do not label uncompressed data with Content-Encoding: gzip.
Precompute the SHA-256 in a separate file pass, then stream the unchanged file. This avoids buffering the whole file merely to calculate the signature. Clear PrecomputedSha256 before another request with different bytes. Use multipart upload for objects beyond the single PUT limit (5 GB), or to retry individual parts.
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.
Chilkat Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loTextCompare
LOCAL loCredentials
LOCAL lnSameText1
LOCAL lnSameText2
LOCAL lcRegion
LOCAL lcBucketName
LOCAL loEndpoint
LOCAL loAws
LOCAL loRest
LOCAL lnSameText3
LOCAL loConnection
LOCAL loXml
LOCAL lcObjectKey
LOCAL loObjectPath
LOCAL lnRestoredSlashes
LOCAL lcInputFile
LOCAL loHash
LOCAL lcSha256
LOCAL loInput
LOCAL lcResponseBody
LOCAL lnSameText4
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
lnSuccess = loRest.AddHeader("Content-Type","application/octet-stream")
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
* Keep the file unchanged from hashing until upload finishes.
lcInputFile = "qa_data/files/example.bin"
loHash = CreateObject('Chilkat.Crypt2')
loHash.HashAlgorithm = "sha256"
loHash.EncodingMode = "hexlower"
lcSha256 = loHash.HashFileENC(lcInputFile)
IF (loHash.LastMethodSuccess <> 1) THEN
? "Could not hash the file."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
RELEASE loHash
CANCEL
ENDIF
* Supply the payload hash for SigV4 without loading the whole file into memory.
loAws.PrecomputedSha256 = lcSha256
loInput = CreateObject('Chilkat.Stream')
loInput.SourceFile = lcInputFile
lcResponseBody = loRest.FullRequestStream("PUT",loObjectPath.GetAsString(),loInput)
IF (loRest.LastMethodSuccess <> 1) THEN
? "Upload did not complete; inspect diagnostics privately."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
RELEASE loHash
RELEASE loInput
CANCEL
ENDIF
IF (loRest.ResponseStatusCode <> 200) THEN
? "S3 HTTP status: " + STR(loRest.ResponseStatusCode)
lnSuccess = loXml.LoadXml(lcResponseBody)
loTextCompare.Clear()
loTextCompare.SetString(loXml.Tag)
lnSameText4 = loTextCompare.ContentsEqual("Error",1)
IF ((lnSuccess = 1) AND (lnSameText4 = 1)) THEN
? "S3 error code: " + loXml.GetChildContent("Code")
ENDIF
* Keep signed headers, credentials, URLs, and full diagnostics private.
* A timeout may leave a write's outcome uncertain. Check before retrying.
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
RELEASE loHash
RELEASE loInput
CANCEL
ENDIF
* Clear this before reusing aws for a DIFFERENT request body.
loAws.PrecomputedSha256 = ""
? "Object uploaded."
? "ETag: " + loRest.ResponseHdrByName("ETag")
* ETag is an opaque validator, not a universally valid MD5 checksum.
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
RELEASE loHash
RELEASE loInput