:::: MENU ::::

Monthly Archives / September 2015

  • Sep 30 / 2015
  • 0
Linux

Apache – Proxy some URLs to another server

In Apache, if you want to forward some requests matching a regex to another server, you can use the rule ProxyPassMatch.

Here is an example that is matching URL like:

  • /admin/anything
  • /list/any number/info
  • /manage/any number

and redirect it to https://my-hidden-domain.com:26000

ProxyPreserveHost On
ProxyRequests Off
ProxyPassMatch ^/(admin/.*@.*|list/[0-9]*/info|manage/[0-9]*)$ https://my-hidden-domain.com:26000/$1

You can then define any rule you want with multiple rules matching different regex.ègles que vous voulez avec différentes expressions régulières.

  • Sep 18 / 2015
  • 0
Python

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)


Question ? Contact