Skip to content

Instantly share code, notes, and snippets.

@michalc
michalc / s3_bulk_delete.py
Last active March 8, 2025 10:49
Bulk delete files from an AWS S3 bucket in Python using multiple threads via a parallel (eventually) depth-first search
# Deletes objects in bulk using boto3's delete_objects, but using multiple threads to achieve some
# parallelism - in spite of the GIL multiple HTTP requests to S3 should happen at the same time.
# Instead of looping over all keys under the root prefix, it walks the tree of keys of delimiter-
# defined "folders" in a depth-first way, which allows each page to be processed by a separate
# thread as its discovered. Depth-first is done because the depth is limited by the maximum key
# size of 1024 in S3, and so means that there is a limit to the memory used by the algorithm to
# store the next requests to make. This would not be the case with breadth-first because there is
# no limit to how many keys are in any folder.
#
# To do the search in parallel, each bit of work (i.e. an HTTP request to fetch a page of keys
@michalc
michalc / cloudfoundry-check-stacks.py
Created November 14, 2023 07:38
Check the stack of apps in CloudFoundry
import json
import os
from urllib.parse import urlparse, parse_qsl
import requests
from rich import box
from rich.console import Console
from rich.table import Table
with open(f'{os.environ["HOME"]}/.cf/config.json') as f:
@michalc
michalc / block_or_not.py
Created April 24, 2024 13:34
Script for checking if any PostgreSQL session blocks another
# docker run --rm -it -p 5432:5432 -e POSTGRES_PASSWORD=password postgres:14
import pprint
import threading
import time
from concurrent.futures import ThreadPoolExecutor
from contextlib import contextmanager
import psycopg2
import psycopg2.extras
@michalc
michalc / restore.py
Last active July 17, 2024 16:15
Restore all objects under S3 prefix in versioned bucket
# Restores all objects under a prefix in an S3 bucket to the version they were before being deleted.
# Works by deleting the latest delete marker for the objects.
import boto3
bucket = 'the-bucket'
prefix = 'the-prefix/'
region = 'eu-west-2'
client = boto3.client('s3', region_name=region)
paginator = client.get_paginator('list_object_versions')
@michalc
michalc / elevator-saga.js
Last active September 1, 2024 14:01
My Elevator Saga solution
{
init: function(elevators, floors) {
const intSort = function(a, b) {
if (a < b) return -1;
if (a > b) return 1;
return 0;
}
const floorWantsUp = function(floorNum) {
@michalc
michalc / add-prod-tag-suffix.py
Created December 10, 2024 10:47
Add a "--prod" tag suffix to existing ECR images
from pprint import pprint
import boto3
repository_name = 'REPLACE_ME'
client = boto3.client('ecr', region_name='eu-west-2')
image_details = (
image_detail
for page in client.get_paginator('describe_images').paginate(