Created
May 22, 2018 15:56
-
-
Save ps2/4508a157f648f10fa7524124000eadbc to your computer and use it in GitHub Desktop.
Utility script to move a bunch of files in s3
This file contains hidden or 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
#!/usr/bin/env python3 | |
import boto3 | |
from argparse import ArgumentParser | |
import os | |
import re | |
import time | |
import pytz | |
from datetime import datetime | |
def get_aws_account_id(): | |
sts = boto3.client("sts") | |
user_arn = sts.get_caller_identity()["Arn"] | |
return user_arn.split("/")[1] | |
def rename_files(): | |
classroom_timezone = pytz.timezone("US/Eastern") | |
utc = pytz.timezone("UTC") | |
prefix='camera-aster/camera/' | |
bucket_name = 'wf-classroom-data' | |
s3 = boto3.resource('s3') | |
account_id = get_aws_account_id() | |
bucket = s3.Bucket(bucket_name) | |
for object in bucket.objects.filter(Prefix=prefix): | |
# camera-aster/camera/2018-03-15/camera01/still_2018-03-15-10-06-40.jpg | |
print(object.key) | |
datetime(2011, 2, 11, 20, 0) | |
m = re.match(prefix + "....-..-../(\S+)/still_(\d{4})-(\d{2})-(\d{2})-(\d{2})-(\d{2})-(\d{2}).jpg", object.key) | |
if m: | |
camera = m.groups()[0] | |
date_components = [int(c) for c in m.groups()[1:]] | |
new_time = classroom_timezone.localize(datetime(*date_components)).astimezone(utc) | |
new_key = prefix + "{timestamp:%Y-%m-%d}/{camera}/still_{timestamp:%Y-%m-%d-%H-%M-%S}Z.jpg".format(timestamp = new_time, camera=camera) | |
print(new_key) | |
s3.Object(bucket_name,new_key).copy_from(CopySource=bucket_name + "/" + object.key) | |
object.delete() | |
break | |
def rename_files2(): | |
classroom_timezone = pytz.timezone("US/Eastern") | |
utc = pytz.timezone("UTC") | |
prefix='camera-aster/camera/' | |
bucket_name = 'wf-classroom-data' | |
s3 = boto3.resource('s3') | |
account_id = get_aws_account_id() | |
bucket = s3.Bucket(bucket_name) | |
for object in bucket.objects.filter(Prefix=prefix): | |
# camera-aster/camera/2018-03-15/camera01/still_2018-03-15-10-06-40.jpg | |
print(object.key) | |
datetime(2011, 2, 11, 20, 0) | |
m = re.match(prefix + "....-..-../(\S+)/still_(\d{4})-(\d{2})-(\d{2})-(\d{2})-(\d{2})-(\d{2})Z.jpg", object.key) | |
if m: | |
camera = m.groups()[0] | |
date_components = [int(c) for c in m.groups()[1:]] | |
photo_time = datetime(*date_components) | |
new_key = prefix + "{timestamp:%Y-%m-%d}/{camera}/still_{timestamp:%Y-%m-%d-%H-%M-%S}.jpg".format(timestamp = photo_time, camera=camera) | |
print(new_key) | |
#s3.Object(bucket_name,new_key).copy_from(CopySource=bucket_name + "/" + object.key) | |
#object.delete() | |
break | |
if __name__ == '__main__': | |
rename_files2() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment