Powershell Get-S3Version not returning all delete markers

0

I am running a powershell against an S3 bucket to get delete markers for object recovery but not getting all the results back for folders deleted.

Here is the code

$bucketName = "BucketName"
$prefix = "BucketFolder"
((Get-S3Version -BucketName $s3Bucket -Prefix $prefix).Versions | Where { $_.IsDeleteMarker -eq "True"})

The results I get back will contain the delete marker for "FolderA" which was deleted but not "FolderB" which was also deleted. The only way I can get results that include FolderB is to set the $prefix value to explicitly list FolderB like this

$bucketName = "BucketName"
$prefix = "BucketFolder/FolderB"
((Get-S3Version -BucketName $s3Bucket -Prefix $prefix).Versions | Where { $_.IsDeleteMarker -eq "True"})

It seems odd that the results will only show the delete markers for FolderB if it is specified but will show the delete marker for FolderA.

Has anyone seen this behavior before or know a way to get it to return all delete markers?

Mike
asked 9 months ago245 views
2 Answers
0

Did you try adding "/" at the end and see how it works.

Here is how I use to delete the delete markers:

    import boto3
    bucket = 'bkt_name'
    my_session = boto3.Session(profile_name='cli_profile_name')
    s3_client = dev_session.client('s3')
    #s3_client = boto3.client('s3')
    object_response_paginator = s3_client.get_paginator('list_object_versions')
    delete_marker_list = []
    for object_response_itr in object_response_paginator.paginate(Bucket=bucket , Prefix ="prefix_name/",PaginationConfig={
            'MaxItems': 22000,
            'PageSize': 100
        }):
        if 'DeleteMarkers' in object_response_itr:
            for delete_marker in object_response_itr['DeleteMarkers']:
                if(len(delete_marker['Key'].split('/')[len(delete_marker['Key'].split('/')) -1 ] ) > 0 ):
                    output = dev_s3_client.delete_objects(Bucket=bucket,Delete={'Objects': [{'Key': delete_marker['Key'], 'VersionId': delete_marker['VersionId']}]  , 'Quiet': True })
                    print(output)

Replace bkt_name, cli_profile_name and prefix_name in the above script. Test it first in your lower test environment and verify if this is working as expected for you and then only do it in your actual environment.

Hope you find this useful.

Comment here if you have additional questions, happy to help.

Abhishek

profile pictureAWS
EXPERT
answered 9 months ago
  • Would you mind pasting your snippet here, I can definitely help.

0

I have tried adding / at the end and it doesn't change the results. I'd like to accomplish this with Powershell instead of Python if possible.

Mike
answered 9 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions