S3: Upload a File with User Metadata
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.
Uploads to the exact object key using a single PutObject request. Existing keys are replaced, or a new version is created when versioning is enabled. Requires s3:PutObject; KMS encryption may need additional key permissions. The bucket's default encryption applies unless explicitly overridden. No ACL header is sent, so the request works with Bucket owner enforced ownership.
This is a single-request upload, not multipart/resumable upload. Use multipart for large objects; the single PUT limit is 5 GB. Supply the original key to objectName; the example encodes it once for the S3 helper while preserving path separators. ETag is not a universal MD5 checksum. After a timeout, inspect object state before retrying a write.
User metadata names are stored in lowercase. The user-defined metadata limit is 2 KB, measured as the UTF-8 byte count of its keys and values; the full PUT request header limit is 8 KB. Use simple ASCII metadata for interoperability. Updating stored user metadata normally requires copying/replacing the object; it is not changed by a later HEAD request.
Chilkat Lua Downloads
local chilkat = require("chilkat")
local stringCompare = chilkat.StringBuilder.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.
local credentials = chilkat.JsonObject.new()
credentials.strict = true
local success = credentials:load_file("qa_data/aws/s3.json")
if not success then
print("Could not load private S3 credentials.")
return
end
stringCompare:set_string(credentials:string_of("access_key"))
local stringMatch1 = stringCompare:contents_equal("", true)
if (credentials:json_type_of("access_key") ~= 1) or (stringMatch1) then
print("Missing or invalid access_key.")
return
end
stringCompare:set_string(credentials:string_of("secret_key"))
local stringMatch2 = stringCompare:contents_equal("", true)
if (credentials:json_type_of("secret_key") ~= 1) or (stringMatch2) then
print("Missing or invalid secret_key.")
return
end
local http = chilkat.Http.new()
http.aws_access_key = credentials:string_of("access_key")
http.aws_secret_key = credentials:string_of("secret_key")
if credentials:has_member("session_token") then
stringCompare:set_string(credentials:string_of("session_token"))
local stringMatch3 = stringCompare:contents_equal("", true)
if (credentials:json_type_of("session_token") ~= 1) or (stringMatch3) then
print("session_token must be a nonempty string when supplied.")
return
end
http.aws_session_token = credentials:string_of("session_token")
end
-- Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
-- Set the actual bucket region, not the caller's location.
local region = "us-west-2"
http.aws_region = region
local endpoint = chilkat.StringBuilder.new()
endpoint:append("s3.")
endpoint:append(region)
endpoint:append(".amazonaws.com")
http.aws_endpoint = endpoint:get_as_string()
http.require_ssl_cert_verify = true
http.follow_redirects = false
-- A region redirect is not success. Correct the endpoint and signing region.
local bucketName = "your-bucket-name"
-- A leading slash is part of a key. Do not add one to imitate a file path.
local objectName = "images/seahorse.jpg"
-- Start with the original key. Encode once and retain '/' path separators.
-- This also protects a literal "%2F" from being interpreted as a slash.
local keyPath = chilkat.StringBuilder.new()
keyPath:append(http:url_encode(objectName))
local slashCount = keyPath:replace_no_case("%2f", "/")
local encodedKey = keyPath:get_as_string()
http:set_request_header("x-amz-meta-genus", "Hippocampus")
http:set_request_header("x-amz-meta-species", "big-belly seahorse")
success = http:s3_upload_file("qa_data/jpg/seahorse.jpg", "image/jpeg", bucketName, encodedKey)
if not success then
print("Request failed (transport or S3 error). HTTP status: " .. http.last_status)
-- Inspect LastErrorText privately. Do not log credentials or signed URLs.
return
end
if http.last_status ~= 200 then
print("Unexpected HTTP status: " .. http.last_status)
return
end
print("File upload succeeded.")