You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在settings.py中配置DEFAULT_FILE_STORAGE触发TypeError问题求助

Fixing "Unicode-objects must be encoded before hashing" TypeError with Django DEFAULT_FILE_STORAGE

Let’s work through solving this error you’re hitting while setting up AWS S3 for your Photologue media files. This issue almost always stems from a mismatch between Unicode strings (Python 3’s default string type) and hashing functions that expect byte inputs—common when working with Django, boto3, or custom storage classes.

Step 1: Update Dependencies to Fix Version Compatibility

First, rule out outdated package issues. Older versions of django-storages or boto3 can have bugs handling Python 3’s Unicode strings. Upgrade to the latest stable releases:

pip install --upgrade django-storages boto3

Step 2: Audit Your Custom Storage Class

If you’re using MediaRootS3Boto3Storage, check for any custom logic that involves hashing strings. For example, if you’re generating unique filenames with hashlib, you need to encode the string to bytes first.

Problematic Code (causes the error):

import hashlib
from storages.backends.s3boto3 import S3Boto3Storage

class MediaRootS3Boto3Storage(S3Boto3Storage):
    location = 'media'
    
    def get_available_name(self, name, max_length=None):
        # Missing encoding for the string input
        hash_name = hashlib.md5(name).hexdigest()
        return f"{hash_name}{os.path.splitext(name)[1]}"

Fixed Version:

import hashlib
from storages.backends.s3boto3 import S3Boto3Storage

class MediaRootS3Boto3Storage(S3Boto3Storage):
    location = 'media'
    file_overwrite = False  # Optional: prevents overwriting existing files
    
    def get_available_name(self, name, max_length=None):
        # Encode the string to bytes before hashing
        hash_name = hashlib.md5(name.encode('utf-8')).hexdigest()
        return f"{hash_name}{os.path.splitext(name)[1]}"

If your custom class only sets the location with no extra logic, double-check it’s correctly inherited from S3Boto3Storage (no typos!).

Step 3: Clean Up Your settings.py Configuration

Never set DEFAULT_FILE_STORAGE to an empty string ''—this breaks Django’s storage backend resolution. Instead:

  • Comment it out to use the default FileSystemStorage (for testing), or
  • Set it to your valid custom storage class.

Here’s a polished AWS S3 setup for media files:

import os

# AWS S3 Core Settings
AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = 'your-bucket-name'
AWS_S3_REGION_NAME = 'us-east-1'  # Replace with your bucket's region
AWS_S3_CUSTOM_DOMAIN = f"{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com"
AWS_S3_FILE_OVERWRITE = False  # Optional: preserves original filenames

# Media File Configuration
DEFAULT_FILE_STORAGE = 'app.aws.utils.MediaRootS3Boto3Storage'
MEDIA_URL = f"https://{AWS_S3_CUSTOM_DOMAIN}/media/"

Step 4: Confirm Photologue Integration

Photologue uses Django’s default media handling out of the box—no extra configuration needed. As long as DEFAULT_FILE_STORAGE points to your S3 backend, Photologue will automatically save its media files to the /media/ directory in your bucket.

Why This Fixes the Error

The "Unicode-objects must be encoded before hashing" message means you’re passing a Unicode string to a function that expects bytes. By encoding strings to bytes (using .encode('utf-8')) before hashing, and ensuring your dependencies are up to date, you eliminate this type mismatch.

内容的提问来源于stack exchange,提问作者Nick Mancini

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:23:15