为用户上传的图像创建唯一的文件名(哈希)

发布于 2025-01-04 19:16:07 字数 122 浏览 1 评论 0原文

我有一个允许用户上传图像的网络应用程序。我将图像存储在 S3 存储桶中,并为它们提供所有文件名,这些文件名本质上是 MD5(session_id + unix_timestamp)的结果。这种创建唯一图像文件名的方法有什么陷阱吗?

I have a web application that allows users to upload images. I store the images in an S3 bucket and give them all filenames that are essentially the result of MD5(session_id + unix_timestamp). Are there any pitfalls of this method for creating unique image filenames?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

内心旳酸楚 2025-01-11 19:16:07

我会考虑使用某种文件夹格式存储文件,因为如果一个文件夹中没有 200,000 个文件,许多基于 UI 的 S3 客户端会工作得更好。此外,向上传的文件添加正确的扩展名和 MIME 类型也不会造成什么影响。这样 - 如果您决定直接从 S3 向网页等提供服务 - 他们就可以开始使用了。

2012/2/6gtbb88uytgfrses4.png

您也可以将尺寸放在名称中...

2012/2/6gtbb88uytgfrses4_600x800_.png

I would consider storing the files with some sort of folder format, as many UI based S3 clients will work much better if there is not 200,000 files in one folder. Also it would not hurt to also add the correct extension and mime type to the uploaded files. That way - if you decide to serve them directly from S3 to a web page, etc - they will be ready to go.

2012/2/6gtbb88uytgfrses4.png

Also you could put the dimensions in the name...

2012/2/6gtbb88uytgfrses4_600x800_.png

岛歌少女 2025-01-11 19:16:07

从独特性的角度来看,你应该是安全的。使用 MD5 时不太可能(尽管可能)发生冲突。

但是,如果您想向用户提供此文件的下载,我假设您将原始文件名存储在其他地方(数据库或类似的)。是这样吗?如果这是正确的,您不妨将会话 ID 和时间戳存储在同一位置,并直接使用 GUID 作为文件名(而不是将它们提供给 MD5 哈希函数),这样会更安全,避免发生冲突立场。

From a uniqueness standpoint, you should be safe. Collisions are not likely (although possible) to occur when using MD5.

But, if you want to provide users with the download of this files, I assume you are storing the original file names elsewhere (database or similar). Is that right? If that is right, you might as well store the session ID and timestamp on the same place, and just go ahead with a GUID for the file name (instead of providing them to your MD5 hash function), which would be safer from a collision standpoint.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文