AWS S3 PS script to get files less than 7 days and delete it

Welcome Forums General PowerShell Q&A AWS S3 PS script to get files less than 7 days and delete it

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
6 months, 1 week ago.

  • Author
    Posts
  • #102460

    Participant
    Points: 0
    Rank: Member

    Hi Team,
    We have S3 bucket for which we need to set up retention policy of 7 days for it folder and subfolders. I wrote the below code however it gives me folders and files both. I need just files (like we use -files paramater in GCI command).

    foreach ($o in $outbox)
    {
    Get-S3Object -BucketName $bucketname -AccessKey $accesskey -SecretKey $secretkey -KeyPrefix $o | where LastModified -LT (Get-Date).AddDays(-7)
    }

    How can i get only files from s3 folders.

  • #102469

    Participant
    Points: 342
    Helping Hand
    Rank: Contributor

    You'd have to see what you can filter on to determine if it is a file. The examples I see indicate the Get-S3Object returns files, like this blog: https://blogs.msdn.microsoft.com/cindygross/2015/02/20/powershell-works-for-amazon-aws-s3-too/

    Try adding a | Select * or Get-Member to see all of the properties that Get-S3Object returns so you can build a Where filter

The topic ‘AWS S3 PS script to get files less than 7 days and delete it’ is closed to new replies.