Thousands of images, how should I organize the directory structure? (linux)

前端 未结 6 1092
一向
一向 2020-12-07 23:31

I am getting thousands of pictures uploaded by thousands of users on my Linux server, which is hosted by 1and1.com (I believe they use CentOS, but am unsure of the version).

6条回答
  •  生来不讨喜
    2020-12-08 00:16

    Might be late to the game on this. But one solution (if it fits your use-case) could be file name hashing. It is a way to create an easily reproducible file path using the name of the file while also creating a well distributed directory structure. For example, you can use the bytes of the filename's hashcode as it's path:

    String fileName = "cat.gif";
    int hash = fileName.hashCode();
    int mask = 255;
    int firstDir = hash & mask;
    int secondDir = (hash >> 8) & mask;
    

    This would result in the path being:

    /172/029/cat.gif
    

    You can then find cat.gif in the directory structure by reproducing the algorithm.

    Using HEX as the directory names would be as easy as converting the int values:

    String path = new StringBuilder(File.separator)
            .append(String.format("%02x", firstDir))
            .append(File.separator)
            .append(String.format("%02x", secondDir)
            .toString();
    

    Resulting in:

    /AC/1D/cat.gif
    

    I wrote an article about this a few years ago and recently moved it to Medium. It has a few more details and some sample code: File Name Hashing: Creating a Hashed Directory Structure. Hope this helps!

提交回复
热议问题