forked from yeshu4/aws-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmpApp.py
More file actions
161 lines (127 loc) · 4.88 KB
/
EmpApp.py
File metadata and controls
161 lines (127 loc) · 4.88 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from flask import Flask, render_template, request
from pymysql import connections
import os
import boto3
from config import *
app = Flask(__name__)
# DBHOST = os.environ.get("DBHOST")
# DBPORT = os.environ.get("DBPORT")
# DBPORT = int(DBPORT)
# DBUSER = os.environ.get("DBUSER")
# DBPWD = os.environ.get("DBPWD")
# DATABASE = os.environ.get("DATABASE")
bucket= custombucket
region= customregion
db_conn = connections.Connection(
host= customhost,
port=3306,
user= customuser,
password= custompass,
db= customdb
)
output = {}
table = 'employee';
@app.route("/", methods=['GET', 'POST'])
def home():
return render_template('AddEmp.html')
@app.route("/about", methods=['POST'])
def about():
return render_template('www.intellipaat.com');
@app.route("/addemp", methods=['POST'])
def AddEmp():
emp_id = request.form['emp_id']
first_name = request.form['first_name']
last_name = request.form['last_name']
pri_skill = request.form['pri_skill']
location = request.form['location']
emp_image_file = request.files['emp_image_file']
insert_sql = "INSERT INTO employee VALUES (%s, %s, %s, %s, %s)"
cursor = db_conn.cursor()
if emp_image_file.filename == "":
return "Please select a file"
try:
cursor.execute(insert_sql,(emp_id, first_name, last_name, pri_skill, location))
db_conn.commit()
emp_name = "" + first_name + " " + last_name
# Uplaod image file in S3 #
emp_image_file_name_in_s3 = "emp-id-"+str(emp_id) + "_image_file"
s3 = boto3.resource('s3')
try:
print("Data inserted in MySQL RDS... uploading image to S3...")
s3.Bucket(custombucket).put_object(Key=emp_image_file_name_in_s3, Body=emp_image_file)
bucket_location = boto3.client('s3').get_bucket_location(Bucket=custombucket)
s3_location = (bucket_location['LocationConstraint'])
if s3_location is None:
s3_location = ''
else:
s3_location = '-' + s3_location
object_url = "https://s3{0}.amazonaws.com/{1}/{2}".format(
s3_location,
custombucket,
emp_image_file_name_in_s3)
# Save image file metadata in DynamoDB #
print("Uploading to S3 success... saving metadata in dynamodb...")
try:
dynamodb_client = boto3.client('dynamodb', region_name='us-east-2')
dynamodb_client.put_item(
TableName='employee_image_table',
Item={
'empid': {
'N': emp_id
},
'image_url': {
'S': object_url
}
}
)
except Exception as e:
program_msg = "Flask could not update DynamoDB table with S3 object URL"
return str(e)
except Exception as e:
return str(e)
finally:
cursor.close()
print("all modification done...")
return render_template('AddEmpOutput.html', name=emp_name)
@app.route("/getemp", methods=['GET', 'POST'])
def GetEmp():
return render_template("GetEmp.html")
@app.route("/fetchdata", methods=['GET','POST'])
def FetchData():
emp_id = request.form['emp_id']
output = {}
select_sql = "SELECT emp_id, first_name, last_name, pri_skill, location from employee where emp_id=%s"
cursor = db_conn.cursor()
try:
cursor.execute(select_sql,(emp_id))
result = cursor.fetchone()
output["emp_id"] = result[0]
print('EVERYTHING IS FINE TILL HERE')
output["first_name"] = result[1]
output["last_name"] = result[2]
output["primary_skills"] = result[3]
output["location"] = result[4]
print(output["emp_id"])
dynamodb_client = boto3.client('dynamodb', region_name=customregion)
try:
response = dynamodb_client.get_item(
TableName='employee_image_table',
Key={
'empid': {
'N': str(emp_id)
}
}
)
image_url = response['Item']['image_url']['S']
except Exception as e:
program_msg = "Flask could not update DynamoDB table with S3 object URL"
return render_template('addemperror.html', errmsg1=program_msg, errmsg2=e)
except Exception as e:
print(e)
finally:
cursor.close()
return render_template("GetEmpOutput.html", id=output["emp_id"], fname=output["first_name"],
lname=output["last_name"], interest=output["primary_skills"], location=output["location"],
image_url=image_url)
if __name__ == '__main__':
app.run(host='0.0.0.0',port=80,debug=True)