### Install wagtail-storages Source: https://github.com/torchbox/wagtail-storages/blob/main/README.rst Installs the wagtail-storages package. ```sh pip install wagtail-storages ``` -------------------------------- ### S3 Bucket Policy Example Source: https://github.com/torchbox/wagtail-storages/blob/main/README.rst Example of an S3 bucket policy that achieves the recommended setup for using S3 with Wagtail, including allowing public access to images and user management permissions. ```json { "Version": "2012-10-17", "Statement": [ { "Sid": "PublicGetObject", "Effect": "Allow", "Principal": "*", "Action": "s3:GetObject", "Resource": "arn:aws:s3:::[BUCKET NAME]/images/*" }, { "Sid": "AllowUserManageBucket", "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::[USER ARN]" ``` -------------------------------- ### Cloudflare CDN purging configuration example Source: https://github.com/torchbox/wagtail-storages/blob/main/README.rst Example configuration for setting up CDN purging with Cloudflare when using a custom domain for S3 storage. ```python # settings.py import os ``` -------------------------------- ### Install django-storages and boto3 Source: https://github.com/torchbox/wagtail-storages/blob/main/README.rst Installs the necessary libraries for using django-storages with AWS S3. ```sh pip install django-storages[boto3] ``` -------------------------------- ### WAGTAIL_STORAGES_DOCUMENTS_FRONTENDCACHE setting example Source: https://github.com/torchbox/wagtail-storages/blob/main/README.rst Example of how to configure the WAGTAIL_STORAGES_DOCUMENTS_FRONTENDCACHE setting, which is used by wagtail-storages to purge documents from the CDN. ```python WAGTAIL_STORAGES_DOCUMENTS_FRONTENDCACHE = { 'cloudfront': { 'BACKEND': 'wagtail.contrib.frontend_cache.backends.CloudfrontBackend', 'DISTRIBUTION_ID': 'your-distribution-id', }, } ``` -------------------------------- ### WAGTAIL_STORAGES_DOCUMENT_HOOK_ORDER setting example Source: https://github.com/torchbox/wagtail-storages/blob/main/README.rst Example of how to set a custom order for the document hook order. It's set to 100 by default. It's important that it runs after any of your hooks since it returns a response. ```python WAGTAIL_STORAGES_DOCUMENT_HOOK_ORDER = 900 ``` -------------------------------- ### Cloudflare Cache Invalidation Configuration Source: https://github.com/torchbox/wagtail-storages/blob/main/README.rst Example Python code snippet for configuring Cloudflare cache invalidation with wagtail-storages. ```python if "S3_CACHE_CLOUDFLARE_TOKEN" in os.environ: WAGTAIL_STORAGES_DOCUMENTS_FRONTENDCACHE = { "default": { "BACKEND": "wagtail.contrib.frontend_cache.backends.CloudflareBackend", "EMAIL": os.environ["S3_CACHE_CLOUDFLARE_EMAIL"], "TOKEN": os.environ["S3_CACHE_CLOUDFLARE_TOKEN"], "ZONEID": os.environ["S3_CACHE_CLOUDFLARE_ZONEID"], }, } ``` -------------------------------- ### Add wagtail-storages to INSTALLED_APPS Source: https://github.com/torchbox/wagtail-storages/blob/main/README.rst Configures Wagtail to use wagtail-storages by adding it to the INSTALLED_APPS setting. ```python # settings.py INSTALLED_APPS = [ # ... Other apps "wagtail_storages.apps.WagtailStoragesConfig", # ... Other apps ] ``` -------------------------------- ### Django settings for S3 bucket configuration Source: https://github.com/torchbox/wagtail-storages/blob/main/README.rst Configures Django settings to use an S3 bucket for file storage, with options for environment variable configuration. ```python # settings.py import os if "AWS_STORAGE_BUCKET_NAME" in os.environ: # Add django-storages to the installed apps INSTALLED_APPS = INSTALLED_APPS + ["storages"] # https://docs.djangoproject.com/en/stable/ref/settings/#default-file-storage DEFAULT_FILE_STORAGE = "storages.backends.s3boto3.S3Boto3Storage" AWS_STORAGE_BUCKET_NAME = os.environ["AWS_STORAGE_BUCKET_NAME"] # Disables signing of the S3 objects' URLs. When set to True it # will append authorization querystring to each URL. AWS_QUERYSTRING_AUTH = False # Do not allow overriding files on S3 as per Wagtail docs recommendation: # https://docs.wagtail.io/en/stable/advanced_topics/deploying.html#cloud-storage # Not having this setting may have consequences such as losing files. AWS_S3_FILE_OVERWRITE = False # Default ACL for new files should be "private" - not accessible to the # public. Images should be made available to public via the bucket policy, # where the documents should use wagtail-storages. AWS_DEFAULT_ACL = "private" # We generally use this setting in production to put the S3 bucket # behind a CDN using a custom domain, e.g. media.llamasavers.com. # https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#cloudfront if "AWS_S3_CUSTOM_DOMAIN" in os.environ: AWS_S3_CUSTOM_DOMAIN = os.environ["AWS_S3_CUSTOM_DOMAIN"] # When signing URLs is enabled, the region must be set. # The global S3 endpoint does not seem to support signed URLS. # Set this only if you will be using signed URLs. if "AWS_S3_REGION_NAME" in os.environ: AWS_S3_REGION_NAME = os.environ["AWS_S3_REGION_NAME"] # This settings lets you force using http or https protocol when generating # the URLs to the files. Set https as default. # https://github.com/jschneier/django-storages/blob/10d1929de5e0318dbd63d715db4bebc9a42257b5/storages/backends/s3boto3.py#L217 AWS_S3_URL_PROTOCOL = os.environ.get("AWS_S3_URL_PROTOCOL", "https:") ``` -------------------------------- ### Fix document ACLs command Source: https://github.com/torchbox/wagtail-storages/blob/main/README.rst Command to ensure all documents in the S3 bucket have the correct ACLs set up, especially after migrating existing files. ```sh django-admin fix_document_acls ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.