This repository has been archived by the owner on Sep 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
65 lines (52 loc) · 1.86 KB
/
app.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
import os
import boto3
from chalice import Chalice, Response, NotFoundError
from jinja2 import Environment, PackageLoader, select_autoescape
S3_BUCKET_NAME = os.environ.get("BUCKET_NAME")
# chalice
app = Chalice(app_name="lapypi")
# jinja2
env = Environment(
loader=PackageLoader("app", "chalicelib/templates"),
autoescape=select_autoescape(["html", "xml"]),
)
# boto3
s3_client = boto3.client("s3")
def get_presigned_url(key):
"""Generate short-lived URL for getting the package."""
url = s3_client.generate_presigned_url(
"get_object",
ExpiresIn=300,
Params={"Bucket": S3_BUCKET_NAME, "Key": key},
HttpMethod="GET",
)
return url
@app.route("/", methods=["GET"])
def simple():
template = env.get_template("simple.html")
s3_root_objects = s3_client.list_objects_v2(Bucket=S3_BUCKET_NAME, Delimiter="/")
package_list = [obj["Prefix"] for obj in s3_root_objects["CommonPrefixes"]]
return Response(
body=template.render(package_list=package_list),
status_code=200,
headers={"Content-Type": "text/html"},
)
@app.route("/{package_name}", methods=["GET"])
def package(package_name):
template = env.get_template("package.html")
paginator = s3_client.get_paginator("list_objects_v2")
pages = paginator.paginate(Bucket=S3_BUCKET_NAME, Prefix=f"{package_name}/")
keys = [key.get("Key") for page in pages for key in page.get("Contents") or []]
if not keys:
raise NotFoundError
urls = [get_presigned_url(key) for key in keys]
names = [os.path.split(key)[-1] for key in keys]
items = [(href, name) for href, name in zip(urls, names)]
# drop S3 folder element
if items:
del items[0]
return Response(
body=template.render(package_name=package_name, items=items),
status_code=200,
headers={"Content-Type": "text/html"},
)