2 from __future__
import print_function
9 print('Loading function')
11 s3
= boto3
.client('s3')
12 iam
= boto3
.client('iam')
15 def lambda_handler(event
, context
):
16 #print("Received event: " + json.dumps(event, indent=2))
18 Bucket
= event
['Records'][0]['s3']['bucket']['name']
19 Key
= urllib
.unquote_plus(event
['Records'][0]['s3']['object']['key'].encode('utf8')).strip('/')
21 Identity
= event
['Records'][0]['userIdentity']['principalId']
22 if Identity
.startswith('AWS:'):
23 if Identity
[4:].find(':')==-1:
32 raise Exception("PrincipalId '%s' is not a UserId" % (Identity
))
34 userlist
= iam
.list_users()
35 for user
in userlist
['Users']:
36 if user
['UserId'] == UserId
:
37 Tagvalue
= user
['UserName']
40 raise Exception("No UserName found for UserId '%s' %s" % (Identity
, repr(userlist
)))
43 Tagname
= "CreatorIdentity"
48 {'Key': Tagname
, 'Value': Tagvalue
},
52 s3
.put_object_tagging(Bucket
=Bucket
, Key
=Key
, Tagging
=Tagging
)