Python 将图片上传至阿里云OSS对象存储
import oss2 import datetime import string import random import requests # 自定义随机名称 now = datetime.datetime.now() random_name = now.strftime("%Y%m%d%H%M%S") + ''.join([random.choice(string.digits) for _ in range(4)]) # 自有域名 cname = 'https://****.com/' # 存放OSS路径 file_name = '****/{}.jpg'.format(random_name) # AccessKeyID和AccessKeySecret auth = oss2.Auth('****', '****') # 外网访问的Bucket域名和Bucket名称 bucket = oss2.Bucket(auth, '*****', '*****', is_cname=True) # 图片链接 url = '********' resp = requests.get(url).content bucket.put_object(file_name, resp) # 最终的图片链接 get_url = cname + file_name print(get_url)