Skip to content

Instantly share code, notes, and snippets.

@archatas
Created September 11, 2024 22:15
Show Gist options
  • Save archatas/65981d9e96e6c84d050adabd9aeedff1 to your computer and use it in GitHub Desktop.
Save archatas/65981d9e96e6c84d050adabd9aeedff1 to your computer and use it in GitHub Desktop.
A utility function that compares two files in the default Django storage (compatible with boto3)
def storage_file_compare(file1_path, file2_path, chunk_size=8192):
"""
Compare two files stored in Django's default_storage
:param file1_path: Path to the first file in the storage system
:param file2_path: Path to the second file in the storage system
:param chunk_size: Size of chunks to read at a time (default is 8192 bytes)
:return: True if files are identical, False otherwise
"""
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()
# Calculate hashes for both files
hash1 = calculate_hash(file1_path)
hash2 = calculate_hash(file2_path)
# Compare the hashes
return hash1 == hash2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment