## -*- coding: utf-8 -*- # !/usr/bin/env python import oss2 from django.conf import settings class AliOSS(object): def __init__(self, access_key_id = settings.ALI_OSS_ACCESS_ID, access_key_secret = settings.ALI_OSS_ACCESS_KEY, endpoint = settings.ALI_OSS_ENDPOINT, bucket_name = settings.ALI_OSS_BUKET_NAME): auth = oss2.Auth(access_key_id, access_key_secret) self.bucket = oss2.Bucket(auth, endpoint, bucket_name, enable_crc = False) def put_object(self, key, data, headers = { # 'Content-Encoding': 'br', 'Content-Disposition': 'inline', 'Cache-Control': 'max-age=315360000' }): self.bucket.put_object(key = key, data = data, headers = headers) def put_attachment(self, key, data, headers = { 'Content-Disposition': 'attachment', 'Cache-Control': 'max-age=315360000' }): self.bucket.put_object(key = key, data = data, headers = headers) def get_object(self, key, headers={ 'Content-Disposition': 'attachment', 'Cache-Control': 'max-age=315360000' }): content = bytes() for chunk in self.bucket.get_object(key, headers=headers): content += chunk return content