oss服务:bucket中的图片,如果想不校验直接访问,需要设置bucket为公共读属性。
# -*- coding: utf-8 -*-
import datetime
import shortuuid
from linde import settings
class Oss:
"""
oss存储类
上传bytes流,返回状态码和url
"""
def __init__(self, access_key_id=settings.ACCESS_KEY_ID, access_key_secret=settings.ACCESS_KEY_SECRET,
endpoint=settings.ENDPOINT, bucket_name=settings.BUCKET_NAME):
import oss2
# 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
auth = oss2.Auth(access_key_id, access_key_secret)
# Endpoint以杭州为例,其它Region请按实际情况填写。‘http://oss-cn-hangzhou.aliyuncs.com‘
self.bucket = oss2.Bucket(auth, endpoint, bucket_name)
def upload_bytes(self, file_bytes, image_name):
"""上传bytes文件"""
result = self.bucket.put_object(‘{}‘.format(image_name), file_bytes)
oss_obj = Oss()
# 上传后,可以访问的 url 的组成
photo_name += ‘.jpg‘
domain = ‘http://{}.{}/‘.format(settings.BUCKET_NAME, settings.ENDPOINT)
url_photo = domain + photo_name
# ENDPOINT = ‘oss-cn-shanghai.aliyuncs.com‘
# BUCKET_NAME = ‘ai-gater‘