Sample code for 30+ languages & platforms
Unicode C 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 Unicode C Downloads

Unicode C
#include <C_CkJsonObjectW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkAuthAwsW.h>
#include <C_CkRestW.h>
#include <C_CkSocketW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObjectW credentials;
    const wchar_t *region;
    HCkStringBuilderW endpoint;
    HCkAuthAwsW aws;
    HCkRestW rest;
    HCkSocketW connection;
    HCkJsonObjectW body;
    HCkJsonObjectW result;
    HCkJsonObjectW error;
    HCkStringBuilderW requestBody;
    HCkStringBuilderW responseBody;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = FALSE;
    //  Private configuration, not a file in public web content or source control.
    //  Load current IAM credentials authorized for the Glacier operation.
    credentials = CkJsonObjectW_Create();
    CkJsonObjectW_putStrict(credentials,TRUE);
    success = CkJsonObjectW_LoadFile(credentials,L"qa_data/aws/glacier.json");
    if (success != TRUE) {
        wprintf(L"Could not load private AWS credentials.\n");
        CkJsonObjectW_Dispose(credentials);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(credentials,L"access_key") != 1) || (CkJsonObjectW_stringOf(credentials,L"access_key") == L"")) {
        wprintf(L"Missing or invalid access_key.\n");
        CkJsonObjectW_Dispose(credentials);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(credentials,L"secret_key") != 1) || (CkJsonObjectW_stringOf(credentials,L"secret_key") == L"")) {
        wprintf(L"Missing or invalid secret_key.\n");
        CkJsonObjectW_Dispose(credentials);
        return;
    }

    //  Set this once to the region containing the vault.
    region = L"us-west-2";
    endpoint = CkStringBuilderW_Create();
    CkStringBuilderW_Append(endpoint,L"glacier.");
    CkStringBuilderW_Append(endpoint,region);
    CkStringBuilderW_Append(endpoint,L".amazonaws.com");
    aws = CkAuthAwsW_Create();
    CkAuthAwsW_putAccessKey(aws,CkJsonObjectW_stringOf(credentials,L"access_key"));
    CkAuthAwsW_putSecretKey(aws,CkJsonObjectW_stringOf(credentials,L"secret_key"));
    CkAuthAwsW_putRegion(aws,region);
    CkAuthAwsW_putServiceName(aws,L"glacier");
    CkAuthAwsW_putSignatureVersion(aws,4);
    rest = CkRestW_Create();
    CkRestW_putConnectTimeoutMs(rest,20000);
    CkRestW_putIdleTimeoutMs(rest,60000);
    success = CkRestW_SetAuthAws(rest,aws);
    if (success != TRUE) {
        wprintf(L"Could not configure AWS signing.\n");
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        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 (CkJsonObjectW_HasMember(credentials,L"session_token") == TRUE) {
        if (CkJsonObjectW_JsonTypeOf(credentials,L"session_token") != 1) {
            wprintf(L"session_token must be a string.\n");
            CkJsonObjectW_Dispose(credentials);
            CkStringBuilderW_Dispose(endpoint);
            CkAuthAwsW_Dispose(aws);
            CkRestW_Dispose(rest);
            return;
        }

        if (CkJsonObjectW_stringOf(credentials,L"session_token") != L"") {
            success = CkRestW_AddQueryParam(rest,L"X-Amz-Security-Token",CkJsonObjectW_stringOf(credentials,L"session_token"));
            if (success != TRUE) {
                wprintf(L"Could not set the session-token parameter.\n");
                CkJsonObjectW_Dispose(credentials);
                CkStringBuilderW_Dispose(endpoint);
                CkAuthAwsW_Dispose(aws);
                CkRestW_Dispose(rest);
                return;
            }

        }

    }

    //  Supply a Socket to explicitly enable TLS certificate verification.
    connection = CkSocketW_Create();
    CkSocketW_putRequireSslCertVerify(connection,TRUE);
    CkSocketW_putMaxReadIdleMs(connection,60000);
    CkSocketW_putMaxSendIdleMs(connection,60000);
    success = CkSocketW_Connect(connection,CkStringBuilderW_getAsString(endpoint),443,TRUE,20000);
    if (success != TRUE) {
        wprintf(L"TLS connection failed; inspect diagnostics privately.\n");
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        return;
    }

    success = CkRestW_UseConnection(rest,connection,TRUE);
    if (success != TRUE) {
        wprintf(L"Could not attach the TLS connection.\n");
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        return;
    }

    success = CkRestW_AddHeader(rest,L"x-amz-glacier-version",L"2012-06-01");
    if (success != TRUE) {
        wprintf(L"Could not set x-amz-glacier-version.\n");
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        return;
    }

    //  A hyphen selects the account belonging to the signing credentials.
    //  Use the original, URL-safe vault name and IDs returned by Glacier.
    success = CkRestW_AddPathParam(rest,L"{vault}",L"chilkat");
    if (success != TRUE) {
        wprintf(L"Could not set vault name.\n");
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        return;
    }

    body = CkJsonObjectW_Create();
    result = CkJsonObjectW_Create();
    CkJsonObjectW_putStrict(result,TRUE);
    error = CkJsonObjectW_Create();
    CkJsonObjectW_putStrict(error,TRUE);
    requestBody = CkStringBuilderW_Create();
    responseBody = CkStringBuilderW_Create();
    success = CkRestW_FullRequestNoBodySb(rest,L"PUT",L"/-/vaults/{vault}",responseBody);
    if (success != TRUE) {
        wprintf(L"Request did not complete; inspect diagnostics privately. Check state before retrying a change.\n");
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkJsonObjectW_Dispose(error);
        CkStringBuilderW_Dispose(requestBody);
        CkStringBuilderW_Dispose(responseBody);
        return;
    }

    if (CkRestW_getResponseStatusCode(rest) != 201) {
        wprintf(L"Glacier HTTP status: %d\n",CkRestW_getResponseStatusCode(rest));
        success = CkJsonObjectW_LoadSb(error,responseBody);
        if (success == TRUE) {
            if (CkJsonObjectW_JsonTypeOf(error,L"code") == 1) {
                wprintf(L"AWS error code: %s\n",CkJsonObjectW_stringOf(error,L"code"));
            }

        }

        wprintf(L"AWS request ID: %s\n",CkRestW_responseHdrByName(rest,L"x-amzn-RequestId"));
        //  Inspect full errors privately. An HTTP error is not a successful operation.
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkJsonObjectW_Dispose(error);
        CkStringBuilderW_Dispose(requestBody);
        CkStringBuilderW_Dispose(responseBody);
        return;
    }

    wprintf(L"Vault location: %s\n",CkRestW_responseHdrByName(rest,L"Location"));
    wprintf(L"Operation succeeded.\n");


    CkJsonObjectW_Dispose(credentials);
    CkStringBuilderW_Dispose(endpoint);
    CkAuthAwsW_Dispose(aws);
    CkRestW_Dispose(rest);
    CkSocketW_Dispose(connection);
    CkJsonObjectW_Dispose(body);
    CkJsonObjectW_Dispose(result);
    CkJsonObjectW_Dispose(error);
    CkStringBuilderW_Dispose(requestBody);
    CkStringBuilderW_Dispose(responseBody);

    }