Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support downloading #78

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions jupyterlab_s3_browser/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,47 @@ def convertS3FStoJupyterFormat(result):
"type": result["type"],
}

class FilesHandler(APIHandler):
"""
Handles requests for getting files (e.g. for downloading)
"""

class S3Handler(APIHandler):
@property
def config(self):
return self.settings["s3_config"]

@tornado.web.authenticated
def get(self, path=""):
"""
Takes a path and returns lists of files/objects
and directories/prefixes based on the path.
"""
path = path.removeprefix("/")

try:
if not self.s3fs:
self.s3fs = create_s3fs(self.config)

self.s3fs.invalidate_cache()

with self.s3fs.open(path, "rb") as f:
result = f.read()

except S3ResourceNotFoundException as e:
result = json.dumps({
"error": 404,
"message": "The requested resource could not be found.",
})
except Exception as e:
logging.error("Exception encountered during GET {}: {}".format(path, e))
result = json.dumps({"error": 500, "message": str(e)})

self.finish(result)

s3fs = None
s3_resource = None

class ContentsHandler(APIHandler):
"""
Handles requests for getting S3 objects
"""
Expand Down Expand Up @@ -388,6 +427,7 @@ def setup_handlers(web_app):
base_url = web_app.settings["base_url"]
handlers = [
(url_path_join(base_url, "jupyterlab_s3_browser", "auth(.*)"), AuthHandler),
(url_path_join(base_url, "jupyterlab_s3_browser", "files(.*)"), S3Handler),
(url_path_join(base_url, "jupyterlab_s3_browser", "contents(.*)"), ContentsHandler),
(url_path_join(base_url, "jupyterlab_s3_browser", "files(.*)"), FilesHandler),
]
web_app.add_handlers(host_pattern, handlers)
12 changes: 4 additions & 8 deletions src/contents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import { DocumentRegistry } from '@jupyterlab/docregistry';

import { Contents, ServerConnection } from '@jupyterlab/services';

import { URLExt } from '@jupyterlab/coreutils';

import * as base64js from 'base64-js';

import * as s3 from './s3';

import { Dialog, showDialog } from '@jupyterlab/apputils';

/**
* A Contents.IDrive implementation for s3-api-compatible object storage.
*/
Expand Down Expand Up @@ -129,12 +129,8 @@ export class S3Drive implements Contents.IDrive {
* path if necessary.
*/
async getDownloadUrl(path: string): Promise<string> {
await showDialog({
title: 'Sorry',
body: 'This feature is not yet implemented.',
buttons: [Dialog.cancelButton({ label: 'Cancel' })]
});
throw Error('Not yet implemented');
const settings = ServerConnection.makeSettings();
return URLExt.join(settings.baseUrl, 'jupyterlab_s3_browser/files', path);
}

/**
Expand Down
17 changes: 8 additions & 9 deletions src/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function copyFile(
const settings = ServerConnection.makeSettings(); // can be stored as class var
const response = await (
await ServerConnection.makeRequest(
URLExt.join(settings.baseUrl, 'jupyterlab_s3_browser/files', newPath),
URLExt.join(settings.baseUrl, 'jupyterlab_s3_browser/contents', newPath),
{ method: 'PUT', headers: { 'X-Custom-S3-Copy-Src': oldPath } },
settings
)
Expand All @@ -29,7 +29,7 @@ export async function moveFile(
const settings = ServerConnection.makeSettings(); // can be stored as class var
const response = await (
await ServerConnection.makeRequest(
URLExt.join(settings.baseUrl, 'jupyterlab_s3_browser/files', newPath),
URLExt.join(settings.baseUrl, 'jupyterlab_s3_browser/contents', newPath),
{ method: 'PUT', headers: { 'X-Custom-S3-Move-Src': oldPath } },
settings
)
Expand All @@ -42,7 +42,7 @@ export async function deleteFile(path: string): Promise<any> {
const settings = ServerConnection.makeSettings(); // can be stored as class var
const response = await (
await ServerConnection.makeRequest(
URLExt.join(settings.baseUrl, 'jupyterlab_s3_browser/files', path),
URLExt.join(settings.baseUrl, 'jupyterlab_s3_browser/contents', path),
{ method: 'DELETE' },
settings
)
Expand All @@ -58,7 +58,7 @@ export async function writeFile(
const settings = ServerConnection.makeSettings(); // can be stored as class var
const response = await (
await ServerConnection.makeRequest(
URLExt.join(settings.baseUrl, 'jupyterlab_s3_browser/files', path),
URLExt.join(settings.baseUrl, 'jupyterlab_s3_browser/contents', path),
{ method: 'PUT', body: JSON.stringify({ content }) },
settings
)
Expand All @@ -70,7 +70,7 @@ export async function createDirectory(path: string): Promise<Contents.IModel> {
const settings = ServerConnection.makeSettings(); // can be stored as class var
await (
await ServerConnection.makeRequest(
URLExt.join(settings.baseUrl, 'jupyterlab_s3_browser/files', path),
URLExt.join(settings.baseUrl, 'jupyterlab_s3_browser/contents', path),
{ method: 'PUT', headers: { 'X-Custom-S3-Is-Dir': 'true' } },
settings
)
Expand All @@ -97,7 +97,7 @@ export async function get(
const settings = ServerConnection.makeSettings(); // can be stored as class var
const response = await (
await ServerConnection.makeRequest(
URLExt.join(settings.baseUrl, 'jupyterlab_s3_browser/files', path),
URLExt.join(settings.baseUrl, 'jupyterlab_s3_browser/contents', path),
{ method: 'GET' },
settings
)
Expand All @@ -124,7 +124,7 @@ export async function ls(path: string): Promise<Contents.IModel> {
const settings = ServerConnection.makeSettings(); // can be stored as class var
const response = await (
await ServerConnection.makeRequest(
URLExt.join(settings.baseUrl, 'jupyterlab_s3_browser/files', path),
URLExt.join(settings.baseUrl, 'jupyterlab_s3_browser/contents', path),
{ method: 'GET', headers: { 'X-Custom-S3-Is-Dir': 'true' } },
settings
)
Expand All @@ -146,11 +146,10 @@ export async function ls(path: string): Promise<Contents.IModel> {
}

export async function read(path: string): Promise<Contents.IModel> {
// pass
const settings = ServerConnection.makeSettings(); // can be stored as class var
const response = (
await ServerConnection.makeRequest(
URLExt.join(settings.baseUrl, 'jupyterlab_s3_browser/files', path),
URLExt.join(settings.baseUrl, 'jupyterlab_s3_browser/contents', path),
{ method: 'GET' },
settings
)
Expand Down