python - Django 配置阿里云 oss

python

一.  安装依赖包

pip install oss2

pip install django-oss-storage

二.  settings.py 中添加 oss 配置 (配置完毕后就会生效, 上传地址, 访问路径都会变成 oss 服务器上的)

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

OSS_ACCESS_KEY_ID = ""  

OSS_ACCESS_KEY_SECRET = ""

OSS_ENDPOINT = "oss-cn-beijing.aliyuncs.com" # 访问域名, 根据服务器上的实际配置修改

OSS_BUCKET_NAME = "" # oss 创建的 BUCKET 名称

# 添加下面配置后 Django admin 后台上传的 ImageField, FileField 类型的字段都会被自动上传到 oss 的服务器中, 访问路径也会自动替换
# 如果注释掉的话 oss 的配置会失效, 上传文件会存储到本地, 且访问路径也会变成本地

DEFAULT_FILE_STORAGE = 'django_oss_storage.backends.OssMediaStorage'

三.  文件上传路径说明

class Base(models.Model):

# oss 服务内的路径为: /media/base/shipin/xx.mp4 (也是本地数据库存储的路径)

# oss 服务器文件的访问地址为 https://ztest.oss-cn-beijing.aliyuncs.com/media/base/shipin/xx.mp4?xxxx

shipin = models.FileField(verbose_name="视频文件", upload_to="base/shipin/", null=True, blank=True)

四.  异常总结

1. 'Message': 'The bucket you access does not belong to you.'

解决: settings 文件中检查  OSS_BUCKET_NAME  填写的名字与服务器上创建的是否一致 

2. 'Message': 'The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.'

解决: settings 文件中 OSS_ENDPOINT  填写错误(与服务器上配置的不一致), 其实在错误信息里面可以直接拿到, 比如这条报错:

oss2.exceptions.AccessDenied: {'status': 403, 'x-oss-request-id': '', 'details': {'Code': 'AccessDenied', 'Message': 'The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.', 'RequestId': '', 'HostId': 'xxx.oss-cn-hangzhou.aliyuncs.com', 'Bucket': 'xxx', 'Endpoint': 'oss-cn-beijing.aliyuncs.com'}}   

# 在错误信息的最后会提示你的访问地址

五.  后台 admin 在 创建/更新 数据时 对图片进行处理 (缩放, 剪裁等), 删除数据 的同时将 oss 服务器上的对应文件也删除

1. 图片处理的相关操作 - 官方文档

2. 监听事件的其他相关操作

class Base(models.Model):

image = models.FileField(verbose_name="图片文件", upload_to="base/image/", null=True, blank=True)

# 该表删除操作时监听

@receiver(post_delete, sender=Base)

def delete_upload_files(sender, instance, **kwargs):

# 删除 oss仓库 图片

files = getattr(instance, 'image')

if not files:

return

# 连接 oss

auth = oss2.Auth(settings.OSS_ACCESS_KEY_ID, settings.OSS_ACCESS_KEY_SECRET)

# 获取 bucket

bucket = oss2.Bucket(auth, settings.OSS_ENDPOINT, settings.OSS_BUCKET_NAME)

# 删除文件

bucket.delete_object(files.file.name)

# 该表更新/创建操作时监听

@receiver(post_save, sender=Base)

def save_upload_files(sender, instance, **kwargs):

# 更新 oss仓库 图片

files = getattr(instance, 'image')

if not files:

return

# 要对图片进行处理的样式

style1 = "image/crop,x_10,y_10,w_200,h_200,g_se"

style2 = 'image/resize,w_700,h_260,m_lfit'

style3 = 'image/resize,m_fixed,h_260,w_700'

auth = oss2.Auth(settings.OSS_ACCESS_KEY_ID, settings.OSS_ACCESS_KEY_SECRET)

bucket = oss2.Bucket(auth, settings.OSS_ENDPOINT, settings.OSS_BUCKET_NAME)

process = "{0}|sys/saveas,o_{1},b_{2}".format(style,

oss2.compat.to_string(base64.urlsafe_b64encode(

oss2.compat.to_bytes(file_name))),

oss2.compat.to_string(base64.urlsafe_b64encode(

oss2.compat.to_bytes(settings.OSS_BUCKET_NAME))))

# 更新文件

bucket.process_object(files.file.name, process)

以上是 python - Django 配置阿里云 oss 的全部内容, 来源链接: utcz.com/z/389365.html

回到顶部