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

Amazon Glacier: Upload Archive from a File

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 hashes and streams a file without buffering the full upload; the file must remain unchanged.

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>
#include <C_CkCrypt2W.h>
#include <C_CkFileAccessW.h>
#include <C_CkStreamW.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;
    const wchar_t *filePath;
    HCkCrypt2W crypt;
    HCkFileAccessW files;
    const wchar_t *fileSize;
    const wchar_t *treeHash;
    const wchar_t *linearHash;
    HCkStreamW fileStream;
    const wchar_t *response;
    const wchar_t *archiveId;
    const wchar_t *returnedHash;

    //  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();
    filePath = L"qa_data/zips/somethingBig.zip";
    //  Keep this file unchanged during BOTH hashing passes and the upload.
    crypt = CkCrypt2W_Create();
    CkCrypt2W_putHashAlgorithm(crypt,L"sha256-tree-hash");
    CkCrypt2W_putEncodingMode(crypt,L"hexlower");
    files = CkFileAccessW_Create();
    fileSize = CkFileAccessW_fileSizeStr(files,filePath);
    if (CkFileAccessW_getLastMethodSuccess(files) != TRUE) {
        wprintf(L"Could not read archive file size.\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);
        CkCrypt2W_Dispose(crypt);
        CkFileAccessW_Dispose(files);
        return;
    }

    if (fileSize == L"0") {
        CkCrypt2W_putHashAlgorithm(crypt,L"sha256");
    }

    treeHash = CkCrypt2W_hashFileENC(crypt,filePath);
    if (CkCrypt2W_getLastMethodSuccess(crypt) != TRUE) {
        wprintf(L"Could not compute tree hash.\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);
        CkCrypt2W_Dispose(crypt);
        CkFileAccessW_Dispose(files);
        return;
    }

    success = CkRestW_AddHeader(rest,L"x-amz-sha256-tree-hash",treeHash);
    if (success != TRUE) {
        wprintf(L"Could not set x-amz-sha256-tree-hash.\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);
        CkCrypt2W_Dispose(crypt);
        CkFileAccessW_Dispose(files);
        return;
    }

    CkCrypt2W_putHashAlgorithm(crypt,L"sha256");
    linearHash = CkCrypt2W_hashFileENC(crypt,filePath);
    if (CkCrypt2W_getLastMethodSuccess(crypt) != TRUE) {
        wprintf(L"Could not compute payload hash.\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);
        CkCrypt2W_Dispose(crypt);
        CkFileAccessW_Dispose(files);
        return;
    }

    CkAuthAwsW_putPrecomputedSha256(aws,linearHash);
    //  Set the precomputed hash before attaching the signing configuration again.
    success = CkRestW_SetAuthAws(rest,aws);
    if (success != TRUE) {
        wprintf(L"Could not update AWS signing.\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);
        CkCrypt2W_Dispose(crypt);
        CkFileAccessW_Dispose(files);
        return;
    }

    success = CkRestW_AddHeader(rest,L"x-amz-content-sha256",linearHash);
    if (success != TRUE) {
        wprintf(L"Could not set x-amz-content-sha256.\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);
        CkCrypt2W_Dispose(crypt);
        CkFileAccessW_Dispose(files);
        return;
    }

    success = CkRestW_AddHeader(rest,L"x-amz-archive-description",L"Example archive");
    if (success != TRUE) {
        wprintf(L"Could not set x-amz-archive-description.\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);
        CkCrypt2W_Dispose(crypt);
        CkFileAccessW_Dispose(files);
        return;
    }

    fileStream = CkStreamW_Create();
    CkStreamW_putSourceFile(fileStream,filePath);
    CkRestW_putStreamNonChunked(rest,TRUE);
    CkRestW_putIdleTimeoutMs(rest,120000);
    CkSocketW_putMaxReadIdleMs(connection,120000);
    CkSocketW_putMaxSendIdleMs(connection,120000);
    response = CkRestW_fullRequestStream(rest,L"POST",L"/-/vaults/{vault}/archives",fileStream);
    if (CkRestW_getLastMethodSuccess(rest) != TRUE) {
        wprintf(L"Upload did not complete; check for an accepted archive before retrying.\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);
        CkCrypt2W_Dispose(crypt);
        CkFileAccessW_Dispose(files);
        CkStreamW_Dispose(fileStream);
        return;
    }

    CkStringBuilderW_Append(responseBody,response);
    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);
        CkCrypt2W_Dispose(crypt);
        CkFileAccessW_Dispose(files);
        CkStreamW_Dispose(fileStream);
        return;
    }

    archiveId = CkRestW_responseHdrByName(rest,L"x-amz-archive-id");
    if ((CkRestW_getLastMethodSuccess(rest) != TRUE) || (archiveId == L"")) {
        wprintf(L"Missing archive ID; investigate the accepted upload before retrying.\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);
        CkCrypt2W_Dispose(crypt);
        CkFileAccessW_Dispose(files);
        CkStreamW_Dispose(fileStream);
        return;
    }

    //  Save this ID even if subsequent checksum verification fails.
    wprintf(L"Archive ID: %s\n",archiveId);
    wprintf(L"Location: %s\n",CkRestW_responseHdrByName(rest,L"Location"));
    returnedHash = CkRestW_responseHdrByName(rest,L"x-amz-sha256-tree-hash");
    if (returnedHash != treeHash) {
        wprintf(L"Upload response checksum mismatch; preserve the archive ID and investigate.\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);
        CkCrypt2W_Dispose(crypt);
        CkFileAccessW_Dispose(files);
        CkStreamW_Dispose(fileStream);
        return;
    }

    wprintf(L"Archive upload verified. Persist its ID and checksum in your archive catalog.\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);
    CkCrypt2W_Dispose(crypt);
    CkFileAccessW_Dispose(files);
    CkStreamW_Dispose(fileStream);

    }