Amazon Glacier: Replace Vault Access Policy
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.
Loads a complete, reviewed IAM policy from a private file and replaces the existing mutable vault policy (maximum 20 KB). Get and back up the current policy before applying a change. Use actual account IDs in policy ARNs; the request-path hyphen is not valid inside an ARN. This does not configure Vault Lock.
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.
Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Credentials
string ls_Region
oleobject loo_Endpoint
oleobject loo_Aws
oleobject loo_Rest
oleobject loo_Connection
oleobject loo_Body
oleobject loo_Result
oleobject loo_Error
oleobject loo_RequestBody
oleobject loo_ResponseBody
oleobject loo_Policy
// Assumes Chilkat is unlocked. See Global Unlock Sample.
li_Success = 0
// Private configuration, not a file in public web content or source control.
// Load current IAM credentials authorized for the Glacier operation.
loo_Credentials = create oleobject
li_rc = loo_Credentials.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
destroy loo_Credentials
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Credentials.Strict = 1
li_Success = loo_Credentials.LoadFile("qa_data/aws/glacier.json")
if li_Success <> 1 then
Write-Debug "Could not load private AWS credentials."
destroy loo_Credentials
return
end if
if (loo_Credentials.JsonTypeOf("access_key") <> 1) OR (loo_Credentials.StringOf("access_key") = "") then
Write-Debug "Missing or invalid access_key."
destroy loo_Credentials
return
end if
if (loo_Credentials.JsonTypeOf("secret_key") <> 1) OR (loo_Credentials.StringOf("secret_key") = "") then
Write-Debug "Missing or invalid secret_key."
destroy loo_Credentials
return
end if
// Set this once to the region containing the vault.
ls_Region = "us-west-2"
loo_Endpoint = create oleobject
li_rc = loo_Endpoint.ConnectToNewObject("Chilkat.StringBuilder")
loo_Endpoint.Append("glacier.")
loo_Endpoint.Append(ls_Region)
loo_Endpoint.Append(".amazonaws.com")
loo_Aws = create oleobject
li_rc = loo_Aws.ConnectToNewObject("Chilkat.AuthAws")
loo_Aws.AccessKey = loo_Credentials.StringOf("access_key")
loo_Aws.SecretKey = loo_Credentials.StringOf("secret_key")
loo_Aws.Region = ls_Region
loo_Aws.ServiceName = "glacier"
loo_Aws.SignatureVersion = 4
loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")
loo_Rest.ConnectTimeoutMs = 20000
loo_Rest.IdleTimeoutMs = 60000
li_Success = loo_Rest.SetAuthAws(loo_Aws)
if li_Success <> 1 then
Write-Debug "Could not configure AWS signing."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
return
end if
// 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 loo_Credentials.HasMember("session_token") = 1 then
if loo_Credentials.JsonTypeOf("session_token") <> 1 then
Write-Debug "session_token must be a string."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
return
end if
if loo_Credentials.StringOf("session_token") <> "" then
li_Success = loo_Rest.AddQueryParam("X-Amz-Security-Token",loo_Credentials.StringOf("session_token"))
if li_Success <> 1 then
Write-Debug "Could not set the session-token parameter."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
return
end if
end if
end if
// Supply a Socket to explicitly enable TLS certificate verification.
loo_Connection = create oleobject
li_rc = loo_Connection.ConnectToNewObject("Chilkat.Socket")
loo_Connection.RequireSslCertVerify = 1
loo_Connection.MaxReadIdleMs = 60000
loo_Connection.MaxSendIdleMs = 60000
li_Success = loo_Connection.Connect(loo_Endpoint.GetAsString(),443,1,20000)
if li_Success <> 1 then
Write-Debug "TLS connection failed; inspect diagnostics privately."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
return
end if
li_Success = loo_Rest.UseConnection(loo_Connection,1)
if li_Success <> 1 then
Write-Debug "Could not attach the TLS connection."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
return
end if
li_Success = loo_Rest.AddHeader("x-amz-glacier-version","2012-06-01")
if li_Success <> 1 then
Write-Debug "Could not set x-amz-glacier-version."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
return
end if
// A hyphen selects the account belonging to the signing credentials.
// Use the original, URL-safe vault name and IDs returned by Glacier.
li_Success = loo_Rest.AddPathParam("{vault}","chilkat")
if li_Success <> 1 then
Write-Debug "Could not set vault name."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
return
end if
loo_Body = create oleobject
li_rc = loo_Body.ConnectToNewObject("Chilkat.JsonObject")
loo_Result = create oleobject
li_rc = loo_Result.ConnectToNewObject("Chilkat.JsonObject")
loo_Result.Strict = 1
loo_Error = create oleobject
li_rc = loo_Error.ConnectToNewObject("Chilkat.JsonObject")
loo_Error.Strict = 1
loo_RequestBody = create oleobject
li_rc = loo_RequestBody.ConnectToNewObject("Chilkat.StringBuilder")
loo_ResponseBody = create oleobject
li_rc = loo_ResponseBody.ConnectToNewObject("Chilkat.StringBuilder")
// This REPLACES the entire mutable policy. Review and back up the current policy first.
loo_Policy = create oleobject
li_rc = loo_Policy.ConnectToNewObject("Chilkat.JsonObject")
loo_Policy.Strict = 1
li_Success = loo_Policy.LoadFile("qa_data/aws/glacier-vault-policy.json")
if li_Success <> 1 then
Write-Debug "Could not load the reviewed vault policy."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_Error
destroy loo_RequestBody
destroy loo_ResponseBody
destroy loo_Policy
return
end if
li_Success = loo_Body.UpdateString("Policy",loo_Policy.Emit())
if li_Success <> 1 then
Write-Debug "Could not set Policy."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_Error
destroy loo_RequestBody
destroy loo_ResponseBody
destroy loo_Policy
return
end if
li_Success = loo_Rest.AddHeader("Content-Type","application/json")
if li_Success <> 1 then
Write-Debug "Could not set Content-Type."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_Error
destroy loo_RequestBody
destroy loo_ResponseBody
destroy loo_Policy
return
end if
li_Success = loo_Body.EmitSb(loo_RequestBody)
if li_Success <> 1 then
Write-Debug "Could not encode JSON request."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_Error
destroy loo_RequestBody
destroy loo_ResponseBody
destroy loo_Policy
return
end if
li_Success = loo_Rest.FullRequestSb("PUT","/-/vaults/{vault}/access-policy",loo_RequestBody,loo_ResponseBody)
if li_Success <> 1 then
Write-Debug "Request did not complete; inspect diagnostics privately. Check state before retrying a change."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_Error
destroy loo_RequestBody
destroy loo_ResponseBody
destroy loo_Policy
return
end if
if loo_Rest.ResponseStatusCode <> 204 then
Write-Debug "Glacier HTTP status: " + string(loo_Rest.ResponseStatusCode)
li_Success = loo_Error.LoadSb(loo_ResponseBody)
if li_Success = 1 then
if loo_Error.JsonTypeOf("code") = 1 then
Write-Debug "AWS error code: " + loo_Error.StringOf("code")
end if
end if
Write-Debug "AWS request ID: " + loo_Rest.ResponseHdrByName("x-amzn-RequestId")
// Inspect full errors privately. An HTTP error is not a successful operation.
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_Error
destroy loo_RequestBody
destroy loo_ResponseBody
destroy loo_Policy
return
end if
Write-Debug "Vault access policy replaced."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_Error
destroy loo_RequestBody
destroy loo_ResponseBody
destroy loo_Policy