Skip to content

Instantly share code, notes, and snippets.

@lukeplausin
Created December 31, 2016 16:07
Show Gist options
  • Save lukeplausin/2a3cd5e92d6d4267a8999de5af1aaf4b to your computer and use it in GitHub Desktop.
Save lukeplausin/2a3cd5e92d6d4267a8999de5af1aaf4b to your computer and use it in GitHub Desktop.
Adds tags for creation date to objects in an S3 bucket. Useful if you've just used `aws s3 sync` and want to add metadata.
from __future__ import print_function
import os
import sys
from datetime import datetime
import boto3
walk_dir = "/Volumes/MyFiles"
client = boto3.client('s3')
bucket = "MyBucket"
print('walk_dir = ' + walk_dir)
print('walk_dir (absolute) = ' + os.path.abspath(walk_dir))
for root, subdirs, files in os.walk(walk_dir):
print('--\nroot = ' + root)
for filename in files:
try:
file_path = os.path.join(root, filename)
time = datetime.utcfromtimestamp(os.path.getmtime(file_path))
key = os.path.relpath(file_path, walk_dir)
print("Created: {1}, Key: {0};".format(key, time))
client.put_object_tagging(
Bucket=bucket, Key=key,
Tagging={"TagSet": [
{
"Key": "CreationDate",
"Value": unicode(time)
}
]}
)
except Exception as e:
print(e, file=sys.stderr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment