前提・実現したいこと
Wagtail(Django)の静的ファイルをAmazon S3互換のオブジェクトストレージ、MinIOに格納しようとしています。
python manage.py collectstatic
で静的ファイルをMinIOに格納したあと、Djangoをpython manage.py runserver
で立ち上げた際に静的ファイルが表示されません。
発生している問題・エラーメッセージ
ブラウザのコンソールを見ると、返ってきた静的ファイルのMIME typeがtext/html
であると表示されています。
ブラウザのエラーメッセージその1
Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost:9090/minio/wagtail/wagtail/css/huidetang.css".
ブラウザのエラーメッセージその2
Cross-Origin Read Blocking (CORB) blocked cross-origin response http://localhost:9090/minio/wagtail/wagtail/css/huidetang.css with MIME type text/html. See https://www.chromestatus.com/feature/5629709824032768 for more details.
CSSのURLに直リンクで飛ぶとMinIOのHTMLインターフェイスが表示されています。
該当のソースコード
リポジトリはhuideyeren/huidetangにあります。
localhost.py(抜粋)
if "AWS_STORAGE_BUCKET_NAME" in os.environ: # Add django-storages to the installed apps INSTALLED_APPS = INSTALLED_APPS + [ "storages", "wagtail_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:") AWS_S3_ENDPOINT_URL = os.environ["AWS_S3_ENDPOINT_URL"] AWS_LOCATION = "wagtail" AWS_S3_USE_SSL = False AWS_S3_SECURE_URLS = False STATICFILES_STORAGE = "storages.backends.s3boto3.S3StaticStorage" STATIC_URL = "http://localhost:9090/minio/wagtail/%s/" % (AWS_LOCATION)
docker-compose.yml
yaml
1version: '3.7' 2services: 3 nginx: 4 image: nginx:latest 5 volumes: 6 - ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf 7 ports: 8 - 80:80 9 depends_on: 10 - backend 11 - frontend 12 postgres: 13 image: postgres:12 14 restart: always 15 environment: 16 POSTGRES_USER: postgres 17 POSTGRES_PASSWORD: password 18 ports: 19 - '5432:5432' 20 volumes: 21 - db-data:/var/lib/postgresql/data:cached 22 backend: 23 build: 24 context: backend 25 dockerfile: Dockerfile 26 volumes: 27 - ./backend:/app/:cached 28 environment: 29 PYTHONPATH: . 30 DATABASE_URL: 'postgresql://postgres:password@postgres:5432/postgres' 31 AWS_S3_CUSTOM_DOMAIN: 'localhost:9090/minio/wagtail' 32 AWS_S3_ENDPOINT_URL: 'http://minio:9000/' 33 AWS_STORAGE_BUCKET_NAME: 'wagtail' 34 AWS_ACCESS_KEY_ID: 'huidetang' 35 AWS_S3_REGION_NAME: 'us-east-1' 36 AWS_SECRET_ACCESS_KEY: 'jsYIDsqqIt9JShu' 37 AWS_S3_URL_PROTOCOL: 'http:' 38 depends_on: 39 - "postgres" 40 - "minio" 41 frontend: 42 build: 43 context: frontend 44 dockerfile: Dockerfile 45 stdin_open: true 46 volumes: 47 - './frontend:/app:cached' 48 - './frontend/node_modules:/app/node_modules:cached' 49 environment: 50 - NODE_ENV=development 51 minio: 52 image: minio/minio:latest 53 ports: 54 - 9090:9000 55 volumes: 56 - ./minio/data:/data 57 - ./minio/export:/export 58 - ./minio/config:/root/.minio 59 - ./minio/policies:/policies 60 environment: 61 MINIO_ACCESS_KEY: 'huidetang' 62 MINIO_SECRET_KEY: 'jsYIDsqqIt9JShu' 63 entrypoint: 64 - /bin/sh 65 - -c 66 command: 67 - "mkdir -p /data/.minio.sys/buckets; cp -r /policies/* /data/.minio.sys/; cp -r /export/* /data/; /usr/bin/minio server /data" 68 createbuckets: 69 image: minio/mc 70 depends_on: 71 - minio 72 entrypoint: > 73 /bin/sh -c " 74 until (/usr/bin/mc config host add wagtail http://minio:9000 huidetang jsYIDsqqIt9JShu) do echo '...waiting...' && sleep 1; done; 75 /usr/bin/mc mb wagtail/wagtail; 76 /usr/bin/mc admin user add wagtail wagtail jsYIDsqqIt9JShu; 77 exit 0; 78 " 79volumes: 80 db-data: 81
policy.json(MinIO)
json
1{ 2 "Version": "2012-10-17", 3 "Statement": [ 4 { 5 "Sid": "AllowUserManageBucket", 6 "Effect": "Allow", 7 "Principal": "*", 8 "Action": [ 9 "s3:ListBucket", 10 "s3:GetBucketLocation", 11 "s3:ListBucketMultipartUploads", 12 "s3:ListBucketVersions" 13 ], 14 "Resource": "arn:aws:s3:::wagtail" 15 }, 16 { 17 "Sid": "AllowUserManageBucketObjects", 18 "Effect": "Allow", 19 "Principal": "*", 20 "Action": [ 21 "s3:GetObject", 22 "s3:PutObject", 23 "s3:DeleteObject" 24 ], 25 "Resource": "arn:aws:s3:::wagtail/*" 26 } 27 ] 28} 29
試したこと
静的ファイルの設定は最初以下のようにしていました。
python
1STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
補足情報(FW/ツールのバージョンなど)
Docker ComposeとVS CodeのdevContainerで動かしています。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。