I'm making a script that needs to move all files from a third folder to the main one and in that list there are also those that are already in the right folder and I need to ignore them.
LIKE THIS: my_bucket/files/id/file.txt TO my_bucket/files/file.txt
my code is like this and it is only listing the objects, it is not moving
I try this code:
defmodule Script.Elixir do
def list_objects_in_bucket do
"my_bucket"
|> ExAws.S3.list_objects(max_keys: 1000)
|> ExAws.request()
|> extract_only_route()
end
def copy_object(origin_path) do
ExAws.S3.put_object_copy(
"my_bucket/",
"/my_folder_destiny/",
"my_bucket",
origin_path
)
|> ExAws.request()
end
defp extract_only_route({:ok, %{body: %{contents: contents}}}) do
Enum.map(contents, fn %{key: route} -> route end)
end
end
Once you have a list of files with list_objects_in_bucket/0
, and a tested copy_object/2
function, accepting arguments from
and to
, you might do somewhat like
@bucket "my_bucket/"
@top "files/"
@nested "id/"
# https://hexdocs.pm/ex_aws_s3/ExAws.S3.html#put_object_copy/5
copy_object = fn from, to ->
@bucket
|> ExAws.S3.put_object_copy(to, @bucket, from)
|> ExAws.request()
end
for @bucket <> @top <> @nested <> name <- list_objects_in_bucket() do
copy_object.(@top <> @nested <> name, @top <> name)
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.