-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy paths3.py
266 lines (224 loc) · 9.23 KB
/
s3.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import tornado
import httpx
from itertools import chain
from typing import Dict, List, Optional, Tuple, Union, Any
import traitlets
from jupyter_server.utils import url_path_join
from tornado.httputil import url_concat
from libcloud.storage.types import Provider
from libcloud.storage.providers import get_driver
from s3contents import S3ContentsManager
from ..base import DrivesConfig
from .manager import JupyterDrivesManager
class S3Manager(JupyterDrivesManager):
"""Jupyter drives manager for S3 drives."""
def __init__(self, config: traitlets.config.Config) -> None:
super().__init__(DrivesConfig(config=config))
self._drives_cache = {}
self.s3_content_managers = {}
@property
def base_api_url(self):
return self._config.api_base_url or "https://s3.amazonaws.com/"
@property
def per_page_argument(self) -> Optional[Tuple[str, int]]:
"""Returns query argument to set number of items per page.
Returns
[str, int]: (query argument name, value)
None: the provider does not support pagination
"""
return ("per_page", 100)
async def list_drives(self):
"""Get the list of available drives.
Returns:
The list of available drives
"""
data = []
if (self._config.access_key_id and self._config.secret_access_key):
S3Drive = get_driver(Provider.S3)
drives = [S3Drive(self._config.access_key_id, self._config.secret_access_key)]
results = []
for drive in drives:
results += drive.list_containers()
for result in results:
data.append(
{
"name": result.name,
"region": result.driver.region,
"creation_date": result.extra["creation_date"],
"status": "inactive",
"provider": "S3"
}
)
response = {
"data": data,
"code": 200
}
else:
response = {"code": 400}
raise tornado.web.HTTPError(
status_code= httpx.codes.BAD_REQUEST,
reason="No AWS credentials specified. Please set them in your user jupyter_server_config file.",
)
return response
async def mount_drive(self, drive_name):
'''Mount a drive by creating an S3ContentsManager for it.
Params:
drive_name: name of drive to mount
Args:
S3ContentsManager
'''
try :
s3_contents_manager = S3ContentsManager(
access_key_id = self._config.access_key_id,
secret_access_key = self._config.secret_access_key,
endpoint_url = self._config.api_base_url,
bucket = drive_name
)
# checking if the drive wasn't mounted already
if drive_name not in self.s3_content_managers or self.s3_content_managers[drive_name] is None:
# dealing with long-term credentials (access key, secret key)
if self._config.session_token is None:
s3_contents_manager = S3ContentsManager(
access_key_id = self._config.access_key_id,
secret_access_key = self._config.secret_access_key,
endpoint_url = self._config.api_base_url,
bucket = drive_name
)
# dealing with short-term credentials (access key, secret key, session token)
else:
s3_contents_manager = S3ContentsManager(
access_key_id = self._config.access_key_id,
secret_access_key = self._config.secret_access_key,
session_token = self._config.session_token,
endpoint_url = self._config.api_base_url,
bucket = drive_name
)
self.s3_content_managers[drive_name] = s3_contents_manager
response = {
"s3_contents_manager": s3_contents_manager,
"code": 201,
"message": "Drive successfully mounted."
}
else:
response = {"code": 409, "message": "Drive already mounted."}
except Exception as e:
response = {"code": 400, "message": e}
return response
async def unmount_drive(self, drive_name):
'''Unmount a drive.
Args:
drive_name: name of drive to unmount
'''
if drive_name in self.s3_content_managers:
self.s3_content_managers.pop(drive_name, None)
response = {"code": 204}
else:
response = {"code": 404}
raise tornado.web.HTTPError(
status_code= httpx.codes.BAD_REQUEST,
reason="Drive is not mounted or doesn't exist.",
)
async def get_contents(self, drive_name, path = ""):
'''Get contents of an S3 drive.
Args:
drive_name: name of drive to get contents of
path: path of file or directory to retrieve the contents of
Returns:
contents: contents of file or directory
'''
response = {}
try:
if drive_name in self.s3_content_managers:
contents = self.s3_content_managers[drive_name].fs.ls(path)
code = 200
response["contents"] = contents
else:
code = 404
response["message"] = "Drive doesn't exist or is not mounted."
except Exception as e:
code = 400
response["message"] = e
response["code"] = code
return response
async def new_file(self, drive_name, type = "notebook", path = ""):
'''Create a new file or directory from an S3 drive.
Args:
type: type of content to be created (notebook or directory)
drive_name: name of drive where new content should be created
path: path where new content should be created
'''
response = {}
try:
if drive_name in self.s3_content_managers:
new_content = self.s3_content_managers[drive_name].new_untitled(type)
code = 201
response["file_name"] = new_content["name"]
else:
code = 404
response["message"] = "Drive doesn't exist or is not mounted."
except Exception as e:
code = 400
response["message"] = e
response["code"] = code
return response
async def rename_file(self, new_file_name, drive_name, path = ""):
'''Rename a file from an S3 drive.
Args:
new_file_name: new name of file
drive_name: name of drive where new content should be created
path: path where new content should be created
'''
response = {}
try:
if drive_name in self.s3_content_managers:
new_file_path = url_path_join(path, new_file_name)
self.s3_content_managers[drive_name].rename_file(new_path = new_file_path, old_path = path)
code = 201
else:
code = 404
response["message"] = "Drive doesn't exist or is not mounted."
except Exception as e:
code = 400
response["message"] = e
response["code"] = code
return response
async def _call_s3(
self,
url: str,
load_json: bool = True,
method: str = "GET",
body: Optional[dict] = None,
params: Optional[Dict[str, str]] = None,
media_type: str = "application/xml",
has_pagination: bool = True,
) -> Union[dict, str]:
"""Call S3 provider
The request is presumed to support pagination by default if
- The method is GET
- load_json is True
- The provider returns not None per_page_argument property
Args:
url: Endpoint to request
load_json: Is the response of JSON type
method: HTTP method
body: Request body; None if no body
params: Query arguments as dictionary; None if no arguments
media_type: Type of accepted content
has_pagination: Whether the pagination query arguments should be appended
Returns:
List or Dict: Create from JSON response body if load_json is True
str: Raw response body if load_json is False
"""
headers = {
"Accept": media_type,
"Authorization": f"session-token {self._config.session_token_token} access-key-id {self._config.access_key_id} secret-access-key {self._config.secret_access_key}",
}
return await super()._call_provider(
url,
load_json=load_json,
method=method,
body=body,
params=params,
headers=headers,
has_pagination=has_pagination,
)