Skip to content

Commit

Permalink
implement size/1 for s3 backend
Browse files Browse the repository at this point in the history
  • Loading branch information
ferd committed Apr 3, 2024
1 parent 2dbf5a9 commit 01e35e6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
9 changes: 7 additions & 2 deletions apps/revault/src/revault_s3.erl
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,13 @@ find_hashes(Dir, Pred) ->
NewList.

-spec size(file:filename()) -> {ok, non_neg_integer()} | {error, term()}.
size(_Path) ->
error(not_implemented).
size(Path) ->
Res = aws_s3:head_object(client(), bucket(), Path, #{}),
maybe
ok ?= handle_result(Res),
{ok, #{<<"ContentLength">> := LenBin}, _} = Res,
{ok, binary_to_integer(LenBin)}
end.

-spec multipart_init(Path, PartsTotal, Hash) -> State when
Path :: file:filename(),
Expand Down
13 changes: 12 additions & 1 deletion apps/revault/test/revault_s3_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
all() ->
[client_cache, consult, hash_cache, hash,
copy, list_uncached, list_cached, is_regular,
write_file, delete].
write_file, delete, size].

init_per_suite(Config) ->
InitBucket = application:get_env(revault, bucket, undefined),
Expand Down Expand Up @@ -335,6 +335,17 @@ delete(_Config) ->
?assertEqual(ok, revault_s3:delete(<<"a">>)),
ok.

size() ->
[{doc, "mocked out call to the size function, based "
"on data from the integration suite"}].
size(_Config) ->
meck:expect(aws_s3, head_object,
fun(_Cli, _Bucket, <<"a">>, _) ->
{ok, #{<<"ContentLength">> => <<"1234">>},
{200, [], make_ref()}}
end),
?assertEqual({ok, 1234}, revault_s3:size(<<"a">>)),
ok.

%% rename is just a copy+delete, ignore it.

Expand Down
7 changes: 4 additions & 3 deletions apps/revault/test/s3_integration_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ crud_object(Config) ->
%% Read
?assertMatch({ok, #{<<"Body">> := NewBody}, _Http},
aws_s3:get_object(Client, Bucket, Key)),
%% Get Hash
?assertMatch({ok, #{<<"ChecksumSHA256">> := NewChk}, _Http},
%% Get Hash and size
?assertMatch({ok, #{<<"ChecksumSHA256">> := NewChk,
<<"ContentLength">> := <<"2">>}, _Http},
aws_s3:head_object(Client, Bucket, Key,
#{<<"ChecksumMode">> => <<"ENABLED">>})),
%% Delete
Expand Down Expand Up @@ -143,7 +144,7 @@ rename_raw(Config) ->
#{<<"CopySource">> => filename:join(Bucket, KeySrc)})),
?assertMatch({ok, #{}, _Http},
aws_s3:delete_object(Client, Bucket, KeySrc, #{})),
%% Read Sode
%% Read Mode
?assertMatch({ok, #{<<"Body">> := Body}, _Http},
aws_s3:get_object(Client, Bucket, KeyDest)),
?assertMatch({ok, #{<<"ChecksumSHA256">> := Chk}, _Http},
Expand Down

0 comments on commit 01e35e6

Please sign in to comment.