Skip to content
Merged
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
34 changes: 23 additions & 11 deletions lib/remote_persistent_term/fetcher/s3.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ defmodule RemotePersistentTerm.Fetcher.S3 do

@impl true
def current_version(state) do
with {:ok, %{body: %{contents: contents}}} <- list_objects(state),
{:ok, %{e_tag: etag}} <- find_latest(contents, state.key) do
Logger.info("found latest version of s3://#{state.bucket}/#{state.key}: #{etag}")
with {:ok, versions} <- list_object_versions(state),
{:ok, %{etag: etag, version_id: version}} <- find_latest(versions) do
Logger.info(
"found latest version of s3://#{state.bucket}/#{state.key}: #{etag} with version: #{version}"
)

{:ok, etag}
else
{:error, {:unexpected_response, %{body: reason}}} ->
Expand Down Expand Up @@ -87,21 +90,26 @@ defmodule RemotePersistentTerm.Fetcher.S3 do
end
end

defp list_objects(state) do
state.bucket
|> ExAws.S3.list_objects()
|> client().request(region: state.region)
defp list_object_versions(state) do
res =
state.bucket
|> ExAws.S3.get_bucket_object_versions(prefix: state.key)
|> aws_client_request(state.region)

with {:ok, %{body: %{versions: versions}}} <- res do
{:ok, versions}
end
end

defp get_object(state) do
state.bucket
|> ExAws.S3.get_object(state.key)
|> client().request(region: state.region)
|> aws_client_request(state.region)
end

defp find_latest([_ | _] = contents, key) do
defp find_latest([_ | _] = contents) do
Enum.find(contents, fn
%{key: ^key} ->
%{is_latest: "true"} ->
true

_ ->
Expand All @@ -113,7 +121,11 @@ defmodule RemotePersistentTerm.Fetcher.S3 do
end
end

defp find_latest(_, _), do: {:error, :not_found}
defp find_latest(_), do: {:error, :not_found}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you matching on this :not_found somewhere? Otherwise I think we should either:

  • use a more descriptive string, something like "object @ #{key} was not found"
  • introduce an Exception that will produce a similar message to the string above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes we match on it already when we call find_latest:

      {:error, :not_found} ->
        {:error, "could not find s3://#{state.bucket}/#{state.key}"}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perfect


defp aws_client_request(op, region) do
client().request(op, region: region)
end

defp client, do: Application.get_env(:remote_persistent_term, :aws_client, ExAws)
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule RemotePersistentTerm.MixProject do
use Mix.Project

@name "RemotePersistentTerm"
@version "0.10.0"
@version "0.10.1"
@repo_url "https://github.com/AppMonet/remote_persistent_term"

def project do
Expand Down