Created
February 8, 2023 20:47
-
-
Save athossampayo/bcf532a27363d6025afcbde0590179c1 to your computer and use it in GitHub Desktop.
Python boto3 S3 Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
s3_client = boto3.resource('s3') | |
my_bucket = s3_client.Bucket('bucket') | |
files_list = [object_summary for object_summary in my_bucket.objects.filter(Prefix='path/to/dir')] | |
for object_summary in files_list: | |
print(object_summary.key) | |
object_summary.delete() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# list files | |
import boto3 | |
s3_client = boto3.resource('s3') | |
my_bucket = s3_client.Bucket('bucket') | |
files_list = [object_summary for object_summary in my_bucket.objects.filter(Prefix='path/to/dir')] | |
for object_summary in files_list: | |
print(object_summary.key) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
thebucket = 'bucket' | |
s3 = boto3.resource('s3') | |
s3client = boto3.client('s3') | |
paginator = s3client.get_paginator('list_object_versions') | |
pageresponse = paginator.paginate(Bucket=thebucket, Prefix='path/to/dir') | |
for pageobject in pageresponse: | |
if 'DeleteMarkers' in pageobject.keys(): | |
for each_delmarker in pageobject['DeleteMarkers']: | |
if 'parquet' in each_delmarker['Key']: | |
print(each_delmarker['Key']) | |
print(each_delmarker['LastModified']) | |
if 'substring' in each_delmarker['Key']: | |
fileobjver = s3.ObjectVersion( | |
thebucket, | |
each_delmarker['Key'], | |
each_delmarker['VersionId'] | |
) | |
print('Recovering: ' + each_delmarker['Key']) | |
fileobjver.delete() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment