-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
67 lines (53 loc) · 1.91 KB
/
main.py
File metadata and controls
67 lines (53 loc) · 1.91 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
import os
from flask import Flask, jsonify, request
from google.appengine.api import blobstore
from google.appengine.api import images
from google.appengine.api import wrap_wsgi_app
app = Flask(__name__)
app.wsgi_app = wrap_wsgi_app(app.wsgi_app)
BUCKET_NAME = os.environ.get("BUCKET_NAME", "YOUR-PROJECT_ID.appspot.com")
@app.after_request
def _add_cors(response):
response.headers["Access-Control-Allow-Origin"] = "*"
return response
def _get_image_param():
image = request.args.get("image")
if not image or not image.strip():
return None
return image
@app.get("/")
def get_image_url():
image = _get_image_param()
if image is None:
return jsonify({"message": "Bad request"}), 400
gcs_path = f"/gs/{BUCKET_NAME}/{image}"
blob_key = blobstore.create_gs_key(gcs_path)
try:
image_url = images.get_serving_url(blob_key, secure_url=True)
except images.ObjectNotFoundError:
return jsonify({"message": "Not found"}), 404
except Exception as exc:
app.logger.exception(
"Images API unexpected error type=%s message=%s", type(exc), exc
)
return jsonify({"message": "Internal server error"}), 500
return jsonify({"url": image_url})
@app.delete("/")
def delete_image_url():
image = _get_image_param()
if image is None:
return jsonify({"message": "Bad request"}), 400
gcs_path = f"/gs/{BUCKET_NAME}/{image}"
blob_key = blobstore.create_gs_key(gcs_path)
try:
images.delete_serving_url(blob_key)
except images.ObjectNotFoundError:
return jsonify({"message": "Not found"}), 404
except Exception as exc:
app.logger.exception(
"Images API unexpected error type=%s message=%s", type(exc), exc
)
return jsonify({"message": "Internal server error"}), 500
return jsonify({})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8080, debug=True)