Last active
March 14, 2019 22:47
-
-
Save hok/4a4991b031cd0f9ef873ab3d3800952b to your computer and use it in GitHub Desktop.
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
defmodule Feed.S3.Request do | |
alias ExAws.S3 | |
@bucket "<BUCKET NAME>" | |
@post_regex ~r/posts\/.*.md/ | |
@tip_regex ~r/tips\/.*.md/ | |
def get_posts do | |
@post_regex | |
|> list_posts() | |
|> parse_list() | |
end | |
def get_tips do | |
@tip_regex | |
|> list_tips() | |
|> parse_list() | |
end | |
defp list_items(regex) do | |
@bucket | |
|> S3.list_objects() | |
|> ExAws.request!() | |
|> Map.get(:body) | |
|> Map.get(:contents) | |
|> Enum.filter(fn item -> | |
String.match?(item.key, regex) | |
end) | |
end | |
defp parse_list(files) do | |
files | |
|> Enum.map(fn file -> | |
body = | |
@bucket | |
|> S3.get_object(file.key) | |
|> ExAws.request!() | |
|> Map.get(:body) | |
%{ | |
body: body, | |
filename: file.key | |
} | |
end) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment