Home > Net >  Transform S3 objects into hierarchical (tree) Map in Elixir
Transform S3 objects into hierarchical (tree) Map in Elixir

Time:09-12

I have this task where I need to extract objects from AWS S3 using ExAws and return it to Frontend in a file-tree-like structure.

From the data returned by ExAws.S3.list_objects("bucket-name", prefix: "folder-path") |> ExAws.request!

%{
  body: %{
    contents: [
      %{key: "file1.svg"},
      %{key: "FolderA/file1.txt"},
      %{key: "FolderA/file2.txt"},
      %{key: "FolderB/Layer1/file1.csv"},
      %{key: "FolderB/Layer1/file2.csv"},
      %{key: "FolderB/Layer1/GroupA/file1.pdf"},
      %{key: "FolderB/Layer1/GroupB/file1.jpg"},
      %{key: "FolderB/Layer2/file1.png"}
    ]
  }
}

I need to transform it into something like this map.

%{
  "FolderA" => %{
    "file1.txt" => "FolderA/file1.txt",
    "file2.txt" => "FolderA/file2.txt"
  },
  "FolderB" => %{
    "Layer1" => %{
      "GroupA" => %{
        "file1.pdf" => "FolderB/Layer1/GroupA/file1.pdf"
      },
      "GroupB" => %{
        "file1.jpg" => "FolderB/Layer1/GroupB/file1.jpg"
      },
      "file1.csv" => "FolderB/Layer1/file1.csv",
      "file2.csv" => "FolderB/Layer1/file2.csv"
    },
    "Layer2" => %{"file1.png" => "FolderB/Layer2/file1.png"}
  },
  "file1.svg" => "file1.svg"
}

I found this npm package, s3-tree but it does a request per 'folder'. What I need is just pure algorithm.

And these with python: Python - How can I convert S3 folders into JSON hierarchy?, s3-tree. Translating these into elixir would help.

CodePudding user response:

Nice recursion challenge

  • Related