Skip to content

Instantly share code, notes, and snippets.

View toshke's full-sized avatar

Nikola Tosic toshke

  • Buffer Overflow
  • Melbourne, Australia
  • X @thetoske
View GitHub Profile
import boto3
def pull_s3_prefix(dst_dir, bucket, prefix):
client = boto3.client('s3')
resource = boto3.resource('s3')
download_dir(client, resource, prefix, prefix, dst_dir, bucket)
def download_dir(client, resource, prefix, start_prefix, local, bucket ):
paginator = client.get_paginator('list_objects')
for result in paginator.paginate(Bucket=bucket, Delimiter='/', Prefix=prefix):
@toshke
toshke / s3copy.py
Created February 16, 2018 02:49
copy from s3 to s3
import boto3
import os
import zipfile
import glob
import logging
import shutil
logger = logging.getLogger()
logger.setLevel(logging.INFO)
@toshke
toshke / dynamodb_copy_table_items
Last active January 31, 2024 19:27
Copy DynamoDB table items from one table to other
#!/usr/bin/env python
import boto3
import sys
####
#### usage copy_dynamo_items.py <srcTable> <dstTable> [dstTableRegion] [dstTableProfile]
#### source table region will get picked up from environment variables
#### if target table region is different than source region, pass it as 3rd argument to script
#### source table credentials are picked up from environment
#### if target table credentials are different from source, pass it as 4rd argument to the script as local profile name
@toshke
toshke / clean-dynamo-table
Last active June 15, 2023 13:08 — forked from k3karthic/truncate_dynamodb.sh
Truncate all keys in a dynamodb table
#!/bin/bash
####
#### Delete (remove) all items from Aws Dynamo DB table, by specifing table name and primary key
####
#### Forked from https://gist.github.com/k3karthic/4bc929885eef40dbe010
####
#### Usage:
#### clean-dynamo-table TABLE_NAME PRIMARY_KEY
####
@toshke
toshke / pynamo_model_serialize_json.py
Created March 7, 2018 02:31
PynamoDB serialize models to json
from pynamodb.models import Model
from pynamodb.attributes import UnicodeAttribute, ListAttribute, MapAttribute
import os
import json
class BaseModel(Model):
def to_dict(self):
rval = {}
for key in self.attribute_values:
@toshke
toshke / list_active_aws.py
Created December 24, 2021 08:22
list active aws profiles
#!/usr/bin/env python3
import boto3
import os
from botocore.exceptions import ClientError
KEY_ID = 'aws_access_key_id'
SECRET_KEY = 'aws_secret_access_key'
SESSION_TOKEN = 'aws_session_token'
@toshke
toshke / rotate_aws_keys.py
Last active July 6, 2021 00:27
Rotate AWS access keys for a profile python script
#!/usr/bin/env python3
import sys
import os
import boto3
import shlex
def rotate(profile: str):
sts = boto3.client('sts')
iam = boto3.client('iam')
user_arn = sts.get_caller_identity()['Arn']
@toshke
toshke / pipeline.groovy
Last active June 20, 2021 16:39
Declarative jenkins pipeline example
pipeline {
//run by default on docker-in-docker slave
agent {
label 'docker'
}
stages {
//demo of running builds within custom container
stage('PrintNodeVersion') {
@toshke
toshke / assume-role.sh
Created April 20, 2021 06:29
assume-role
#!/bin/bash
set -eox pipefail
if [ "$1" == "" ]; then
echo "usage: assume-role arn [session-name=nikolatosic\$(date +%s)] [duration=900]"
exit 1
fi
if [ "$2" == "" ]; then
echo "usage: assume-role arn [session-name\$(date +%s)] [duration=900]"
session_name="nikolatosic$(date +%s)"
fi
@toshke
toshke / crupdate.py
Last active December 22, 2020 09:15
crupdate custom resource
physical_id = get_physical_id(event)
if request_type == 'Create' or request_type == 'Update:
if resource_exists(physical_id):
print(f'{physical_id} will be overwritten')
create_resource()
response.success(physical_id)