Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multipart copy requires upload id, part number, and source range #217

Merged
merged 1 commit into from
Sep 25, 2023
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
26 changes: 14 additions & 12 deletions lib/ex_aws/s3.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1126,8 +1126,7 @@ defmodule ExAws.S3 do
end

@type upload_part_copy_opts :: [
{:copy_source_range, Range.t()}
| {:copy_source_if_modified_since, binary}
{:copy_source_if_modified_since, binary}
| {:copy_source_if_unmodified_since, binary}
| {:copy_source_if_match, binary}
| {:copy_source_if_none_match, binary}
Expand All @@ -1140,21 +1139,27 @@ defmodule ExAws.S3 do
dest_bucket :: binary,
dest_object :: binary,
src_bucket :: binary,
src_object :: binary
src_object :: binary,
upload_id :: binary,
part_number :: pos_integer,
source_range :: Range.t()
) :: ExAws.Operation.S3.t()
@spec upload_part_copy(
dest_bucket :: binary,
dest_object :: binary,
src_bucket :: binary,
src_object :: binary,
upload_id :: binary,
part_number :: pos_integer,
source_range :: Range.t(),
opts :: upload_part_copy_opts
) :: ExAws.Operation.S3.t()
@amz_headers ~w(
copy_source_if_modified_since
copy_source_if_unmodified_since
copy_source_if_match
copy_source_if_none_match)a
def upload_part_copy(dest_bucket, dest_object, src_bucket, src_object, opts \\ []) do
def upload_part_copy(dest_bucket, dest_object, src_bucket, src_object, upload_id, part_number, source_range, opts \\ []) do
opts = opts |> Map.new()

source_encryption =
Expand All @@ -1177,17 +1182,14 @@ defmodule ExAws.S3 do
|> Map.merge(source_encryption)
|> Map.merge(destination_encryption)

first..last = source_range
headers =
case opts do
%{copy_source_range: first..last} ->
Map.put(headers, "x-amz-copy-source-range", "bytes=#{first}-#{last}")

_ ->
headers
end
headers
|> Map.put("x-amz-copy-source-range", "bytes=#{first}-#{last}")
|> Map.put("x-amz-copy-source", "/#{src_bucket}/#{src_object}")

