本文为博主原创,未经许可严禁转载。
本文链接:https://blog.csdn.net/zyooooxie/article/details/108194392
去年某个需求用到 上传本地文件到阿里云oss + 从oss下载文件做内容校验; 从没接触过的我,只能边学习边实践,如今趁着有机会,做个总结;
个人博客:https://blog.csdn.net/zyooooxie
阿里云OSS
官方介绍 https://help.aliyun.com/document_detail/31817.html
图形化管理工具 ossbrowser
我实际 是在用这个ossbrowser, 官方文档 https://help.aliyun.com/document_detail/61872.html
Python 代码
官方文档 https://help.aliyun.com/document_detail/32026.html
https://aliyun-oss-python-sdk.readthedocs.io/en/stable/api.html#
本身我要上传、下载的文件 大小在10M内,属于小文件,就是普通的上传、下载;
上传 put_object_from_file()
https://help.aliyun.com/document_detail/88426.html#title-urs-0wu-mpi
class OssUpload(unittest.TestCase):
"""
Oss上传文件
"""
def upload_csv(self, date, file_name, object_dir='settlement_file'):
os.chdir(r'D:\csdn\zyooooxie')
if date.find('-') == -1:
date = '-'.join([date[:4], date[4:6], date[6:]])
oss_auth = oss2.Auth('zyooooxie', 'zyooooxie')
oss_bucket = oss2.Bucket(oss_auth, 'http://oss-cn-zyooooxie-zyooooxie.zyooooxie.com', 'finance-zyooooxie-zyooooxie-zyooooxie')
# OSS是没有文件夹这个概念的,所有元素都是以Object来存储。
# 创建 模拟文件夹 本质上来说是创建一个文件名以正斜线(/)结尾,大小为0 KB的Object。
# 这个Object可以被上传和下载,只是控制台会对以正斜线(/)结尾的Object以文件夹的方式展示
remote_name = '/'.join([object_dir, date, file_name])
result = oss_bucket.put_object_from_file(key=remote_name, filename=file_name)
if result.status == 200:
Log.info('上传成功 status为{}'.format(result.status))
下载 get_object_to_file()
https://help.aliyun.com/document_detail/88442.html
实际因为涉及到 某不确定的 object_dir ,下载时候要先列举下所有文件 做个筛选;
https://help.aliyun.com/document_detail/88458.html
class OssDownload(unittest.TestCase):
"""
Oss下载文件
"""
def setUp(self):
warnings.simplefilter('ignore', ResourceWarning)
def download_csv(self, object_dir, date, file_type='zip', file_name=None, qjs='yes'):
# 下面2行 实际是有省略
if qjs == 'yes':
re_str = r'^{}{}.*{}$'.format(object_dir, date, file_type)
end_point = 'http://oss-cn-zyooooxie-zyooooxie.zyooooxie.com'
oss_auth = oss2.Auth('zyooooxie', 'zyooooxie')
oss_bucket = oss2.Bucket(oss_auth, end_point, 'finance-zyooooxie-zyooooxie-zyooooxie')
if file_name is not None:
file = [abc.key for abc in oss2.ObjectIterator(oss_bucket) if abc.key.find(file_name) != -1 and abc.key.find(object_dir) != -1]
else:
file = [i.key for i in oss2.ObjectIterator(oss_bucket) if re.search(re_str, i.key)]
Log.info(file)
if len(file) == 0:
raise Exception('file 没找到: {}'.format(file))
elif len(file) == 1:
Log.info('file 找到了:{}'.format(file))
self.download_csv_2(oss_bucket, file[0], date, file[0].split('/')[-1])
else:
raise Exception('file数量 不对')
Log.info('文件已下载')
# os.system("explorer {}".format(lu_old))
def download_csv_2(self, oss_bucket, key, date, remote_name):
# 下面2行 实际是有省略
lu = r'D:\work\zyooooxie\csdn'
os.chdir(lu)
try:
result = oss_bucket.get_object_to_file(key, remote_name)
if result.status == 200:
Log.debug('文件已下载 {}'.format(remote_name))
except NoSuchKey as e:
Log.info('下载失败 {}'.format(e))
os.chdir(r'..')
这篇分享讲的很粗,是因为 我实际工作只用到这些 = = ;
交流技术 欢迎+QQ 153132336 zy
个人博客 https://blog.csdn.net/zyooooxie