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

Amazon Glacier: Create Vault

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.

Creates a vault for an eligible existing customer. The returned Location identifies the vault.

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 Swift Downloads

Swift

func chilkatTest() {
    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    var success: Bool = false
    //  Private configuration, not a file in public web content or source control.
    //  Load current IAM credentials authorized for the Glacier operation.
    let credentials = CkoJsonObject()!
    credentials.strict = true
    success = credentials.loadFile(path: "qa_data/aws/glacier.json")
    if success != true {
        print("Could not load private AWS credentials.")
        return
    }

    if (credentials.jsonType(of: "access_key").intValue != 1) || (credentials.string(of: "access_key") == "") {
        print("Missing or invalid access_key.")
        return
    }

    if (credentials.jsonType(of: "secret_key").intValue != 1) || (credentials.string(of: "secret_key") == "") {
        print("Missing or invalid secret_key.")
        return
    }

    //  Set this once to the region containing the vault.
    var region: String? = "us-west-2"
    let endpoint = CkoStringBuilder()!
    endpoint.append(value: "glacier.")
    endpoint.append(value: region)
    endpoint.append(value: ".amazonaws.com")
    let aws = CkoAuthAws()!
    aws.accessKey = credentials.string(of: "access_key")
    aws.secretKey = credentials.string(of: "secret_key")
    aws.region = region
    aws.serviceName = "glacier"
    aws.signatureVersion = 4
    let rest = CkoRest()!
    rest.connectTimeoutMs = 20000
    rest.idleTimeoutMs = 60000
    success = rest.setAuthAws(authProvider: aws)
    if success != true {
        print("Could not configure AWS signing.")
        return
    }

    //  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.hasMember(jsonPath: "session_token") == true {
        if credentials.jsonType(of: "session_token").intValue != 1 {
            print("session_token must be a string.")
            return
        }

        if credentials.string(of: "session_token") != "" {
            success = rest.addQueryParam(name: "X-Amz-Security-Token", value: credentials.string(of: "session_token"))
            if success != true {
                print("Could not set the session-token parameter.")
                return
            }

        }

    }

    //  Supply a Socket to explicitly enable TLS certificate verification.
    let connection = CkoSocket()!
    connection.requireSslCertVerify = true
    connection.maxReadIdleMs = 60000
    connection.maxSendIdleMs = 60000
    success = connection.connect(hostname: endpoint.getAsString(), port: 443, ssl: true, maxWaitMs: 20000)
    if success != true {
        print("TLS connection failed; inspect diagnostics privately.")
        return
    }

    success = rest.useConnection(connection: connection, autoReconnect: true)
    if success != true {
        print("Could not attach the TLS connection.")
        return
    }

    success = rest.addHeader(name: "x-amz-glacier-version", value: "2012-06-01")
    if success != true {
        print("Could not set x-amz-glacier-version.")
        return
    }

    //  A hyphen selects the account belonging to the signing credentials.
    //  Use the original, URL-safe vault name and IDs returned by Glacier.
    success = rest.addPathParam(name: "{vault}", value: "chilkat")
    if success != true {
        print("Could not set vault name.")
        return
    }

    let body = CkoJsonObject()!
    let result = CkoJsonObject()!
    result.strict = true
    let error = CkoJsonObject()!
    error.strict = true
    let requestBody = CkoStringBuilder()!
    let responseBody = CkoStringBuilder()!
    success = rest.fullRequestNoBodySb(httpVerb: "PUT", uriPath: "/-/vaults/{vault}", sb: responseBody)
    if success != true {
        print("Request did not complete; inspect diagnostics privately. Check state before retrying a change.")
        return
    }

    if rest.responseStatusCode.intValue != 201 {
        print("Glacier HTTP status: \(rest.responseStatusCode.intValue)")
        success = error.loadSb(sb: responseBody)
        if success == true {
            if error.jsonType(of: "code").intValue == 1 {
                print("AWS error code: \(error.string(of: "code")!)")
            }

        }

        print("AWS request ID: \(rest.responseHdr(byName: "x-amzn-RequestId")!)")
        //  Inspect full errors privately. An HTTP error is not a successful operation.
        return
    }

    print("Vault location: \(rest.responseHdr(byName: "Location")!)")
    print("Operation succeeded.")

}