request(:put, dest_bucket, dest_object, [headers: headers], %{
params = %{"uploadId" => upload_id, "partNumber" => part_number}
request(:put, dest_bucket, dest_object, [headers: headers, params: params], %{
parser: &Parsers.parse_upload_part_copy/1
})
end
Expand Down
37 changes: 22 additions & 15 deletions lib/ex_aws/s3/parsers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,6 @@ if Code.ensure_loaded?(SweetXml) do
defmodule ExAws.S3.Parsers do
import SweetXml, only: [sigil_x: 2]

def parse_upload({:ok, resp = %{body: xml}}) do
parsed_body =
xml
|> SweetXml.xpath(~x"//CompleteMultipartUploadResult",
location: ~x"./Location/text()"s,
bucket: ~x"./Bucket/text()"s,
key: ~x"./Key/text()"s,
eTag: ~x"./ETag/text()"s
)

{:ok, %{resp | body: parsed_body}}
end

def parse_list_objects({:ok, resp = %{body: xml}}) do
parsed_body =
xml
Expand Down Expand Up @@ -86,8 +73,28 @@ if Code.ensure_loaded?(SweetXml) do

def parse_initiate_multipart_upload(val), do: val

def parse_upload_part_copy(val), do: val
def parse_complete_multipart_upload(val), do: val
def parse_upload_part_copy({:ok, resp = %{body: xml}}) do
parsed_body = xml
|> SweetXml.xpath(~x"//CopyPartResult",
last_modified: ~x"./LastModified/text()"s,
etag: ~x"./ETag/text()"s
)

{:ok, %{resp | body: parsed_body}}
end

def parse_complete_multipart_upload({:ok, resp = %{body: xml}}) do
parsed_body = xml
|> SweetXml.xpath(~x"//CompleteMultipartUploadResult",
location: ~x"./Location/text()"s,
bucket: ~x"./Bucket/text()"s,
key: ~x"./Key/text()"s,
etag: ~x"./ETag/text()"s
)

{:ok, %{resp | body: parsed_body}}
end


def parse_list_multipart_uploads({:ok, %{body: xml} = resp}) do
parsed_body =
Expand Down
57 changes: 36 additions & 21 deletions test/lib/s3/parser_test.exs
Original file line number Diff line number Diff line change
@@ -1,27 +1,6 @@
defmodule ExAws.S3.ParserTest do
use ExUnit.Case, async: true

test "#parse_upload parses CompleteMultipartUploadResult" do
upload_response = """
<CompleteMultipartUploadResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/\">
<Location>https://google.com</Location>
<Bucket>name_of_my_bucket</Bucket>
<Key>name_of_my_key.ext</Key>
<ETag>&quot;89asdfasdf0asdfasdfasd&quot;</ETag>
</CompleteMultipartUploadResult>
"""

result = ExAws.S3.Parsers.parse_upload({:ok, %{body: upload_response}})
{:ok, %{body: parsed_body}} = result

assert parsed_body == %{
location: "https://google.com",
bucket: "name_of_my_bucket",
key: "name_of_my_key.ext",
eTag: "\"89asdfasdf0asdfasdfasd\""
}
end

test "#parse_list_objects parses CommonPrefixes" do
list_objects_response = """
<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
Expand Down Expand Up @@ -325,4 +304,40 @@ defmodule ExAws.S3.ParserTest do
assert is_map(delete_marker1[:owner])
assert delete_marker1[:owner][:display_name] == "[email protected]"
end

test "#parse_upload_part_copy parses response" do
parse_upload_part_copy_response = """
<CopyPartResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<LastModified>2019-02-09T06:27:26.000Z</LastModified>
<ETag>&quot;7cbef1ad67ecd0d9ba35af98d3de5a94&quot;</ETag>
</CopyPartResult>
"""

result = ExAws.S3.Parsers.parse_upload_part_copy({:ok, %{body: parse_upload_part_copy_response}})
{:ok, %{body: %{last_modified: last_modified, etag: etag}}} = result

assert "2019-02-09T06:27:26.000Z" == last_modified
assert "\"7cbef1ad67ecd0d9ba35af98d3de5a94\"" == etag
end

test "#parse_complete_multipart_upload parses CompleteMultipartUploadResult" do
complete_multipart_upload_response = """
<CompleteMultipartUploadResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Location>https://s3-eu-west-1.amazonaws.com/my-bucket/tmp-copy3.mp4</Location>
<Bucket>my-bucket</Bucket>
<Key>tmp-copy3.mp4</Key>
<ETag>&quot;17fbc0a106abbb6f381aac6e331f2a19-1&quot;</ETag>
</CompleteMultipartUploadResult>
"""

result = ExAws.S3.Parsers.parse_complete_multipart_upload({:ok, %{body: complete_multipart_upload_response}})
{:ok, %{body: body}} = result

assert body == %{
location: "https://s3-eu-west-1.amazonaws.com/my-bucket/tmp-copy3.mp4",
bucket: "my-bucket",
key: "tmp-copy3.mp4",
etag: "\"17fbc0a106abbb6f381aac6e331f2a19-1\""
}
end
end
10 changes: 9 additions & 1 deletion test/lib/s3/upload_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,18 @@ defmodule ExAws.S3.UploadTest do

case conn do
%{method: "POST", request_path: ^request_path, query_params: %{"uploadId" => ^upload_id}} ->
body = """
<CompleteMultipartUploadResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Location>https://s3-eu-west-1.amazonaws.com/my-bucket/#{path}</Location>
<Bucket>#{bucket_name}</Bucket>
<Key>#{path}</Key>
<ETag>&quot;17fbc0a106abbb6f381aac6e331f2a19-1&quot;</ETag>
</CompleteMultipartUploadResult>
"""
send(test_pid, :completed_upload)

conn
|> Plug.Conn.send_resp(200, "")
|> Plug.Conn.send_resp(200, body)

%{method: "POST", request_path: ^request_path} ->
body = """
Expand Down
7 changes: 5 additions & 2 deletions test/lib/s3_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ defmodule ExAws.S3Test do
"x-amz-copy-source-range" => "bytes=1-9",
"x-amz-copy-source-server-side-encryption-customer-algorithm" => "md5"
},
params: %{"uploadId" => "upload-id", "partNumber" => 1},
path: "dest-object",
http_method: :put,
parser: &ExAws.S3.Parsers.parse_upload_part_copy/1
Expand All @@ -273,8 +274,10 @@ defmodule ExAws.S3Test do
"dest-object",
"src-bucket",
"src-object",
source_encryption: [customer_algorithm: "md5"],
copy_source_range: 1..9
"upload-id",
1,
1..9,
source_encryption: [customer_algorithm: "md5"]
)
end

Expand Down