make getpeername() return the original socket address which before it was intercepted
[hband-tools.git] / aws / s3_quota / lambda_function.py
blob9bc644a93367c0bf1e57cd2558ee17023bdfbef9
2 from __future__ import print_function
4 import json
5 import urllib
6 import boto3
7 import traceback
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('/')
20 UserId = None
21 Identity = event['Records'][0]['userIdentity']['principalId']
22 if Identity.startswith('AWS:'):
23 if Identity[4:].find(':')==-1:
24 UserId = Identity[4:]
25 else:
26 UserId = Identity
27 Tagname = "Creator"
28 Tagvalue = None
30 try:
31 if UserId is None:
32 raise Exception("PrincipalId '%s' is not a UserId" % (Identity))
33 else:
34 userlist = iam.list_users()
35 for user in userlist['Users']:
36 if user['UserId'] == UserId:
37 Tagvalue = user['UserName']
38 break
39 if Tagvalue is None:
40 raise Exception("No UserName found for UserId '%s' %s" % (Identity, repr(userlist)))
41 except:
42 traceback.print_exc()
43 Tagname = "CreatorIdentity"
44 Tagvalue = Identity
46 Tagging = {
47 'TagSet': [
48 {'Key': Tagname, 'Value': Tagvalue},
52 s3.put_object_tagging(Bucket=Bucket, Key=Key, Tagging=Tagging)