About File Comparison in Default Storage

Here's a function to compare two files in default_storage whether their content is the same:

def storage_file_compare(file1_path, file2_path, chunk_size=8192):
    import hashlib
    from django.core.files.storage import default_storage

    def calculate_hash(file_path):
        sha256 = hashlib.sha256()
        with default_storage.open(file_path, "rb") as f:
            for chunk in iter(lambda: f.read(chunk_size), b""):
                sha256.update(chunk)
        return sha256.hexdigest()

    hash1 = calculate_hash(file1_path)
    hash2 = calculate_hash(file2_path)

    return hash1 == hash2

Then you can compare the files:

>>> storage_file_compare("gallery/cat.png", "gallery/cat-copy.png")
True

It works with boto3 and object storage, such as AWS S3 or DigitalOcean Spaces.

Tips and Tricks Programming Architecture Django 5.x Django 4.2 Django 3.2 django-storages Boto3