# -*- coding: utf-8 -*- import oss2 from settings import ALI_ACCESS_KEY, ALI_SECRET_KEY, OSS_BUCKET, OSS_ENDPOINT class AliOss(): def __init__(self): self.ali_access_key = ALI_ACCESS_KEY self.ali_secret_key = ALI_SECRET_KEY self.ali_oss_bucket = OSS_BUCKET self.ali_endpoint = OSS_ENDPOINT def upload_file(self, file_path, target_path, content_type='application/octet-stream'): """ 向阿里云oss上传文件 :return: """ auth = oss2.Auth(self.ali_access_key, self.ali_secret_key) bucket = oss2.Bucket(auth, self.ali_endpoint, self.ali_oss_bucket) headers = { 'Content-Type': content_type, 'x-oss-object-acl': 'public-read' } result = bucket.put_object_from_file(target_path, file_path, headers) return result