Sample code for 30+ languages & platforms
Unicode C

Amazon Glacier Get Vault Access Policy

See more Amazon Glacier Examples

Demonstrates how to retrieve the access-policy subresource set on the vault

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkRestW rest;
    BOOL bTls;
    int port;
    BOOL bAutoReconnect;
    HCkAuthAwsW authAws;
    HCkStringBuilderW sbResponseBody;
    int respStatusCode;
    HCkJsonObjectW json;
    HCkJsonObjectW jsonPolicy;
    const wchar_t *Version;
    int i;
    int count_i;
    const wchar_t *Sid;
    const wchar_t *Effect;
    const wchar_t *PrincipalAWS;
    const wchar_t *Action;
    const wchar_t *Resource;

    success = FALSE;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    rest = CkRestW_Create();

    // Connect to the Amazon AWS REST server in the desired region.
    bTls = TRUE;
    port = 443;
    bAutoReconnect = TRUE;
    success = CkRestW_Connect(rest,L"glacier.us-west-2.amazonaws.com",port,bTls,bAutoReconnect);

    // Provide AWS credentials.
    authAws = CkAuthAwsW_Create();
    CkAuthAwsW_putAccessKey(authAws,L"AWS_ACCESS_KEY");
    CkAuthAwsW_putSecretKey(authAws,L"AWS_SECRET_KEY");
    CkAuthAwsW_putServiceName(authAws,L"glacier");
    CkAuthAwsW_putRegion(authAws,L"us-west-2");

    success = CkRestW_SetAuthAws(rest,authAws);

    // --------------------------------------------------------------------------
    // Note: The above REST connection and setup of the AWS credentials
    // can be done once.  After connecting, any number of REST calls can be made.
    // The "auto reconnect" property passed to rest.Connect indicates that if
    // the connection is lost, a REST method call will automatically reconnect
    // if needed.
    // --------------------------------------------------------------------------

    // 
    // For more information, see Glacier Get Vault Access Policy Reference Documentation
    // 
    CkRestW_AddHeader(rest,L"x-amz-glacier-version",L"2012-06-01");

    // Get the access policy for the "chilkat" vault.
    sbResponseBody = CkStringBuilderW_Create();
    success = CkRestW_FullRequestNoBodySb(rest,L"GET",L"/AWS_ACCOUNT_ID/vaults/chilkat/access-policy",sbResponseBody);
    if (success != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkAuthAwsW_Dispose(authAws);
        CkStringBuilderW_Dispose(sbResponseBody);
        return;
    }

    respStatusCode = CkRestW_getResponseStatusCode(rest);
    if (respStatusCode >= 400) {
        wprintf(L"Response Status Code = %d\n",respStatusCode);
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",CkRestW_responseHeader(rest));
        wprintf(L"Response Body:\n");
        wprintf(L"%s\n",CkStringBuilderW_getAsString(sbResponseBody));
        CkRestW_Dispose(rest);
        CkAuthAwsW_Dispose(authAws);
        CkStringBuilderW_Dispose(sbResponseBody);
        return;
    }

    // Success is indicated by a 200 response status.
    wprintf(L"response status code = %d\n",respStatusCode);

    json = CkJsonObjectW_Create();
    CkJsonObjectW_LoadSb(json,sbResponseBody);
    CkJsonObjectW_putEmitCompact(json,FALSE);

    // Returns JSON such as this:
    // {
    //   "Policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"Define-owner-access-rights\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"arn:aws:iam::9999999999999:root\"},\"Action\":\"glacier:DeleteArchive\",\"Resource\":\"arn:aws:glacier:us-west-2:9999999999999:vaults/chilkat\"}]}"
    // }

    // Unwrap...
    jsonPolicy = CkJsonObjectW_Create();
    CkJsonObjectW_Load(jsonPolicy,CkJsonObjectW_stringOf(json,L"Policy"));
    CkJsonObjectW_putEmitCompact(jsonPolicy,FALSE);

    wprintf(L"%s\n",CkJsonObjectW_emit(jsonPolicy));
    wprintf(L"----\n");

    // The jsonPolicy contains:

    // {
    //   "Version": "2012-10-17",
    //   "Statement": [
    //     {
    //       "Sid": "Define-owner-access-rights",
    //       "Effect": "Allow",
    //       "Principal": {
    //         "AWS": "arn:aws:iam::9999999999999:root"
    //       },
    //       "Action": "glacier:DeleteArchive",
    //       "Resource": "arn:aws:glacier:us-west-2:9999999999999:vaults/chilkat"
    //     }
    //   ]
    // }
    // 

    // Use this online tool to generate parsing code from sample JSON: 
    // Generate Parsing Code from JSON

    // To parse the above contents of jsonPolicy...

    Version = CkJsonObjectW_stringOf(jsonPolicy,L"Version");
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jsonPolicy,L"Statement");
    while (i < count_i) {
        CkJsonObjectW_putI(jsonPolicy,i);
        Sid = CkJsonObjectW_stringOf(jsonPolicy,L"Statement[i].Sid");
        Effect = CkJsonObjectW_stringOf(jsonPolicy,L"Statement[i].Effect");
        PrincipalAWS = CkJsonObjectW_stringOf(jsonPolicy,L"Statement[i].Principal.AWS");
        Action = CkJsonObjectW_stringOf(jsonPolicy,L"Statement[i].Action");
        Resource = CkJsonObjectW_stringOf(jsonPolicy,L"Statement[i].Resource");
        i = i + 1;
    }



    CkRestW_Dispose(rest);
    CkAuthAwsW_Dispose(authAws);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(json);
    CkJsonObjectW_Dispose(jsonPolicy);

    }