Amazon Glacier: Retry Multipart Completion
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.
Uses fixed 8 MiB parts, at most 10,000 parts (78.125 GiB), and buffers one part at a time. The API also allows other power-of-two MiB sizes up to 4 GiB; this portable example deliberately fixes the size. The last part may be smaller. Use a nonempty, immutable file and one writer per upload. Create qa_output first. Byte offsets and archive sizes stay exact in languages with 32-bit integers.
The private checkpoint stores the upload ID, region, vault, file path, size, part size, and full-file tree hash. Preserve it across failures. It contains no AWS credentials. A separate completion receipt records the archive ID. A lost initiation response requires inspecting List Multipart Uploads; starting a new upload is not a retry of the old one. Inactive uploads can expire after 24 hours. Do not abort automatically on transient errors.
Use only after all parts were uploaded and a previous completion response was lost. Sends the identical upload ID, total size, and complete tree hash from the checkpoint, without listing parts or re-uploading bytes. AWS permits idempotent completion for a short period and returns the same archive ID. After completion, List Parts and List Multipart Uploads no longer expose the upload. This example deliberately does not require the local source file. A later 404 cannot prove whether the archive was completed; preserve the checkpoint and investigate your archive catalog/inventory.
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 files = chilkat.FileAccess.new()
local statePath = "qa_output/glacier-multipart.json"
local receiptPath = "qa_output/glacier-multipart-completed.json"
local state = chilkat.JsonObject.new()
state.strict = true
local saved = chilkat.StringBuilder.new()
success = state:load_file(statePath)
if not success then
print("Could not load the private multipart checkpoint.")
return
end
if state:json_type_of("UploadId") ~= 1 then
print("Missing or invalid UploadId.")
return
end
if state:string_of("UploadId") == "" then
print("Empty UploadId.")
return
end
if state:json_type_of("Region") ~= 1 then
print("Missing or invalid Region.")
return
end
if state:string_of("Region") == "" then
print("Empty Region.")
return
end
if state:json_type_of("VaultName") ~= 1 then
print("Missing or invalid VaultName.")
return
end
if state:string_of("VaultName") == "" then
print("Empty VaultName.")
return
end
if state:json_type_of("FilePath") ~= 1 then
print("Missing or invalid FilePath.")
return
end
if state:string_of("FilePath") == "" then
print("Empty FilePath.")
return
end
if state:json_type_of("ArchiveSize") ~= 1 then
print("Missing or invalid ArchiveSize.")
return
end
if state:string_of("ArchiveSize") == "" then
print("Empty ArchiveSize.")
return
end
if state:json_type_of("TreeHash") ~= 1 then
print("Missing or invalid TreeHash.")
return
end
if state:string_of("TreeHash") == "" then
print("Empty TreeHash.")
return
end
if state:json_type_of("PartSize") ~= 1 then
print("Missing or invalid PartSize.")
return
end
if state:string_of("PartSize") == "" then
print("Empty PartSize.")
return
end
if (state:string_of("Region") ~= region) or (state:string_of("VaultName") ~= "chilkat") then
print("Checkpoint region/vault differs from this example configuration.")
return
end
if state:string_of("PartSize") ~= "8388608" then
print("This workflow requires the original 8 MiB part size.")
return
end
local uploadId = state:string_of("UploadId")
success = rest:add_path_param("{upload}", uploadId)
if not success then
print("Could not set upload ID.")
return
end
if files:file_exists(receiptPath) then
print("A completion receipt already exists; read its archive ID.")
return
end
success = rest:add_header("x-amz-sha256-tree-hash", state:string_of("TreeHash"))
if not success then
print("Could not set x-amz-sha256-tree-hash.")
return
end
success = rest:add_header("x-amz-archive-size", state:string_of("ArchiveSize"))
if not success then
print("Could not set x-amz-archive-size.")
return
end
-- Completion has NO body: never reuse a part's precomputed payload hash.
aws.precomputed_sha256 = ""
success = rest:set_auth_aws(aws)
if not success then
print("Could not reset signing.")
return
end
success = rest:remove_header("x-amz-content-sha256")
if not success then
print("Could not clear part payload hash.")
return
end
success = rest:remove_header("Content-Range")
if not success then
print("Could not clear part range.")
return
end
success = rest:remove_header("Content-Type")
if not success then
print("Could not clear content type.")
return
end
success = rest:full_request_no_body_sb("POST", "/-/vaults/{vault}/multipart-uploads/{upload}", responseBody)
if not success then
print("Request did not complete; inspect diagnostics privately. Check state before retrying a change.")
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("Completion response has no archive ID. Preserve the checkpoint and retry identical completion promptly.")
return
end
print("Archive ID: " .. archiveId)
local location = rest:response_hdr_by_name("Location")
print("Location: " .. location)
local returnedHash = rest:response_hdr_by_name("x-amz-sha256-tree-hash")
if (rest.last_method_success) and (returnedHash ~= state:string_of("TreeHash")) then
print("Completion checksum mismatch; preserve the printed archive ID and investigate.")
return
end
local receipt = chilkat.JsonObject.new()
success = receipt:update_string("UploadId", uploadId)
if not success then
print("Could not set UploadId.")
return
end
success = receipt:update_string("ArchiveId", archiveId)
if not success then
print("Could not set ArchiveId.")
return
end
success = receipt:update_string("Location", location)
if not success then
print("Could not set Location.")
return
end
success = receipt:update_string("TreeHash", state:string_of("TreeHash"))
if not success then
print("Could not set TreeHash.")
return
end
saved:clear()
success = receipt:emit_sb(saved)
if not success then
print("Could not serialize receipt; preserve the printed archive ID.")
return
end
success = saved:write_file(receiptPath, "utf-8", false)
if not success then
print("Could not save receipt; preserve the printed archive ID.")
return
end
print("Completion receipt saved. Keep it in your archive catalog.")