> ## 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.

# AWS - Update massively metadata using boto (python) on multiple S3 objects
- URL: https://www.bagz.fr/aws-update-massively-metadata-using-boto-python-on-multiple-s3-objects/
- Published: 2015-10-09T09:34:06.000Z
- Updated: 2015-10-09T09:34:06.000Z
- Author: bgazui
- Tags: Python, #Migrated-1790668826633, #wp, #wp-post, #Import 2026-09-29 08:00

A simple script that allows you to update massively Content-Type for files on an S3 bucket.  
This script is able to:

- Browse recursively a bucket
- Perform action only on files matching specific prefix
- Auto-detect type of file depending on its extension

Obviously, you can add any extension you want to the function *update\_md* to handle more if needed.#!/usr/bin/env python  
\# -\*- coding: utf-8 -\*-  
import os, re, sys  
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'

# Function to update MetaData

def update\_md(k):  
  
"""  
  
Update the metadata with an existing object.  
  
"""  
  
\# Get extension  
  
ext = k.name.split('.')\[-1\]  
  
if ext in \['bmp','BMP'\]:  
  
metadata = {'Content-Type':'image/bmp'}  
  
elif ext in \['jpg','jpeg','JPG','JPEG'\]:  
  
metadata = {'Content-Type':'image/jpeg'}  
  
elif ext in \['gif','GIF'\]:  
  
metadata = {'Content-Type':'image/gif'}  
  
elif ext in \['png','PNG'\]:  
  
metadata = {'Content-Type':'image/png'}  
  
elif ext in \['pdf','PDF'\]:  
  
metadata = {'Content-Type':'application/pdf'}  
  
elif ext in \['txt','TXT'\]:  
  
metadata = {'Content-Type':'text/plain'}  
  
elif ext in \['zip','ZIP'\]:  
  
metadata = {'Content-Type':'application/zip'}  
  
else:  
  
return  
  
\# If not same type -- update  
  
if metadata\['Content-Type'\] != akey.content\_type:  
  
akey.copy(AWS\_BUCKET\_NAME, k.name, metadata, preserve\_acl=True)  
  
return k

# Main function

if **name** \== '**main**':  
  
\# Connect to S3  
  
conn = boto.connect\_s3(AWS\_ACCESS\_KEY\_ID, AWS\_SECRET\_ACCESS\_KEY)  
  
b = conn.get\_bucket(AWS\_BUCKET\_NAME)

```
# Select files to parse (prefix can be specified)
rs = b.list(prefix="")

# Browse files
for k in rs:
    print k.name
    akey = b.get_key(k.name)
    # Print type before
    print "Before:",akey.content_type
    try:
        k = update_md(k)
        akey = b.get_key(k.name)
        print "After: ",akey.content_type
    except Exception,e:
        print "Content-Type not handled by this script"

print "Script finished!"

```