S3: Download UTF-8 Text from a Regional Bucket
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.
Downloads a small text object using the actual bucket region for both signing and endpoint selection. The complete object is held in memory. Empty objects are valid; do not treat an empty result as an error without checking LastMethodSuccess and HTTP status. Requires s3:GetObject and applicable KMS permissions. Use the file download example for binary or large objects.
Chilkat Ruby Downloads
require 'chilkat'
success = false
stringCompare = Chilkat::CkStringBuilder.new()
# 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.
credentials = Chilkat::CkJsonObject.new()
credentials.put_Strict(true)
success = credentials.LoadFile("qa_data/aws/s3.json")
if (success != true)
print "Could not load private S3 credentials." + "\n";
exit
end
stringCompare.SetString(credentials.stringOf("access_key"))
stringMatch1 = stringCompare.ContentsEqual("",true)
if ((credentials.JsonTypeOf("access_key") != 1) or (stringMatch1 == true))
print "Missing or invalid access_key." + "\n";
exit
end
stringCompare.SetString(credentials.stringOf("secret_key"))
stringMatch2 = stringCompare.ContentsEqual("",true)
if ((credentials.JsonTypeOf("secret_key") != 1) or (stringMatch2 == true))
print "Missing or invalid secret_key." + "\n";
exit
end
http = Chilkat::CkHttp.new()
http.put_AwsAccessKey(credentials.stringOf("access_key"))
http.put_AwsSecretKey(credentials.stringOf("secret_key"))
if (credentials.HasMember("session_token") == true)
stringCompare.SetString(credentials.stringOf("session_token"))
stringMatch3 = stringCompare.ContentsEqual("",true)
if ((credentials.JsonTypeOf("session_token") != 1) or (stringMatch3 == true))
print "session_token must be a nonempty string when supplied." + "\n";
exit
end
http.put_AwsSessionToken(credentials.stringOf("session_token"))
end
# Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
# Set the actual bucket region, not the caller's location.
region = "eu-central-1"
http.put_AwsRegion(region)
endpoint = Chilkat::CkStringBuilder.new()
endpoint.Append("s3.")
endpoint.Append(region)
endpoint.Append(".amazonaws.com")
http.put_AwsEndpoint(endpoint.getAsString())
http.put_RequireSslCertVerify(true)
http.put_FollowRedirects(false)
# A region redirect is not success. Correct the endpoint and signing region.
bucketName = "your-bucket-name"
objectName = "hello.txt"
# Start with the original key. Encode once and retain '/' path separators.
# This also protects a literal "%2F" from being interpreted as a slash.
keyPath = Chilkat::CkStringBuilder.new()
keyPath.Append(http.urlEncode(objectName))
slashCount = keyPath.ReplaceNoCase("%2f","/")
encodedKey = keyPath.getAsString()
text = http.s3_DownloadString(bucketName,encodedKey,"utf-8")
if (http.get_LastMethodSuccess() != true)
print "Request failed (transport or S3 error). HTTP status: " + http.get_LastStatus().to_s() + "\n";
# Inspect LastErrorText privately. Do not log credentials or signed URLs.
exit
end
if (http.get_LastStatus() != 200)
print "Unexpected HTTP status: " + http.get_LastStatus().to_s() + "\n";
exit
end
# Empty content is valid. Success is determined by the call and HTTP status.
print text + "\n";