使用python上传和下载文件到oss中
参看阿里接口文档链接: https://help.aliyun.com/document_detail/88426.html
上传本地文件到oss
# -*- coding: utf-8 -*-
import oss2
import os
AccessKeyId = 'xxx'
AccessKeySecret = 'xxx'
# [这个是必须的]
# 参考链接: https://help.aliyun.com/document_detail/31837.html
ENDPOINT = 'https://oss-cn-hangzhou.aliyuncs.com'
class HandleOSS(object):
def __init__(self, key_id, key_secret, endpoint, bucket='qmgy-private-hz-dev'):
'''
初始化
:param key_id:
:param key_secret:
:param bucket: bucket名称
'''
self.key_id = key_id
self.key_secret = key_secret
self.endpoint = endpoint
self.bucket = bucket
self.auth = oss2.Auth(key_id, key_secret)
if bucket:
self.bucket = oss2.Bucket(self.auth, self.endpoint, self.bucket)
def update_one_file(self, file_path, oss_path):
'''
将文件上传到oss上
:param file_dir: 要上传的文件
:param oss_dir: oss上的路径, 要存在oss上的那个文件
:return:
'''
file_name = file_path.split('/')[-1]
oss_save_path = f'{oss_path}/{file_name}'
res = self.bucket.put_object_from_file(oss_save_path, file_path)
if __name__ == '__main__':
file_path = "文件路径/demo_test.txt"
# 前面已经设置了bucket, 所以这里只需要设置后面的路径即可
oss_path = "algo/cpv_result"
handle = HandleOSS(AccessKeyId, AccessKeySecret, ENDPOINT)
handle.update_one_file(file_path,oss_path)
下载文件
# -*- coding: utf-8 -*-
import oss2
import os
AccessKeyId = 'xxx'
AccessKeySecret = 'xxx'
# [这个是必须的]
# 参考链接: https://help.aliyun.com/document_detail/31837.html
ENDPOINT = 'https://oss-cn-hangzhou.aliyuncs.com'
class HandleOSS(object):
def __init__(self, key_id, key_secret, endpoint, bucket='qmgy-private-hz-dev'):
'''
初始化
:param key_id:
:param key_secret:
:param bucket: bucket名称, 这里是qmgy-private-hz-dev
'''
self.key_id = key_id
self.key_secret = key_secret
self.endpoint = endpoint
self.bucket = bucket
self.auth = oss2.Auth(key_id, key_secret)
if bucket:
self.bucket = oss2.Bucket(self.auth, self.endpoint, self.bucket)
def update_one_file(self, file_path, oss_path):
'''
将文件上传到oss上
:param file_dir: 要上传的文件
:param oss_dir: oss上的路径, 要存在oss上的那个文件
:return:
'''
file_name = file_path.split('/')[-1]
print("file_path===: ", file_path)
oss_save_path = f'{oss_path}/{file_name}'
print("oss_save_paht===: ", oss_save_path)
res = self.bucket.put_object_from_file(oss_save_path, file_path)
print(res.status)
# print(res.data)
def download_one_file(self, oss_path, save_dir):
'''
下载单个文件
:param oss_path: 文件所在的oss地址
:param save_dir: 要保存在本地的文件目录
:return:
'''
file_name = oss_path.split('/')[-1]
save_path = os.path.join(save_dir, file_name)
print("save_path===:", save_path)
result = self.bucket.get_object_to_file(oss_path, save_path)
if result.status == 200:
return '下载成功'
pass
def download_many_file(self, oss_dir, save_dir):
'''
批量下载文件
:param oss_dir: oss上要下载的文件目录
:param save_dir: 要存在本地的文件目录
:return:
'''
obj = oss2.ObjectIterator(self.bucket, prefix=oss_dir)
# 遍历oss文件夹获取所有的对象列表,i.key是文件的完整路径
for i in obj:
# 如果文件是以斜杠结尾的,说明不是文件,则跳过
if i.key.endswith('/'):
continue
# 文件名:文件路径按照斜杠分割取最后一个
file_name = i.key.split('/')[-1]
# 下载到的具体路径
save_path = os.path.join(save_dir, file_name)
# 从oss下载
self.bucket.get_object_to_file(i.key, save_path)
if __name__ == '__main__':
file_path = "xx/demo_test.txt"
oss_path = "algo/cpv_result"
download_oss_path = "algo/cpv_result/demo_test.txt"
save_dir = "xxx"
handle = HandleOSS(AccessKeyId, AccessKeySecret, ENDPOINT)
handle.update_one_file(file_path,oss_path)
print("上传成功, 开始下载")
handle.download_one_file(download_oss_path, save_dir)
print('目录下载开始')
handle.download_many_file(oss_path, save_dir)