PowerShell AWS: Put Bucket Lifecycle

Back to Index

Creates a new lifecycle configuration for the bucket or replaces an existing lifecycle configuration.

Documentation: http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUTlifecycle.html


[Reflection.Assembly]::LoadFile("C:\myAssemblies\ChilkatDotNet2.dll")

$rest = New-Object Chilkat.Rest

#   Provide AWS credentials for the REST call.
$authAws = New-Object Chilkat.AuthAws
$authAws.AccessKey = "AWS_ACCESS_KEY"
$authAws.SecretKey = "AWS_SECRET_KEY"
$authAws.ServiceName = "s3"
$authAws.Region = "us-west-2"
$rest.SetAuthAws($authAws)

$success = $rest.Connect("s3.us-west-2.amazonaws.com",443,$true,$true)
if ($success -ne $true) {
    $($rest.LastErrorText)
    exit
}

$rest.Host = "examplebucket.s3.us-west-2.amazonaws.com"

#  The following code creates the XML request body.
#  The XML created by this code is shown below.
$xmlReq = New-Object Chilkat.Xml
$xmlReq.Tag = "LifecycleConfiguration"
$xmlReq.UpdateChildContent("Rule|ID","id1")
$xmlReq.UpdateChildContent("Rule|Filter|Prefix","documents/")
$xmlReq.UpdateChildContent("Rule|Status","Enabled")
$xmlReq.UpdateChildContent("Rule|Transition|Days","30")
$xmlReq.UpdateChildContent("Rule|Transition|StorageClass","GLACIER")
$xmlReq.UpdateChildContent("Rule[1]|ID","id2")
$xmlReq.UpdateChildContent("Rule[1]|Filter|Prefix","logs/")
$xmlReq.UpdateChildContent("Rule[1]|Status","Enabled")
$xmlReq.UpdateChildContent("Rule[1]|Expiration|Days","365")

$sbReq = New-Object Chilkat.StringBuilder
$xmlReq.GetXmlSb($sbReq)

$rest.AddHeader("Content-Type","text/xml")

$sbResponse = New-Object Chilkat.StringBuilder
$success = $rest.FullRequestSb("PUT","/?lifecycle",$sbReq,$sbResponse)
if ($success -ne $true) {
    $($rest.LastErrorText)
    exit
}

if ($rest.ResponseStatusCode -ne 200) {
    $("Received error response code: " + $rest.ResponseStatusCode)
    $("Response body:")
    $($sbResponse.GetAsString())
    exit
}

$("Example Completed.")

Sample XML Request Body

<LifecycleConfiguration>
  <Rule>
    <ID>id1</ID>
    <Filter>
       <Prefix>documents/</Prefix>
    </Filter>
    <Status>Enabled</Status>
    <Transition>
      <Days>30</Days>
      <StorageClass>GLACIER</StorageClass>
    </Transition>
  </Rule>
  <Rule>
    <ID>id2</ID>
    <Filter>
       <Prefix>logs/</Prefix>
    </Filter>
    <Status>Enabled</Status>
    <Expiration>
      <Days>365</Days>
    </Expiration>
  </Rule>
</LifecycleConfiguration>