django 阿里云存储media
django 配置阿里云OSS存储media文件
一 安装django-aliyun-oss2-storage包
linux上用 pip install django-aliyun-oss2-storage 无报错,顺利安装
windows上报错:
(python3_sbs) F:\projects\virtualenv\python3_sbs\Scripts>pip install django-aliyun-oss2-storage
Collecting django-aliyun-oss2-storage
Using cached django-aliyun-oss2-storage-0.1.5.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\super\AppData\Local\Temp\pip-build-pb4u0qtw\django-aliyun-oss2-storage\setup.py", line 5, in <module>
README = readme.read()
UnicodeDecodeError: 'gbk' codec can't decode byte 0x91 in position 63: illegal multibyte sequence
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in C:\Users\super\AppData\Local\Temp\pip-build-pb4u0qtw\django-aliyun-oss2-storage\
-
下载源码 django-aliyun-oss2-storage-0.1.5.tar.gz 地址: https://github.com/xiewenya/django-aliyun-oss2-storage
-
解压进入解压后的文件夹
-
打开README.md 删除所有内容
-
安装
python setup.py install #这个过程可能会很慢要耐心
二 设置setting.py
ACCESS_KEY_ID = "xxxx"
ACCESS_KEY_SECRET = "xxxx"
END_POINT = "oss-cn-beijing.aliyuncs.com"
PREFIX_URL = 'http://'
BUCKET_NAME = "xxx"
ALIYUN_OSS_CNAME = "" # 自定义域名,如果不需要可以不填写
BUCKET_ACL_TYPE = "public-read" # private, public-read, public-read-write
DEFAULT_FILE_STORAGE = 'aliyun_oss2_storage.backends.AliyunMediaStorage'
MEDIA_URL = '/media/'
MEDIA_ROOT = "media"
顺便提一下,当在xadmin后台上传文件, filename的时候,文件会上传到路径
PREFIX_URL + BUCKET_NAME+"."END_POINT+MEDIA_URL+filename
配置结束。ueditor和xadmin上传的图片都会上传到阿里云oss中