Skip to content

Commit ff6dd9e

Browse files
committed
switch to async logic for region extraction
1 parent c7a824f commit ff6dd9e

File tree

1 file changed

+8
-18
lines changed

1 file changed

+8
-18
lines changed

jupyter_drives/manager.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from libcloud.storage.types import Provider
1717
from libcloud.storage.providers import get_driver
1818
import pyarrow
19-
import boto3
19+
from aiobotocore.session import get_session
2020

2121
from .log import get_logger
2222
from .base import DrivesConfig
@@ -42,19 +42,10 @@ def __init__(self, config: traitlets.config.Config) -> None:
4242
self._content_managers = {}
4343
self._max_files_listed = 1000
4444

45-
# initiate boto3 session if we are dealing with S3 drives
45+
# initiate aiobotocore session if we are dealing with S3 drives
4646
if self._config.provider == 's3':
4747
self._s3_clients = {}
48-
if self._config.access_key_id and self._config.secret_access_key:
49-
if self._config.session_token is None:
50-
self._s3_session = boto3.Session(aws_access_key_id = self._config.access_key_id, aws_secret_access_key = self._config.secret_access_key)
51-
else:
52-
self._s3_session = boto3.Session(aws_access_key_id = self._config.access_key_id, aws_secret_access_key = self._config.secret_access_key, aws_session_token = self._config.session_token)
53-
else:
54-
raise tornado.web.HTTPError(
55-
status_code= httpx.codes.BAD_REQUEST,
56-
reason="No credentials specified. Please set them in your user jupyter_server_config file.",
57-
)
48+
self._s3_session = get_session()
5849

5950
@property
6051
def base_api_url(self) -> str:
@@ -149,7 +140,7 @@ async def mount_drive(self, drive_name, provider):
149140
if drive_name not in self._content_managers or self._content_managers[drive_name] is None:
150141
if provider == 's3':
151142
# get region of drive
152-
region = self._get_drive_location(drive_name)
143+
region = await self._get_drive_location(drive_name)
153144
if self._config.session_token is None:
154145
configuration = {
155146
"aws_access_key_id": self._config.access_key_id,
@@ -555,7 +546,7 @@ async def presigned_link(self, drive_name, path):
555546
}
556547
return response
557548

558-
def _get_drive_location(self, drive_name):
549+
async def _get_drive_location(self, drive_name):
559550
"""Helping function for getting drive region.
560551
561552
Args:
@@ -564,10 +555,9 @@ def _get_drive_location(self, drive_name):
564555
location = 'eu-north-1'
565556
try:
566557
# set temporary client for location extraction
567-
s3 = self._s3_session.client('s3')
568-
result = s3.get_bucket_location(Bucket = drive_name)
569-
570-
location = result['LocationConstraint']
558+
async with self._s3_session.create_client('s3', aws_secret_access_key=self._config.secret_access_key, aws_access_key_id=self._config.access_key_id, aws_session_token=self._config.session_token) as client:
559+
result = await client.get_bucket_location(Bucket=drive_name)
560+
location = result['LocationConstraint']
571561
except Exception as e:
572562
raise tornado.web.HTTPError(
573563
status_code= httpx.codes.BAD_REQUEST,

0 commit comments

Comments
 (0)