-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathlambda_s3_dynamodb_sample.txt
50 lines (42 loc) · 1.9 KB
/
lambda_s3_dynamodb_sample.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from __future__ import print_function
import json
import urllib
import boto3
print('Loading function')
s3 = boto3.client('s3')
def lambda_handler(event, context):
#print("Received event: " + json.dumps(event, indent=2))
# Get the object from the event and show its content type
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key'].encode('utf8'))
try:
response = s3.get_object(Bucket=bucket, Key=key)
print("#############################################################")
print(response)
print("CONTENT TYPE: " + response['ContentType'])
print("Key Size is : " + str(response['ContentLength']))
print("Key Name is : " + str(key))
print("Hello..Welcome to AWS Lamba function world. Kumar welcome you.....")
print("#############################################################")
print("calling dynamodb function...")
dynamodb = boto3.resource('dynamodb', region_name='us-east-1')
print("calling dynamodb table...")
table = dynamodb.Table('s3info')
print(table.creation_date_time)
print("Inserting the value..")
try:
response = table.put_item(
Item={
's3objectname': str(key),
'content type': response['ContentType'],
'size': str(response['ContentLength']),
'etag': str(response['ETag'])
})
except ClientError as e:
print(e.response['Error']['Message'])
print("PutItem succeeded:")
return str(response['ContentLength'])
except Exception as e:
print(e)
print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
raise e