Skip to content

Instantly share code, notes, and snippets.

@athossampayo
Created February 8, 2023 20:47
Show Gist options
  • Save athossampayo/bcf532a27363d6025afcbde0590179c1 to your computer and use it in GitHub Desktop.
Save athossampayo/bcf532a27363d6025afcbde0590179c1 to your computer and use it in GitHub Desktop.
Python boto3 S3 Snippets
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()
# 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)
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