diff --git a/lib/ex_aws/s3.ex b/lib/ex_aws/s3.ex index 98154b6..9de4e3a 100644 --- a/lib/ex_aws/s3.ex +++ b/lib/ex_aws/s3.ex @@ -1320,7 +1320,8 @@ defmodule ExAws.S3 do @type download_file_opts :: [ max_concurrency: pos_integer, chunk_size: pos_integer, - timeout: pos_integer + timeout: pos_integer, + encryption: customer_encryption_opts ] @doc ~S""" @@ -1331,6 +1332,21 @@ defmodule ExAws.S3 do Defaults to a concurrency of 8, chunk size of 1MB, and a timeout of 1 minute. + ### Using SSE-C encryption + + To download an object using SSE-C encryption, pass the same `:encryption` + option accepted by `get_object/3` and `head_object/3`: + + encryption = [ + customer_algorithm: "AES256", + customer_key: key, + customer_key_md5: key_md5 + ] + + ExAws.S3.download_file("example-bucket", "path/to/file.txt", "file.txt", + encryption: encryption + ) + ### Streaming to memory In order to use `ExAws.stream!/2`, the third `dest` parameter must be set to `:memory`. diff --git a/lib/ex_aws/s3/download.ex b/lib/ex_aws/s3/download.ex index a84997b..8eace75 100644 --- a/lib/ex_aws/s3/download.ex +++ b/lib/ex_aws/s3/download.ex @@ -17,7 +17,10 @@ defmodule ExAws.S3.Download do def get_chunk(op, %{start_byte: start_byte, end_byte: end_byte}, config) do %{body: body} = op.bucket - |> ExAws.S3.get_object(op.path, range: "bytes=#{start_byte}-#{end_byte}") + |> ExAws.S3.get_object( + op.path, + [range: "bytes=#{start_byte}-#{end_byte}"] ++ request_opts(op) + ) |> ExAws.request!(config) {start_byte, body} @@ -25,7 +28,7 @@ defmodule ExAws.S3.Download do def build_chunk_stream(op, config) do op.bucket - |> get_file_size(op.path, config) + |> get_file_size(op.path, config, request_opts(op)) |> chunk_stream(op.opts[:chunk_size] || 1024 * 1024) end @@ -47,8 +50,8 @@ defmodule ExAws.S3.Download do end) end - defp get_file_size(bucket, path, config) do - %{headers: headers} = ExAws.S3.head_object(bucket, path) |> ExAws.request!(config) + defp get_file_size(bucket, path, config, opts) do + %{headers: headers} = ExAws.S3.head_object(bucket, path, opts) |> ExAws.request!(config) headers |> Enum.find(fn {k, _} -> String.downcase(k) == "content-length" end) @@ -61,6 +64,8 @@ defmodule ExAws.S3.Download do content_length |> List.first() |> String.to_integer() end end + + defp request_opts(op), do: Keyword.take(op.opts, [:encryption]) end defimpl ExAws.Operation, for: ExAws.S3.Download do diff --git a/test/lib/s3/download_test.exs b/test/lib/s3/download_test.exs index c9c05b5..e3cf69f 100644 --- a/test/lib/s3/download_test.exs +++ b/test/lib/s3/download_test.exs @@ -19,8 +19,39 @@ defmodule ExAws.S3.DownloadTest do |> S3.download_file("test.txt", "downloaded_file.txt") |> ExAws.request!(exaws_config_for_bypass(bypass)) - assert_received :fetched_file_size - assert_received :downloaded_chunk + assert_received {:fetched_file_size, _headers} + assert_received {:downloaded_chunk, _headers} + + assert_file("downloaded_file.txt", file_body) + end) + end + + test "downloading an SSE-C encrypted file", %{bypass: bypass} do + file_body = "hello world" + + encryption = [ + customer_algorithm: "AES256", + customer_key: "customer-key", + customer_key_md5: "customer-key-md5" + ] + + setup_multipart_download_backend(bypass, self(), "my-bucket", "test.txt", file_body) + + in_tmp(fn _ -> + :done = + "my-bucket" + |> S3.download_file("test.txt", "downloaded_file.txt", encryption: encryption) + |> ExAws.request!(exaws_config_for_bypass(bypass)) + + assert_received {:fetched_file_size, head_headers} + assert_received {:downloaded_chunk, get_headers} + + for headers <- [head_headers, get_headers] do + assert {"x-amz-server-side-encryption-customer-algorithm", "AES256"} in headers + assert {"x-amz-server-side-encryption-customer-key", "customer-key"} in headers + + assert {"x-amz-server-side-encryption-customer-key-md5", "customer-key-md5"} in headers + end assert_file("downloaded_file.txt", file_body) end) @@ -33,14 +64,14 @@ defmodule ExAws.S3.DownloadTest do Bypass.expect(bypass, fn conn -> case conn do %{method: "HEAD", request_path: ^request_path} -> - send(test_pid, :fetched_file_size) + send(test_pid, {:fetched_file_size, conn.req_headers}) conn |> Plug.Conn.put_resp_header("Content-Length", file_body |> byte_size |> to_string) |> Plug.Conn.send_resp(200, "") %{method: "GET", request_path: ^request_path} -> - send(test_pid, :downloaded_chunk) + send(test_pid, {:downloaded_chunk, conn.req_headers}) conn |> Plug.Conn.send_resp(200, file_body)