Finn
Finn

Reputation: 163

django-ckeditor5 server error 500 with AWS S3 bucket

I am attempting to use AWS S3 bucket for storage with a Django site. I am using django-ckeditor5 for some text fields in some models. I am allowing image uploads in the ckeditor fields. This works with local storage.

However, when I attempt to upload an image while using the S3 bucket for storage, I get the following error in the terminal:

OSError: [Errno 30] 
Read-only file system: '//bucketname.s3.amazonaws.com'
[12/Apr/2023 13:33:58] "POST /ckeditor5/image_upload/ HTTP/1.1" 500 118977

For testing I have made the bucket policy completely open with the following policy:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AddPerm",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:*",
            "Resource": "arn:aws:s3:::bucketname/*"
        }
    ]
}

The relevant section from the settings.py file is:

#S3 Bucket config
AWS_ACCESS_KEY_ID = 'Key ID here'

AWS_SECRET_ACCESS_KEY = 'secret key here'
AWS_STORAGE_BUCKET_NAME = 'bucketname'
AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com'


#AWS S3 Settings
AWS_S3_FILE_OVERWRITE = False
AWS_DEFAULT_ACL = None
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3StaticStorage'

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/

AWS_LOCATION = 'static'
STATICfILES_DIRs = [
    str(BASE_DIR.joinpath('static')),
]
STATIC_ROOT = BASE_DIR / "static"
STATIC_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/{AWS_LOCATION}/'


STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    # other finders..
    'compressor.finders.CompressorFinder',
)

MEDIA_URL = '//%s.s3.amazonaws.com/media/' % AWS_STORAGE_BUCKET_NAME
MEDIA_ROOT = MEDIA_URL
AWS_QUERYSTRING_AUTH = False # needed for ckeditor with S3
AWS_DEFAULT_ACL = 'public-read'

Any ideas to fix this so that images are uploaded to the correct folder in the bucket?

Upvotes: 0

Views: 293

Answers (1)

Chris White
Chris White

Reputation: 1447

I notice here:

AWS_DEFAULT_ACL = 'public-read'

Showing suspicion on the permissions side, ala improper permissions defaulting to the public-read ACL which is not allowing writes. If this is an EC2 instance, you need to ensure it has the proper permissions in the instance profile to allow S3 writes and other permissions. Note there are separate permissions for doing things on the bucket and doing things on files within the bucket. The same goes for any other resources trying to gain access as far as the IAM permissions go.

Upvotes: 0

Related Questions