Python – Browse directory and push on S3 (with regex)

A simple script that can browse a directory, and upload to S3 some files matching a regex.
The file will be uploaded by respecting the path you have in local.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, re
import boto
from boto.s3.connection import S3Connection
from boto.s3.key import Key

# Variables
AWS_ACCESS_KEY_ID     = 'YOUR_AWS_ACCESS_KEY_ID'
AWS_SECRET_ACCESS_KEY = 'YOUR_AWS_SECRET_ACCESS_KEY'
AWS_BUCKET_NAME       = 'YOUR_BUCKET_NAME'
DIR_TO_SCAN			  = '/path/to/your/directory/'

# Prepare regex 
r = re.compile("[0-9]+\.jpg$")

# Open connection to S3
conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
b = conn.get_bucket(AWS_BUCKET_NAME)
k = Key(b)

for root, directories, filenames in os.walk(DIR_TO_SCAN):
    for filename in filenames:
        if r.match(os.path.join(root,filename)):
            print os.path.join(root,filename)
            # Remove the full path
            tp = os.path.join(root,filename).split(DIR_TO_SCAN)[1]
            # Push file to S3
            k.key = tp
            size = k.set_contents_from_filename(os.path.join(root,filename), replace=False)
            print "%d bytes uploaded for %s"%(size, tp)