- 安装依赖包
pip install oss2
- 核心代码
import oss2
access_key_id = 'LTA*******************'
access_key_secret = 'ZAx*******************************'
bucket_name = 'caucshop'
endpoint = 'oss-cn-beijing.aliyuncs.com'
# 创建bucket对象
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)
# 待上传的文件路径
file_path_local = "./Snipaste_2023-05-13_18-54-02.jpg"
# 上传到oss后保保存的路径
file_path_oss = "goodImgsCompresss/"+file_path_local.split("/")[-1]
# 读取文件
with open(file_path_local, "rb") as f:
data = f.read()
# 上传文件
bucket.put_object(file_path_oss, data)
# 获取文件的url
file_url_oss = "https://"+bucket_name+"."+endpoint+"/"+file_path_oss;
print(file_url_oss)
执行结果,得到文件在oss中的存储地址,我这里采用的是公共读的权限
https://caucshop.oss-cn-beijing.aliyuncs.com/goodImgsCompresss/Snipaste_2023-05-13_18-54-02.jpg
评论 (0)