-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for accessing notebook files(#319)
Co-authored-by: Jonatan Kłosko <[email protected]>
- Loading branch information
1 parent
703889a
commit e9c96f7
Showing
5 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
defmodule Kino.FS do | ||
@moduledoc """ | ||
Provides access to notebook files. | ||
""" | ||
|
||
defmodule ForbiddenError do | ||
@moduledoc """ | ||
Exception raised when access to a notebook file is forbidden. | ||
""" | ||
|
||
defexception [:name] | ||
|
||
@impl true | ||
def message(exception) do | ||
"forbidden access to file #{inspect(exception.name)}" | ||
end | ||
end | ||
|
||
@doc """ | ||
Accesses notebook file with the given name and returns a local path | ||
to read its contents from. | ||
This invocation may take a while, in case the file is downloaded | ||
from a URL and is not in the cache. | ||
> #### File operations {: .info} | ||
> | ||
> You should treat the file as read-only. To avoid unnecessary | ||
> copies the path may potentially be pointing to the original file, | ||
> in which case any write operations would be persisted. This | ||
> behaviour is not always the case, so you should not rely on it | ||
> either. | ||
""" | ||
@spec file_path(String.t()) :: String.t() | ||
def file_path(name) when is_binary(name) do | ||
case Kino.Bridge.get_file_entry_path(name) do | ||
{:ok, path} -> | ||
path | ||
|
||
{:error, :forbidden} -> | ||
raise ForbiddenError, name: name | ||
|
||
{:error, message} when is_binary(message) -> | ||
raise message | ||
|
||
{:error, reason} when is_atom(reason) -> | ||
raise "failed to access file path, reason: #{inspect(reason)}" | ||
end | ||
end | ||
|
||
@doc """ | ||
Accesses notebook file with the given name and returns a specification | ||
of the file location. | ||
This does not copy any files and moves the responsibility of reading | ||
the file to the caller. If you need to read a file directly, use | ||
`file_path/1`. | ||
""" | ||
@spec file_spec(String.t()) :: FSS.entry() | ||
def file_spec(name) do | ||
case Kino.Bridge.get_file_entry_spec(name) do | ||
{:ok, spec} -> | ||
file_spec_to_fss(spec) | ||
|
||
{:error, :forbidden} -> | ||
raise ForbiddenError, name: name | ||
|
||
{:error, message} when is_binary(message) -> | ||
raise message | ||
|
||
{:error, reason} when is_atom(reason) -> | ||
raise "failed to access file spec, reason: #{inspect(reason)}" | ||
end | ||
end | ||
|
||
defp file_spec_to_fss(%{type: :local} = file_spec) do | ||
FSS.Local.from_path(file_spec.path) | ||
end | ||
|
||
defp file_spec_to_fss(%{type: :url} = file_spec) do | ||
case FSS.HTTP.parse(file_spec.url) do | ||
{:ok, entry} -> entry | ||
{:error, error} -> raise error | ||
end | ||
end | ||
|
||
defp file_spec_to_fss(%{type: :s3} = file_spec) do | ||
case FSS.S3.parse("s3:///" <> file_spec.key, | ||
config: [ | ||
region: file_spec.region, | ||
endpoint: file_spec.bucket_url, | ||
access_key_id: file_spec.access_key_id, | ||
secret_access_key: file_spec.secret_access_key | ||
] | ||
) do | ||
{:ok, entry} -> entry | ||
{:error, error} -> raise error | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
defmodule Kino.FSTest do | ||
use ExUnit.Case, async: true | ||
|
||
describe "file_spec/1" do | ||
test "returns a file spec" do | ||
name = "file.txt" | ||
path = "/home/bob/file.txt" | ||
spec = %{type: :local, path: path} | ||
|
||
configure_gl_with_reply({:livebook_get_file_entry_spec, name}, {:ok, spec}) | ||
|
||
assert %FSS.Local.Entry{path: ^path} = Kino.FS.file_spec(name) | ||
end | ||
|
||
test "returns an HTTP FSS entry" do | ||
name = "remote-file.txt" | ||
url = "https://example.com/remote-file.txt" | ||
spec = %{type: :url, url: url} | ||
|
||
configure_gl_with_reply({:livebook_get_file_entry_spec, name}, {:ok, spec}) | ||
|
||
assert %FSS.HTTP.Entry{url: ^url, config: %FSS.HTTP.Config{headers: []}} = | ||
Kino.FS.file_spec(name) | ||
end | ||
|
||
test "returns a S3 FSS entry" do | ||
name = "file-from-s3.txt" | ||
bucket_url = "https://s3.us-west-1.amazonaws.com/my-bucket" | ||
|
||
spec = %{ | ||
type: :s3, | ||
bucket_url: bucket_url, | ||
region: "us-west-1", | ||
access_key_id: "access-key-1", | ||
secret_access_key: "secret-access-key-1", | ||
key: "file-from-s3.txt" | ||
} | ||
|
||
configure_gl_with_reply({:livebook_get_file_entry_spec, name}, {:ok, spec}) | ||
|
||
assert %FSS.S3.Entry{} = s3 = Kino.FS.file_spec(name) | ||
|
||
assert s3.key == spec.key | ||
|
||
assert s3.config.region == spec.region | ||
assert s3.config.endpoint == bucket_url | ||
assert s3.config.access_key_id == spec.access_key_id | ||
assert s3.config.secret_access_key == spec.secret_access_key | ||
assert s3.config.bucket == nil | ||
end | ||
|
||
test "raises an error in case s3 file_spec has something nil" do | ||
name = "file-from-s3.txt" | ||
|
||
spec = %{ | ||
type: :s3, | ||
bucket_url: nil, | ||
region: "us-west-1", | ||
access_key_id: "access-key-1", | ||
secret_access_key: "secret-access-key-1", | ||
key: name | ||
} | ||
|
||
configure_gl_with_reply({:livebook_get_file_entry_spec, name}, {:ok, spec}) | ||
|
||
assert_raise ArgumentError, "endpoint is required when bucket is nil", fn -> | ||
Kino.FS.file_spec(name) | ||
end | ||
|
||
bucket_url = "https://s3.us-west-1.amazonaws.com/my-bucket" | ||
|
||
spec = | ||
spec | ||
|> Map.replace!(:bucket_url, bucket_url) | ||
|> Map.replace!(:access_key_id, nil) | ||
|
||
configure_gl_with_reply({:livebook_get_file_entry_spec, name}, {:ok, spec}) | ||
|
||
assert_raise ArgumentError, | ||
"missing :access_key_id for FSS.S3 (set the key or the AWS_ACCESS_KEY_ID env var)", | ||
fn -> | ||
Kino.FS.file_spec(name) | ||
end | ||
end | ||
end | ||
|
||
defp configure_gl_with_reply(request, reply) do | ||
gl = | ||
spawn(fn -> | ||
assert_receive {:io_request, from, reply_as, ^request} | ||
send(from, {:io_reply, reply_as, reply}) | ||
end) | ||
|
||
Process.group_leader(self(), gl) | ||
end | ||
end |