-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
205 lines (167 loc) · 3.94 KB
/
main.py
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#
# Main program for photoapp program using AWS S3 and RDS to
# implement a simple photo application for photo storage and
# viewing.
#
# Authors:
# YOUR NAME
# Prof. Joe Hummel (initial template)
# Northwestern University
#
import datatier # MySQL database access
import awsutil # helper functions for AWS
import boto3 # Amazon AWS
import uuid
import pathlib
import logging
import sys
import os
from configparser import ConfigParser
import matplotlib.pyplot as plt
import matplotlib.image as img
###################################################################
#
# prompt
#
def prompt():
"""
Prompts the user and returns the command number
Parameters
----------
None
Returns
-------
Command number entered by user (0, 1, 2, ...)
"""
try:
print()
print(">> Enter a command:")
print(" 0 => end")
print(" 1 => stats")
print(" 2 => users")
print(" 3 => assets")
print(" 4 => download")
print(" 5 => download and display")
print(" 6 => upload")
print(" 7 => add user")
cmd = int(input())
return cmd
except Exception as e:
print("ERROR")
print("ERROR: invalid input")
print("ERROR")
return -1
###################################################################
#
# stats
#
def stats(bucketname, bucket, endpoint, dbConn):
"""
Prints out S3 and RDS info: bucket name, # of assets, RDS
endpoint, and # of users and assets in the database
Parameters
----------
bucketname: S3 bucket name,
bucket: S3 boto bucket object,
endpoint: RDS machine name,
dbConn: open connection to MySQL server
Returns
-------
nothing
"""
#
# bucket info:
#
try:
print("S3 bucket name:", bucketname)
assets = bucket.objects.all()
print("S3 assets:", len(list(assets)))
#
# MySQL info:
#
print("RDS MySQL endpoint:", endpoint)
sql = """
select now();
"""
row = datatier.retrieve_one_row(dbConn, sql)
if row is None:
print("Database operation failed...")
elif row == ():
print("Unexpected query failure...")
else:
print(row[0])
except Exception as e:
print("ERROR")
print("ERROR: an exception was raised and caught")
print("ERROR")
print("MESSAGE:", str(e))
#########################################################################
# main
#
print('** Welcome to PhotoApp **')
print()
# eliminate traceback so we just get error message:
sys.tracebacklimit = 0
#
# what config file should we use for this session?
#
config_file = 'photoapp-config.ini'
print("What config file to use for this session?")
print("Press ENTER to use default (photoapp-config.ini),")
print("otherwise enter name of config file>")
s = input()
if s == "": # use default
pass # already set
else:
config_file = s
#
# does config file exist?
#
if not pathlib.Path(config_file).is_file():
print("**ERROR: config file '", config_file, "' does not exist, exiting")
sys.exit(0)
#
# gain access to our S3 bucket:
#
s3_profile = 's3readwrite'
os.environ['AWS_SHARED_CREDENTIALS_FILE'] = config_file
boto3.setup_default_session(profile_name=s3_profile)
configur = ConfigParser()
configur.read(config_file)
bucketname = configur.get('s3', 'bucket_name')
s3 = boto3.resource('s3')
bucket = s3.Bucket(bucketname)
#
# now let's connect to our RDS MySQL server:
#
endpoint = configur.get('rds', 'endpoint')
portnum = int(configur.get('rds', 'port_number'))
username = configur.get('rds', 'user_name')
pwd = configur.get('rds', 'user_pwd')
dbname = configur.get('rds', 'db_name')
dbConn = datatier.get_dbConn(endpoint, portnum, username, pwd, dbname)
if dbConn is None:
print('**ERROR: unable to connect to database, exiting')
sys.exit(0)
#
# main processing loop:
#
cmd = prompt()
while cmd != 0:
#
if cmd == 1:
stats(bucketname, bucket, endpoint, dbConn)
#
#
# TODO
#
#
else:
print("** Unknown command, try again...")
#
cmd = prompt()
#
# done
#
print()
print('** done **')