workaround for inconsistent PutObjectCopy on R2

This commit is contained in:
byte[] 2022-11-05 09:23:56 -04:00
parent 419ebdfe83
commit 464cc26a85

View file

@ -3,19 +3,25 @@ defmodule Philomena.Objects do
Replication wrapper for object storage backends.
"""
alias Philomena.Mime
require Logger
#
# Fetch a key from the primary storage backend and
# Fetch a key from the storage backend and
# write it into the destination file.
#
# sobelow_skip ["Traversal.FileModule"]
@spec download_file(String.t(), String.t()) :: any()
def download_file(key, file_path) do
[opts] = primary_opts()
contents =
backends()
|> Enum.find_value(fn opts ->
ExAws.S3.get_object(opts[:bucket], key)
|> ExAws.request!(opts[:config_overrides])
|> ExAws.request(opts[:config_overrides])
|> case do
{:ok, result} -> result
_ -> nil
end
end)
File.write!(file_path, contents.body)
end
@ -58,10 +64,16 @@ defmodule Philomena.Objects do
#
@spec copy(String.t(), String.t()) :: any()
def copy(source_key, dest_key) do
run_all(fn opts ->
ExAws.S3.put_object_copy(opts[:bucket], dest_key, opts[:bucket], source_key)
|> ExAws.request!(opts[:config_overrides])
end)
# Potential workaround for inconsistent PutObjectCopy on R2
#
# run_all(fn opts->
# ExAws.S3.put_object_copy(opts[:bucket], dest_key, opts[:bucket], source_key)
# |> ExAws.request!(opts[:config_overrides])
# end)
file_path = Briefly.create!()
download_file(source_key, file_path)
upload(dest_key, file_path)
end
#
@ -101,7 +113,7 @@ defmodule Philomena.Objects do
|> Enum.any?(fn {_, v} -> v == :error end)
|> case do
true ->
raise "Failed to operate on all backends"
Logger.warn("Failed to operate on all backends")
_ ->
:ok