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

PHP
<?php

use Chilkat2\AuthAws;
use Chilkat2\JsonObject;
use Chilkat2\Rest;
use Chilkat2\Socket;
use Chilkat2\StringBuilder;

// 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 = new JsonObject();
$credentials->Strict = true;
try {
    $credentials->LoadFile('qa_data/aws/glacier.json');
} catch (\Chilkat2\Exception $e) {
    echo "Could not load private AWS credentials.\n";
    exit;
}

if (($credentials->JsonTypeOf('access_key') != 1) || ($credentials->StringOf('access_key') === '')) {
    echo "Missing or invalid access_key.\n";
    exit;
}

if (($credentials->JsonTypeOf('secret_key') != 1) || ($credentials->StringOf('secret_key') === '')) {
    echo "Missing or invalid secret_key.\n";
    exit;
}

// Set this once to the region containing the vault.
$region = 'us-west-2';
$endpoint = new StringBuilder();
$endpoint->Append('glacier.');
$endpoint->Append($region);
$endpoint->Append('.amazonaws.com');
$aws = new AuthAws();
$aws->AccessKey = $credentials->StringOf('access_key');
$aws->SecretKey = $credentials->StringOf('secret_key');
$aws->Region = $region;
$aws->ServiceName = 'glacier';
$aws->SignatureVersion = 4;
$rest = new Rest();
$rest->ConnectTimeoutMs = 20000;
$rest->IdleTimeoutMs = 60000;
try {
    $rest->SetAuthAws($aws);
} catch (\Chilkat2\Exception $e) {
    echo "Could not configure AWS signing.\n";
    exit;
}

// 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('session_token')) {
    if ($credentials->JsonTypeOf('session_token') != 1) {
        echo "session_token must be a string.\n";
        exit;
    }

    if ($credentials->StringOf('session_token') !== '') {
        try {
            $rest->AddQueryParam('X-Amz-Security-Token', $credentials->StringOf('session_token'));
        } catch (\Chilkat2\Exception $e) {
            echo "Could not set the session-token parameter.\n";
            exit;
        }
    }
}

// Supply a Socket to explicitly enable TLS certificate verification.
$connection = new Socket();
$connection->RequireSslCertVerify = true;
$connection->MaxReadIdleMs = 60000;
$connection->MaxSendIdleMs = 60000;
try {
    $connection->Connect($endpoint->GetAsString(), 443, true, 20000);
} catch (\Chilkat2\Exception $e) {
    echo "TLS connection failed; inspect diagnostics privately.\n";
    exit;
}

try {
    $rest->UseConnection($connection, true);
} catch (\Chilkat2\Exception $e) {
    echo "Could not attach the TLS connection.\n";
    exit;
}

try {
    $rest->AddHeader('x-amz-glacier-version', '2012-06-01');
} catch (\Chilkat2\Exception $e) {
    echo "Could not set x-amz-glacier-version.\n";
    exit;
}

// A hyphen selects the account belonging to the signing credentials.
// Use the original, URL-safe vault name and IDs returned by Glacier.
try {
    $rest->AddPathParam('{vault}', 'chilkat');
} catch (\Chilkat2\Exception $e) {
    echo "Could not set vault name.\n";
    exit;
}

$body = new JsonObject();
$result = new JsonObject();
$result->Strict = true;
$error = new JsonObject();
$error->Strict = true;
$requestBody = new StringBuilder();
$responseBody = new StringBuilder();
try {
    $rest->FullRequestNoBodySb('PUT', '/-/vaults/{vault}', $responseBody);
} catch (\Chilkat2\Exception $e) {
    echo "Request did not complete; inspect diagnostics privately. Check state before retrying a change.\n";
    exit;
}

if ($rest->ResponseStatusCode != 201) {
    echo 'Glacier HTTP status: ', $rest->ResponseStatusCode, "\n";
    try {
        $error->LoadSb($responseBody);
        $success = true;
    } catch (\Chilkat2\Exception $e) {
        $success = false;
    }
    if ($success) {
        if ($error->JsonTypeOf('code') == 1) {
            echo 'AWS error code: ', $error->StringOf('code'), "\n";
        }
    }

    echo 'AWS request ID: ', $rest->ResponseHdrByName('x-amzn-RequestId'), "\n";
    // Inspect full errors privately. An HTTP error is not a successful operation.
    exit;
}

echo 'Vault location: ', $rest->ResponseHdrByName('Location'), "\n";
echo "Operation succeeded.\n";