> ## Content Index
> Fetch the complete content index at: https://www.bagz.fr/llms.txt
> Use this file to discover other available public pages before exploring further.

# Python - Browse directory and push on S3 (with regex)
- URL: https://www.bagz.fr/python-browse-directory-and-push-to-s3-regex/
- Published: 2015-09-18T10:15:02.000Z
- Updated: 2015-09-18T10:15:02.000Z
- Author: bgazui
- Tags: Python, #Migrated-1790668826633, #wp, #wp-post, #Import 2026-09-29 08:00

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)