Wednesday, June 4, 2014

Access S3 files meta data and other details using Python Boto using Assume Role

Here is the code

import sys
import boto
from boto.sts import STSConnection
from boto.s3.connection import S3Connection
from boto.s3.key import Key


sts_connection = STSConnection()
assumedRoleObject = sts_connection.assume_role(
    role_arn="arn:aws:iam::0000000000:role/xxxx/xxxx-yyyy-zzz",
    role_session_name="AssumeRoleSession1"
)

s3_connection = S3Connection(
    aws_access_key_id=assumedRoleObject.credentials.access_key,
    aws_secret_access_key=assumedRoleObject.credentials.secret_key,
    security_token=assumedRoleObject.credentials.session_token
)


bname="BUCKETNAME"
fname="FILENAME"
bucket = s3_connection.get_bucket(bname)
files=bucket.list(prefix=fname)

for file in files:
        k=bucket.get_key(file)
        print k.get_metadata('key-name')
        print file.name
        print file.size
        print file.last_modified
        print file.get_acl()
        print



1: Lets assume your key name is x-amz-meta-keyname then in the code pass your keyname as "keyname". No need to mention x-amz-meta

Let me know in comments section if his works/doesn't work for you

No comments:

Post a Comment