Fix root directory listing on S3 when empty (#646)

This commit is contained in:
Jonatan Kłosko 2021-10-26 15:39:24 +02:00 committed by GitHub
parent 46d49743b9
commit 9da44fd541
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View file

@ -70,7 +70,7 @@ defimpl Livebook.FileSystem, for: Livebook.FileSystem.S3 do
delimiter = if recursive, do: nil, else: "/"
with {:ok, %{keys: keys}} <- list_objects(file_system, prefix: dir_key, delimiter: delimiter) do
if keys == [] do
if keys == [] and dir_key != "" do
FileSystem.Utils.posix_error(:enoent)
else
paths = keys |> List.delete(dir_key) |> Enum.map(&("/" <> &1))

View file

@ -99,6 +99,25 @@ defmodule Livebook.FileSystem.S3Test do
assert {:error, "no such file or directory"} = FileSystem.list(file_system, dir_path, false)
end
test "does not return an error when the root directory is empty", %{bypass: bypass} do
Bypass.expect_once(bypass, "GET", "/mybucket", fn conn ->
assert %{"delimiter" => "/"} = conn.params
conn
|> Plug.Conn.put_resp_content_type("application/xml")
|> Plug.Conn.resp(200, """
<ListBucketResult>
<Name>mybucket</Name>
</ListBucketResult>
""")
end)
file_system = S3.new(bucket_url(bypass.port), "key", "secret")
dir_path = "/"
assert {:ok, []} = FileSystem.list(file_system, dir_path, false)
end
test "returns a list of absolute child object paths", %{bypass: bypass} do
Bypass.expect_once(bypass, "GET", "/mybucket", fn conn ->
assert %{"delimiter" => "/"} = conn.params