-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Upload a timestamped file - Update a stable copy to the latest file
- Loading branch information
Showing
9 changed files
with
172 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
defmodule Transport.S3.AggregatesUploader do | ||
@moduledoc """ | ||
Helpers to upload a file, computes its sha256, and update a "latest" file. | ||
""" | ||
|
||
@spec upload_aggregate!(Path.t(), String.t(), String.t()) :: :ok | ||
@doc """ | ||
Example | ||
with_tmp_file(fn file -> | ||
File.write(file, "some relevant data") | ||
upload_aggregate!(file, "aggregate-20250127193035.csv", "aggregate-latest.csv") | ||
end) | ||
""" | ||
def upload_aggregate!(file, remote_file, remote_latest_file) do | ||
with_tmp_file(fn checksum_file -> | ||
sha256!(file, checksum_file) | ||
|
||
upload_files!(file, checksum_file, remote_file) | ||
|> update_latest_files!(remote_latest_file) | ||
end) | ||
end | ||
|
||
@spec with_tmp_file((Path.t() -> any())) :: any() | ||
def with_tmp_file(cb) do | ||
file = mk_tmp_file() | ||
|
||
try do | ||
cb.(file) | ||
after | ||
:ok = File.rm(file) | ||
end | ||
end | ||
|
||
defp mk_tmp_file do | ||
path = System.tmp_dir!() |> Path.join(Ecto.UUID.generate()) | ||
|
||
File.touch!(path) | ||
|
||
path | ||
end | ||
|
||
defp sha256!(file, checksum_file) do | ||
hash_state = :crypto.hash_init(:sha256) | ||
|
||
hash = | ||
File.stream!(file, 2048) | ||
|> Enum.reduce(hash_state, fn chunk, prev_state -> | ||
:crypto.hash_update(prev_state, chunk) | ||
end) | ||
|> :crypto.hash_final() | ||
|> Base.encode16() | ||
|> String.downcase() | ||
|
||
File.write(checksum_file, hash) | ||
end | ||
|
||
defp upload_files!(file, checksum_file, remote_file) do | ||
remote_checksum_file = checksum_filename(remote_file) | ||
|
||
stream_upload!(file, remote_file) | ||
stream_upload!(checksum_file, remote_checksum_file) | ||
|
||
{remote_file, remote_checksum_file} | ||
end | ||
|
||
defp update_latest_files!({remote_file, remote_checksum_file}, remote_latest_file) do | ||
remote_latest_checksum_file = checksum_filename(remote_latest_file) | ||
|
||
copy!(remote_file, remote_latest_file) | ||
copy!(remote_checksum_file, remote_latest_checksum_file) | ||
|
||
:ok | ||
end | ||
|
||
defp checksum_filename(base_filename) do | ||
"#{base_filename}.sha256sum" | ||
end | ||
|
||
defp stream_upload!(file, filename) do | ||
Transport.S3.stream_to_s3!(:aggregates, file, filename) | ||
end | ||
|
||
defp copy!(s3_path, filename) do | ||
Transport.S3.remote_copy_file!(:aggregates, s3_path, filename) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
apps/transport/test/transport/S3/aggregates_uploader_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
defmodule Transport.S3.AggregatesUploaderTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias Transport.S3.AggregatesUploader | ||
alias Transport.Test.S3TestUtils | ||
|
||
test "export to S3" do | ||
snapshot = "aggregate-20250127193035.csv" | ||
latest = "aggregate-latest.csv" | ||
checksum = "#{snapshot}.sha256sum" | ||
latest_checksum = "#{latest}.sha256sum" | ||
|
||
bucket_name = Transport.S3.bucket_name(:aggregates) | ||
|
||
S3TestUtils.s3_mock_stream_file(path: snapshot, bucket: bucket_name, acl: :private) | ||
S3TestUtils.s3_mock_stream_file(path: checksum, bucket: bucket_name, acl: :private) | ||
S3TestUtils.s3_mocks_remote_copy_file(bucket_name, snapshot, latest) | ||
S3TestUtils.s3_mocks_remote_copy_file(bucket_name, checksum, latest_checksum) | ||
|
||
AggregatesUploader.with_tmp_file(fn file -> | ||
File.write(file, "some relevant data") | ||
|
||
:ok = AggregatesUploader.upload_aggregate!(file, snapshot, latest) | ||
end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters