9
9
import os
10
10
from datetime import datetime
11
11
from urllib .parse import urlparse
12
+
12
13
import boto3
13
14
import botocore
15
+ from tornado import iostream
14
16
from tornado import web
15
17
16
18
from .. import _load_handler_from_location
@@ -24,17 +26,17 @@ class S3Handler(RenderingHandler):
24
26
25
27
Serving notebooks from S3 buckets
26
28
"""
29
+
27
30
def initialize (self , ** kwargs ):
28
31
self .s3_client = boto3 .client ("s3" )
29
32
self ._downloadable_data = None
30
33
self ._downloaded_path = None
31
34
super ().initialize (** kwargs )
32
35
33
-
34
36
async def download (self , path ):
35
37
"""Download the notebook"""
36
38
headers = await self .get_notebook_headers (path )
37
- filename = os .path .basename (path )
39
+ filename = os .path .basename (path )
38
40
self .set_header ("Content-Length" , headers ["ContentLength" ])
39
41
# Escape commas to workaround Chrome issue with commas in download filenames
40
42
self .set_header (
@@ -55,7 +57,6 @@ async def download(self, path):
55
57
except iostream .StreamClosedError :
56
58
return
57
59
58
-
59
60
async def get_notebook_data (self , path ):
60
61
"""Get additional notebook data"""
61
62
is_download = self .get_query_arguments ("download" )
@@ -65,7 +66,6 @@ async def get_notebook_data(self, path):
65
66
66
67
return path
67
68
68
-
69
69
async def get_notebook_headers (self , path ):
70
70
"""Get the size of a notebook file."""
71
71
o = urlparse (path )
@@ -81,7 +81,6 @@ async def get_notebook_headers(self, path):
81
81
raise ex
82
82
return head
83
83
84
-
85
84
async def read_s3_file (self , path ):
86
85
"""Download the notebook file from s3."""
87
86
o = urlparse (path )
@@ -98,11 +97,10 @@ async def read_s3_file(self, path):
98
97
raise ex
99
98
s3_file .seek (0 )
100
99
self .log .debug ("Done downloading." )
101
- self ._downloadable_data = s3_file .read ().decode (' utf-8' )
100
+ self ._downloadable_data = s3_file .read ().decode (" utf-8" )
102
101
self ._downloaded_path = path
103
102
return self ._downloadable_data
104
103
105
-
106
104
async def deliver_notebook (self , path ):
107
105
nbdata = await self .read_s3_file (path )
108
106
@@ -137,16 +135,15 @@ async def get(self, path):
137
135
await self .deliver_notebook (fullpath )
138
136
139
137
140
-
141
138
def default_handlers (handlers = [], ** handler_names ):
142
139
"""Tornado handlers"""
143
140
144
141
s3_handler = _load_handler_from_location (handler_names ["s3_handler" ])
145
142
146
143
return handlers + [(r"/(s3%3A//.*)" , s3_handler , {})]
147
144
145
+
148
146
def uri_rewrites (rewrites = []):
149
147
return [
150
148
(r"^(s3://.*)$" , "{0}" ),
151
149
]
152
-
0 commit comments