Skip to content
Open
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
18 changes: 17 additions & 1 deletion lib/ex_aws/s3.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand All @@ -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`.
Expand Down
13 changes: 9 additions & 4 deletions lib/ex_aws/s3/download.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ 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}
end

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

Expand All @@ -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)
Expand All @@ -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
Expand Down
39 changes: 35 additions & 4 deletions test/lib/s3/download_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down