Delphi DLL
Delphi DLL
Amazon Glacier Get Vault Access Policy
See more Amazon Glacier Examples
Demonstrates how to retrieve the access-policy subresource set on the vaultChilkat Delphi DLL Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, AuthAws, Rest, StringBuilder, JsonObject;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
rest: HCkRest;
bTls: Boolean;
port: Integer;
bAutoReconnect: Boolean;
authAws: HCkAuthAws;
sbResponseBody: HCkStringBuilder;
respStatusCode: Integer;
json: HCkJsonObject;
jsonPolicy: HCkJsonObject;
Version: PWideChar;
i: Integer;
count_i: Integer;
Sid: PWideChar;
Effect: PWideChar;
PrincipalAWS: PWideChar;
Action: PWideChar;
Resource: PWideChar;
begin
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) then
begin
Memo1.Lines.Add(CkRest__lastErrorText(rest));
Exit;
end;
respStatusCode := CkRest_getResponseStatusCode(rest);
if (respStatusCode >= 400) then
begin
Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
Memo1.Lines.Add('Response Header:');
Memo1.Lines.Add(CkRest__responseHeader(rest));
Memo1.Lines.Add('Response Body:');
Memo1.Lines.Add(CkStringBuilder__getAsString(sbResponseBody));
Exit;
end;
// Success is indicated by a 200 response status.
Memo1.Lines.Add('response status code = ' + IntToStr(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);
Memo1.Lines.Add(CkJsonObject__emit(jsonPolicy));
Memo1.Lines.Add('----');
// 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 do
begin
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;
end;
CkRest_Dispose(rest);
CkAuthAws_Dispose(authAws);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonPolicy);
end;