Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.6.0+

Amazon Glacier: List Jobs with Pagination and Status Filters

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.

Lists active and recently completed jobs for one vault, with optional statuscode and completed filters. Leave both configuration strings empty to list all statuses. Allowed status values are InProgress, Succeeded, and Failed; completion values are lowercase true and false. Empty means omitted: completed=false explicitly selects unfinished jobs. When both filters are set, both must match; contradictory choices can return an empty result.

Uses limit=50, follows the top-level response Marker even after an empty page, and retains the filters on every request. Raw markers are encoded by AddQueryParam. A repeated, missing, or malformed marker or the 1,000-page bound stops with incomplete results. InventoryRetrievalParameters.Marker inside a job belongs to inventory retrieval and must not be used for job-list pagination.

Prints job IDs, action, status, completion, and available dates, sizes, and tier. Nullable fields are checked before use; sizes stay decimal strings to preserve large integers. A failed job does not stop enumeration. Completed does not necessarily mean succeeded. Use DescribeJob to check a selected job, then download archive output or download CSV inventory output.

Requires glacier:ListJobs on the target vault. Results cover retained jobs, not a permanent job history, and pages are not a single consistent snapshot. This can help recover a job ID after an uncertain InitiateJob response; a missing job does not prove the request failed. The final count is records listed, not a deduplicated count of unique jobs.

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.

AWS reference

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- Optional filters. An empty string means OMIT the filter, not false.
-- statusFilter: "", "InProgress", "Succeeded", or "Failed" (case-sensitive).
-- completedFilter: "", "true", or "false" (lowercase on the wire).
-- Examples: "Failed" + "true" lists failed completed jobs;
-- "" + "true" includes both succeeded and failed completed jobs.
local statusFilter = ""
local completedFilter = ""
if (statusFilter ~= "") and ((statusFilter ~= "InProgress") and ((statusFilter ~= "Succeeded") and (statusFilter ~= "Failed"))) then
    print("Invalid status filter. Use InProgress, Succeeded, Failed, or an empty string.")
    return
end

if (completedFilter ~= "") and ((completedFilter ~= "true") and (completedFilter ~= "false")) then
    print("Invalid completion filter. Use true, false, or an empty string.")
    return
end

-- 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 result = chilkat.JsonObject.new()
result.strict = true
local error_ = chilkat.JsonObject.new()
error_.strict = true
local responseBody = chilkat.StringBuilder.new()
-- Both filters, if supplied, apply together. Keep them on every page.
if statusFilter ~= "" then
    success = rest:add_query_param("statuscode", statusFilter)
    if not success then
        print("Could not set statuscode filter.")
        return
    end
end

if completedFilter ~= "" then
    success = rest:add_query_param("completed", completedFilter)
    if not success then
        print("Could not set completed filter.")
        return
    end
end

local recordsListed = 0
success = rest:add_query_param("limit", "50")
if not success then
    print("Could not set page limit.")
    return
end

local seenMarkers = chilkat.Hashtable.new()
local marker = ""
local more = true
local page = 0
while more do
    if page == 1000 then
        print("Page limit reached; listing is incomplete.")
        return
    end

    page = page + 1
    if marker ~= "" then
        success = rest:add_query_param("marker", marker)
        if not success then
            print("Could not set marker.")
            return
        end
    end

    success = rest:full_request_no_body_sb("GET", "/-/vaults/{vault}/jobs", 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 ~= 200 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

    success = result:load_sb(responseBody)
    if not success then
        print("Expected valid JSON response.")
        return
    end

    if result:json_type_of("JobList") ~= 4 then
        print("Missing or invalid JobList.")
        return
    end

    local i = 0
    while i < result:size_of_array("JobList") do
        result.i = i
        if result:json_type_of("JobList[i].JobId") ~= 1 then
            print("Missing or invalid JobList[i].JobId.")
            return
        end

        if result:string_of("JobList[i].JobId") == "" then
            print("Empty JobList[i].JobId.")
            return
        end

        if result:json_type_of("JobList[i].Action") ~= 1 then
            print("Missing or invalid JobList[i].Action.")
            return
        end

        if result:string_of("JobList[i].Action") == "" then
            print("Empty JobList[i].Action.")
            return
        end

        if result:json_type_of("JobList[i].StatusCode") ~= 1 then
            print("Missing or invalid JobList[i].StatusCode.")
            return
        end

        if result:string_of("JobList[i].StatusCode") == "" then
            print("Empty JobList[i].StatusCode.")
            return
        end

        if result:json_type_of("JobList[i].Completed") ~= 5 then
            print("Missing or invalid JobList[i].Completed.")
            return
        end

        print("Job ID: " .. result:string_of("JobList[i].JobId"))
        print("Action: " .. result:string_of("JobList[i].Action"))
        print("Status: " .. result:string_of("JobList[i].StatusCode"))
        print("Completed: " .. result:string_of("JobList[i].Completed"))
        if result:json_type_of("JobList[i].CreationDate") == 1 then
            print("Created: " .. result:string_of("JobList[i].CreationDate"))
        end

        if result:json_type_of("JobList[i].CompletionDate") == 1 then
            print("Finished: " .. result:string_of("JobList[i].CompletionDate"))
        end

        if result:json_type_of("JobList[i].ArchiveId") == 1 then
            print("Archive ID: " .. result:string_of("JobList[i].ArchiveId"))
        end

        if result:json_type_of("JobList[i].ArchiveSizeInBytes") == 2 then
            print("Archive bytes: " .. result:string_of("JobList[i].ArchiveSizeInBytes"))
        end

        if result:json_type_of("JobList[i].InventorySizeInBytes") == 2 then
            print("Inventory bytes: " .. result:string_of("JobList[i].InventorySizeInBytes"))
        end

        if result:json_type_of("JobList[i].Tier") == 1 then
            print("Retrieval tier: " .. result:string_of("JobList[i].Tier"))
        end

        -- Completed=true alone does not mean success. Do not download failed jobs.
        if (result:bool_of("JobList[i].Completed")) and (result:string_of("JobList[i].StatusCode") == "Succeeded") then
            print("Job succeeded; retrieve output promptly.")
        elseif result:string_of("JobList[i].StatusCode") == "Failed" then
            print("Job failed. Inspect StatusMessage privately; continue listing other jobs.")
        else
            print("Job is not confirmed successful. Check its status before retrieving output.")
        end

        recordsListed = recordsListed + 1
        print("---")
        i = i + 1
    end

    more = false
    if result:json_type_of("Marker") == 1 then
        marker = result:string_of("Marker")
        if marker == "" then
            print("Empty marker; listing is incomplete.")
            return
        end

        if seenMarkers:contains(marker) then
            print("Repeated marker; listing is incomplete.")
            return
        end

        success = seenMarkers:add_str(marker, "seen")
        if not success then
            print("Could not track pagination.")
            return
        end

        more = true
    elseif result:json_type_of("Marker") ~= 6 then
        print("Missing or invalid Marker; listing is incomplete.")
        return
    end
end

success = rest:remove_query_param("limit")
if not success then
    print("Could not clear limit.")
    return
end

if marker ~= "" then
    success = rest:remove_query_param("marker")
    if not success then
        print("Could not clear marker.")
        return
    end
end

print("Job listing complete. Records listed: " .. recordsListed)