Use when configuring Django to store static and media files on AWS S3 with django-storages. Invoke when working with the STORAGES setting, S3 buckets, presigned
复制下面这句话,粘贴给 Claude Code、Codex、Cursor 等 AI 编程工具,它会读取安装说明并在你确认后完成安装。
请阅读 https://ai.atlankj.com/install/asset/gh-django-storages-s3-5d56021f0369 ,按照其中的说明把「django-storages-s3」安装到你(当前 AI 工具)中。执行前先告诉我将运行的命令和写入的位置,等我确认。
查看 AI 将读取的安装说明正在读取 GitHub 原文…
内容来自 GitHub 原始文件,由原作者维护。在 GitHub 查看
Senior Django specialist for production-grade file storage on AWS S3 via django-storages and boto3 — public and private media, static files, presigned URLs, and CloudFront.
STORAGES dict or legacy DEFAULT_FILE_STORAGEFileField/ImageField storage to S3 without code changespip install django-storages[s3] boto3; add "storages" to INSTALLED_APPSSTORAGES dict — Set default (media) and staticfiles backends with separate location prefixesSTORAGES entries when neededcollectstatic, confirm uploads land in S3, and mock S3 in tests with InMemoryStorage or motoLoad detailed guidance based on context:
| Topic | Reference | Load When |
|---|---|---|
| Settings & STORAGES | references/configuration.md | Core settings, 4.2+ vs legacy, CloudFront |
| Custom backends | references/custom-backends.md | Public vs. private buckets, per-field storage |
| Presigned URLs | references/presigned-urls.md | Download links, direct browser uploads |
| Testing & IAM | references/testing-storages.md | Mocking S3, IAM policy, common pitfalls |
The snippet below demonstrates the core MUST DO constraints: env-loaded credentials, STORAGES dict, separate media/static locations, and default_acl=None on the media backend.
# settings.py
import os
AWS_STORAGE_BUCKET_NAME = os.environ["AWS_STORAGE_BUCKET_NAME"]
AWS_S3_REGION_NAME = os.environ.get("AWS_S3_REGION_NAME", "us-east-1")
AWS_S3_CUSTOM_DOMAIN = f"{AWS_STORAGE_BUCKET_NAME}.s3.{AWS_S3_REGION_NAME}.amazonaws.com"
# On EC2/ECS/Lambda, omit keys entirely — boto3 uses the attached IAM role.
STORAGES = {
"default": { # media uploads
"BACKEND": "storages.backends.s3boto3.S3Boto3Storage",
"OPTIONS": {
"bucket_name": AWS_STORAGE_BUCKET_NAME,
"location": "media",
"default_acl": None, # rely on bucket policy, not per-object ACLs
"file_overwrite": False,
"querystring_auth": False, # public objects → clean URLs
},
},
"staticfiles": {
"BACKEND": "storages.backends.s3boto3.S3StaticStorage",
"OPTIONS": {
"bucket_name": AWS_STORAGE_BUCKET_NAME,
"location": "static",
},
},
}
MEDIA_URL = f"https://{AWS_S3_CUSTOM_DOMAIN}/media/"
STATIC_URL = f"https://{AWS_S3_CUSTOM_DOMAIN}/static/"
# models.py — uploads go straight to S3 on save()
from django.db import models
class Document(models.Model):
file = models.FileField(upload_to="docs/") # uses STORAGES["default"]
When reviewing a project that already uses S3 (not greenfield), walk this checklist — each item is a constraint below rephrased as "find X, confirm Y":
grep -rn "AWS_SECRET_ACCESS_KEY\|aws_secret" settings/ → confirm values come from os.environ/django-environ or an IAM role, never literals committed to the repo.grep -rn "default_acl\|AWS_DEFAULT_ACL" . → on buckets created after April 2023, every value must be None. Any "public-read"/"private" will raise AccessControlListNotSupported; public access belongs in a bucket policy.STORAGES dict, not DEFAULT_FILE_STORAGE/STATICFILES_STORAGE (removed in Django 5.1, so silently ignored on 5.1/5.2/6.0); confirm the static class is S3StaticStorage, not a fabricated name.default (media) and staticfiles have distinct location prefixes or buckets so collectstatic never collides with uploads.region_name (or the global AWS_S3_REGION_NAME) matches the bucket's real region and that AWS_S3_CUSTOM_DOMAIN includes the region segment for non-us-east-1 buckets.querystring_auth=True and custom_domain=None; confirm presigned .url() results aren't cached past AWS_QUERYSTRING_EXPIRE.file_overwrite=False, confirm replaced files are explicitly deleted (otherwise superseded objects leak).Get/Put/Delete/ListBucket on the bucket ARN, not broader S3 access.default_acl=None so bucket policies (not object ACLs) control accesslocation prefixes or separate bucketsSTORAGES dict on Django 4.2+ (same config through 5.2 LTS and 6.0); DEFAULT_FILE_STORAGE/STATICFILES_STORAGE were removed in 5.1, so reserve them for < 4.2 onlycustom_domain=None on any backend that issues presigned URLsInMemoryStorage or moto) in tests instead of hitting real bucketsAWS_SECRET_ACCESS_KEY in settings.py or commit itquerystring_auth=True with a custom_domain (presigning breaks)Get/Put/Delete/ListBucket on the bucket ARNdjango-storages, S3Boto3Storage, S3StaticStorage, boto3, STORAGES dict, presigned URLs, generate_presigned_post, CloudFront, IAM policy, InMemoryStorage, moto
django-expert — core Django models, DRF, and ORM that produce the files this skill persists to S3fullstack-guardian — secure end-to-end upload flows and access control around stored filesdevops-engineer — provisioning the S3 buckets, IAM roles, and CloudFront distributions this skill targets