17

Django 集成阿里云OSS

 3 years ago
source link: https://foofish.net/django-oss2.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

在django项目中,如果希望静态资源存储到阿里云的OSS中,不需要写一行代码,因为别人已经造好了轮子,你就不要重复造了。记录下配置过程。

第一步: 安装依赖包

pip install oss2
pip install django-oss-storage

第二步: setting.py配置OSS参数

如果你使用过阿里云的OSS的话,下面这些参数都能在控制台找到

OSS_ACCESS_KEY_ID  = "xxxxx"
OSS_ACCESS_KEY_SECRET  = "xxxxx"
OSS_ENDPOINT   = "xxxxx"
OSS_BUCKET_NAME  = "xxxxx"

# mediafile自动上传
DEFAULT_FILE_STORAGE  = 'django_oss_storage.backends.OssMediaStorage'

MEDIA_URL = '/media/'

配置完成后,所有FileField类型的字段将自动上传到阿里云。

qrcode = models.FileField(max_length=200, null=True, blank=True, verbose_name="二维码")

数据库表中存储的是上传文件的名称,而完整的访问路径就是:OSS_ENDPOINT/media/filename

有问题可以扫描二维码和我交流

关注公众号「Python之禅」,回复「1024」免费获取Python资源

vQremuy.jpg!web

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK