Designate a root folder for HashFS. If the folder doesn’t already exist, it will be created.

# Set the `depth` to the number of subfolders the file's hash should be split when saving.# Set the `width` to the desired width of each subfolder.fs=HashFS('temp_hashfs',depth=4,width=1,algorithm='sha256')# With depth=4 and width=1, files will be saved in the following pattern:# temp_hashfs/a/b/c/d/efghijklmnopqrstuvwxyz# With depth=3 and width=2, files will be saved in the following pattern:# temp_hashfs/ab/cd/ef/ghijklmnopqrstuvwxyz

NOTE: The algorithm value should be a valid string argument to hashlib.new().

Add content to the folder using either readable objects (e.g. StringIO) or file paths (e.g. 'a/path/to/some/file').

fromioimportStringIOsome_content=StringIO('some content')address=fs.put(some_content)# Or if you'd like to save the file with an extension...address=fs.put(some_content,'.txt')# The id of the file (i.e. the hexdigest of its contents).address.id# The absolute path where the file was saved.address.abspath# The path relative to fs.root.address.relpath# Whether the file previously existed.address.is_duplicate

The HashFS files may not always be in sync with it’s depth, width, or algorithm settings (e.g. if HashFS takes ownership of a directory that wasn’t previously stored using content hashes or if the HashFS settings change). These files can be easily reindexed using repair().

repaired=fs.repair()# Or if you want to drop file extensions...repaired=fs.repair(extensions=False)

WARNING: It’s recommended that a backup of the directory be made before repairing just in case something goes wrong.