C
C
Amazon Glacier Get Vault Access Policy
See more Amazon Glacier Examples
Demonstrates how to retrieve the access-policy subresource set on the vaultChilkat C Downloads
#include <C_CkRest.h>
#include <C_CkAuthAws.h>
#include <C_CkStringBuilder.h>
#include <C_CkJsonObject.h>
void ChilkatSample(void)
{
BOOL success;
HCkRest rest;
BOOL bTls;
int port;
BOOL bAutoReconnect;
HCkAuthAws authAws;
HCkStringBuilder sbResponseBody;
int respStatusCode;
HCkJsonObject json;
HCkJsonObject jsonPolicy;
const char *Version;
int i;
int count_i;
const char *Sid;
const char *Effect;
const char *PrincipalAWS;
const char *Action;
const char *Resource;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
rest = CkRest_Create();
// Connect to the Amazon AWS REST server in the desired region.
bTls = TRUE;
port = 443;
bAutoReconnect = TRUE;
success = CkRest_Connect(rest,"glacier.us-west-2.amazonaws.com",port,bTls,bAutoReconnect);
// Provide AWS credentials.
authAws = CkAuthAws_Create();
CkAuthAws_putAccessKey(authAws,"AWS_ACCESS_KEY");
CkAuthAws_putSecretKey(authAws,"AWS_SECRET_KEY");
CkAuthAws_putServiceName(authAws,"glacier");
CkAuthAws_putRegion(authAws,"us-west-2");
success = CkRest_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
//
CkRest_AddHeader(rest,"x-amz-glacier-version","2012-06-01");
// Get the access policy for the "chilkat" vault.
sbResponseBody = CkStringBuilder_Create();
success = CkRest_FullRequestNoBodySb(rest,"GET","/AWS_ACCOUNT_ID/vaults/chilkat/access-policy",sbResponseBody);
if (success != TRUE) {
printf("%s\n",CkRest_lastErrorText(rest));
CkRest_Dispose(rest);
CkAuthAws_Dispose(authAws);
CkStringBuilder_Dispose(sbResponseBody);
return;
}
respStatusCode = CkRest_getResponseStatusCode(rest);
if (respStatusCode >= 400) {
printf("Response Status Code = %d\n",respStatusCode);
printf("Response Header:\n");
printf("%s\n",CkRest_responseHeader(rest));
printf("Response Body:\n");
printf("%s\n",CkStringBuilder_getAsString(sbResponseBody));
CkRest_Dispose(rest);
CkAuthAws_Dispose(authAws);
CkStringBuilder_Dispose(sbResponseBody);
return;
}
// Success is indicated by a 200 response status.
printf("response status code = %d\n",respStatusCode);
json = CkJsonObject_Create();
CkJsonObject_LoadSb(json,sbResponseBody);
CkJsonObject_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 = CkJsonObject_Create();
CkJsonObject_Load(jsonPolicy,CkJsonObject_stringOf(json,"Policy"));
CkJsonObject_putEmitCompact(jsonPolicy,FALSE);
printf("%s\n",CkJsonObject_emit(jsonPolicy));
printf("----\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 = CkJsonObject_stringOf(jsonPolicy,"Version");
i = 0;
count_i = CkJsonObject_SizeOfArray(jsonPolicy,"Statement");
while (i < count_i) {
CkJsonObject_putI(jsonPolicy,i);
Sid = CkJsonObject_stringOf(jsonPolicy,"Statement[i].Sid");
Effect = CkJsonObject_stringOf(jsonPolicy,"Statement[i].Effect");
PrincipalAWS = CkJsonObject_stringOf(jsonPolicy,"Statement[i].Principal.AWS");
Action = CkJsonObject_stringOf(jsonPolicy,"Statement[i].Action");
Resource = CkJsonObject_stringOf(jsonPolicy,"Statement[i].Resource");
i = i + 1;
}
CkRest_Dispose(rest);
CkAuthAws_Dispose(authAws);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonPolicy);
}