forked from stackgen-demo/ImagesRepo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
48 lines (40 loc) · 1.47 KB
/
app.py
File metadata and controls
48 lines (40 loc) · 1.47 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
from flask import Flask, request, redirect, url_for, render_template, jsonify
import boto3
import os
import random
from botocore.config import Config
app = Flask(__name__)
@app.route('/health')
def health():
return jsonify({"status": "healthy"})
@app.route('/')
def index():
return render_template('index.html')
@app.route('/image', methods=['GET'])
def view():
bucket = os.environ["IMAGES_BUCKET"]
config = Config(signature_version='s3v4')
s3_client = boto3.client("s3",config=config)
image_name = request.args.get('image_name')
image_key = f"{image_name}.jpg"
try:
image_url = s3_client.generate_presigned_url(
"get_object", Params={"Bucket": bucket, "Key": image_key}, ExpiresIn=300
)
except Exception as e:
return "Error downloading image from S3: {}".format(str(e)), 500
return render_template('image.html', image_url=image_url, image_name=image_name),200
@app.route('/upload', methods=['POST'])
def upload():
bucket = os.environ["IMAGES_BUCKET"]
config = Config(signature_version='s3v4')
s3_client = boto3.client("s3",config=config)
file = request.files['file']
if file:
try:
s3_client.upload_fileobj(file, bucket, file.filename)
except Exception as e:
return "Error uploading image to S3: {}".format(str(e)), 500
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)