jschneier / django-storages

https://django-storages.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
2.74k stars 859 forks source link

An error occurred when using django storage to store images in Cloudflare R2 #1411

Closed huxiaofeng1983 closed 3 months ago

huxiaofeng1983 commented 3 months ago

Hi, I encountered some issues while using django storage to save images to Cloudflare R2.

I have configured the following content in the settings file:

from my_project.conf import const

...

STORAGES = {
    "default": {
        "BACKEND": "my_project.custom_storage.MediaStorage"
    }
}

AWS_S3_SECRET_ACCESS_KEY = const.S3_ACCESS_KEY
AWS_SECRET_ACCESS_KEY = const.S3_SECRET_KEY
AWS_STORAGE_BUCKET_NAME = const.S3_BUCKET_NAME
AWS_S3_ENDPOINT_URL = const.S3_ENDPOINT_URL
AWS_S3_CUSTOM_DOMAIN = const.S3_DOMAIN

MEDIA_FILES_LOCATION = "media"
MEDIA_URL = f"https://{AWS_S3_CUSTOM_DOMAIN}/{MEDIA_FILES_LOCATION}/"

custom_storage.py:

from django.conf import settings
from storages.backends.s3boto3 import S3Boto3Storage

class MediaStorage(S3Boto3Storage):
    location = settings.MEDIA_FILES_LOCATION
    default_acl = 'public-read'
    file_overwrite = False

When I tried to save the image using the create method, it threw an exception of sequence item 0: expected str instance, NoneType found and the response status was 423.

The packages version I am using is as follows: boto3==1.34.127 Django==4.2.13 djangorestframework==3.14.0 django-storages==1.14.3 ...

What do I need to do to make it work properly?