From 5c7ec0622427a3ddd3dd2e8778332a3299537040 Mon Sep 17 00:00:00 2001 From: Roman Hargrave Date: Fri, 23 Dec 2022 20:06:18 -0800 Subject: [PATCH] Simplify S3 object name generation The previous design used an extremely long prefix including a forward slash that could have made managing uploaded objects extremely difficult for operators. --- mod_s3_upload/src/mod_s3_upload.erl | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/mod_s3_upload/src/mod_s3_upload.erl b/mod_s3_upload/src/mod_s3_upload.erl index 7af9a80..168dce7 100644 --- a/mod_s3_upload/src/mod_s3_upload.erl +++ b/mod_s3_upload/src/mod_s3_upload.erl @@ -447,16 +447,8 @@ object_url(BucketURL, FileName) -> FileName :: binary() ) -> ObjectName :: binary(). -% generate a unique-in-time object name. the name consists of a hash -% derived from the file, node, time, and a random number, a -% forward-slash, and the original filename. this ensures that it does -% not collide with other objects, while the forward slash ensures that -% the client displays only the original file name. +% generate reasonably unique sortable (by time first) object name. object_name(FileName) -> - MD = crypto:hash_init(sha256), - MDFilename = crypto:hash_update(MD, FileName), - MDNodeName = crypto:hash_update(MDFilename, atom_to_list(node())), - MDTime = crypto:hash_update(MDNodeName, <<(os:system_time())>>), - MDRand = crypto:hash_update(MDTime, crypto:strong_rand_bytes(256)), - Hash = crypto:hash_final(MDRand), - <<(str:to_hexlist(Hash))/binary, "/", FileName/binary>>. + str:format("~.36B~.36B-~s", [os:system_time(microsecond), + erlang:phash2(node()), + FileName]).