Amazon Glacier: Upload Archive from Memory
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.
Computes both SHA-256 hashes, uploads immutable archive bytes, checks HTTP 201 and the returned tree hash, and prints the archive ID for durable storage in your catalog. A description is optional, must be printable ASCII, and is limited to 1,024 characters; it is not a lookup key. Single-request uploads support up to 4 GiB; use multipart upload for larger archives. This variant buffers data in memory.
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.
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
-- Private configuration, not a file in public web content or source control.
-- Load current IAM credentials authorized for the Glacier operation.
local credentials = chilkat.JsonObject.new()
credentials.strict = true
local success = credentials:load_file("qa_data/aws/glacier.json")
if not success then
print("Could not load private AWS credentials.")
return
end
if (credentials:json_type_of("access_key") ~= 1) or (credentials:string_of("access_key") == "") then
print("Missing or invalid access_key.")
return
end
if (credentials:json_type_of("secret_key") ~= 1) or (credentials:string_of("secret_key") == "") then
print("Missing or invalid secret_key.")
return
end
-- Set this once to the region containing the vault.
local region = "us-west-2"
local endpoint = chilkat.StringBuilder.new()
endpoint:append("glacier.")
endpoint:append(region)
endpoint:append(".amazonaws.com")
local aws = chilkat.AuthAws.new()
aws.access_key = credentials:string_of("access_key")
aws.secret_key = credentials:string_of("secret_key")
aws.region = region
aws.service_name = "glacier"
aws.signature_version = 4
local rest = chilkat.Rest.new()
rest.connect_timeout_ms = 20000
rest.idle_timeout_ms = 60000
success = rest:set_auth_aws(aws)
if not success then
print("Could not configure AWS signing.")
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.
if credentials:has_member("session_token") then
if credentials:json_type_of("session_token") ~= 1 then
print("session_token must be a string.")
return
end
if credentials:string_of("session_token") ~= "" then
success = rest:add_query_param("X-Amz-Security-Token", credentials:string_of("session_token"))
if not success then
print("Could not set the session-token parameter.")
return
end
end
end
-- Supply a Socket to explicitly enable TLS certificate verification.
local connection = chilkat.Socket.new()
connection.require_ssl_cert_verify = true
connection.max_read_idle_ms = 60000
connection.max_send_idle_ms = 60000
success = connection:connect(endpoint:get_as_string(), 443, true, 20000)
if not success then
print("TLS connection failed; inspect diagnostics privately.")
return
end
success = rest:use_connection(connection, true)
if not success then
print("Could not attach the TLS connection.")
return
end
success = rest:add_header("x-amz-glacier-version", "2012-06-01")
if not success then
print("Could not set x-amz-glacier-version.")
return
end
-- A hyphen selects the account belonging to the signing credentials.
-- Use the original, URL-safe vault name and IDs returned by Glacier.
success = rest:add_path_param("{vault}", "chilkat")
if not success then
print("Could not set vault name.")
return
end
local body = chilkat.JsonObject.new()
local result = chilkat.JsonObject.new()
result.strict = true
local error_ = chilkat.JsonObject.new()
error_.strict = true
local requestBody = chilkat.StringBuilder.new()
local responseBody = chilkat.StringBuilder.new()
local data = chilkat.BinData.new()
success = data:load_file("qa_data/jpg/penguins.jpg")
if not success then
print("Could not load archive bytes.")
return
end
-- The BinData can instead be populated directly by your application.
local crypt = chilkat.Crypt2.new()
crypt.hash_algorithm = "sha256-tree-hash"
crypt.encoding_mode = "hexlower"
if data.num_bytes == 0 then
crypt.hash_algorithm = "sha256"
end
local treeHash = crypt:hash_bd_enc(data)
if not crypt.last_method_success then
print("Could not compute tree hash.")
return
end
success = rest:add_header("x-amz-sha256-tree-hash", treeHash)
if not success then
print("Could not set x-amz-sha256-tree-hash.")
return
end
crypt.hash_algorithm = "sha256"
local linearHash = crypt:hash_bd_enc(data)
if not crypt.last_method_success then
print("Could not compute payload hash.")
return
end
aws.precomputed_sha256 = linearHash
-- Set the precomputed hash before attaching the signing configuration again.
success = rest:set_auth_aws(aws)
if not success then
print("Could not update AWS signing.")
return
end
success = rest:add_header("x-amz-content-sha256", linearHash)
if not success then
print("Could not set x-amz-content-sha256.")
return
end
success = rest:add_header("x-amz-archive-description", "Example archive")
if not success then
print("Could not set x-amz-archive-description.")
return
end
success = rest:full_request_bd("POST", "/-/vaults/{vault}/archives", data, responseBody)
if not success then
print("Upload did not complete; check for an accepted archive before retrying.")
return
end
if rest.response_status_code ~= 201 then
print("Glacier HTTP status: " .. rest.response_status_code)
success = error_:load_sb(responseBody)
if success then
if error_:json_type_of("code") == 1 then
print("AWS error code: " .. error_:string_of("code"))
end
end
print("AWS request ID: " .. rest:response_hdr_by_name("x-amzn-RequestId"))
-- Inspect full errors privately. An HTTP error is not a successful operation.
return
end
local archiveId = rest:response_hdr_by_name("x-amz-archive-id")
if (not rest.last_method_success) or (archiveId == "") then
print("Missing archive ID; investigate the accepted upload before retrying.")
return
end
-- Save this ID even if subsequent checksum verification fails.
print("Archive ID: " .. archiveId)
print("Location: " .. rest:response_hdr_by_name("Location"))
local returnedHash = rest:response_hdr_by_name("x-amz-sha256-tree-hash")
if returnedHash ~= treeHash then
print("Upload response checksum mismatch; preserve the archive ID and investigate.")
return
end
print("Archive upload verified. Persist its ID and checksum in your archive catalog.")