Skip to content

Commit

Permalink
Merge pull request #321 from mjohnson9/fix/mod_s3_upload-host-no-match
Browse files Browse the repository at this point in the history
fix mod_s3_upload
  • Loading branch information
badlop authored Aug 2, 2023
2 parents c8e7591 + 88229e9 commit 28489e4
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions mod_s3_upload/src/mod_s3_upload.erl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@

-import(gen_mod, [get_opt/2]).

-import(crypto, [strong_rand_bytes/1]).

%%-----------------------------------------------------------------------
%% gen_mod callbacks and related machinery
%%-----------------------------------------------------------------------
Expand Down Expand Up @@ -410,7 +412,7 @@ put_get_url(#params{bucket_url = BucketURL,
UploadRequest,
Filename) ->
ObjectName = object_name(Filename),
UnsignedPutURL = decorated_put_url(UploadRequest, Params, ObjectName),
UnsignedPutURL = decorated_put_url(UploadRequest, Params, BucketURL, ObjectName),
{aws_util:signed_url(Auth, put, ?AWS_SERVICE_S3, UnsignedPutURL, [], calendar:universal_time(), TTL),
object_url(DownloadURL, ObjectName)}.

Expand Down Expand Up @@ -440,12 +442,13 @@ upload_parameters(#upload_request_0{size = FileSize,
-spec decorated_put_url(
UploadRequest :: #upload_request_0{},
Params :: #params{},
URL :: binary()
BucketURL :: binary(),
ObjectName :: binary()
) ->
PutURL :: binary().
% attach additional query parameters (to the PUT URL), specifically canned ACL.
decorated_put_url(UploadRequest, ServiceParams, URL) ->
UriMap = uri_string:parse(URL),
decorated_put_url(UploadRequest, ServiceParams, BucketURL, ObjectName) ->
UriMap = uri_string:parse(uri_string:resolve(ObjectName, BucketURL)),
QueryList = case UriMap of
#{query := QueryString} ->
uri_string:dissect_query(QueryString);
Expand All @@ -463,15 +466,17 @@ decorated_put_url(UploadRequest, ServiceParams, URL) ->
ObjectURL :: binary().
% generate a unique random object URL for the given filename
object_url(BucketURL, ObjectName) ->
#{path := BasePath} = UriMap = uri_string:parse(BucketURL),
uri_string:recompose(UriMap#{path => <<BasePath/binary, "/", ObjectName/binary>>}).
uri_string:resolve(ObjectName, BucketURL).

-spec object_name(
Filename :: binary()
) ->
ObjectName :: binary().
% generate reasonably unique sortable (by time first) object name.
object_name(Filename) ->
str:format("~.36B~.36B-~s", [os:system_time(microsecond),
erlang:phash2(node()),
Filename]).
<<RandomPrefix:128/big-unsigned-integer>> = crypto:strong_rand_bytes(16),
RandomPrefixS = str:format("~32.16.0b", [RandomPrefix]),
FolderName = str:substr(RandomPrefixS, 1, 2),
str:format("~s/~s-~s", [FolderName,
RandomPrefixS,
uri_string:quote(Filename)]).

0 comments on commit 28489e4

Please sign in to comment.