提交 5f0e701f authored 作者: zw.wang's avatar zw.wang

feat: [utils] 阿里云oss工具

上级 64da3c75
import os
import oss2
import urllib.request
from functools import lru_cache
from dynaconf import settings
from oss2.exceptions import NoSuchKey
local_endpoint = 'https://oss-cn-{}-internal.aliyuncs.com' # 局域网配置
......@@ -12,9 +14,11 @@ local_endpoint = config.get('endpoint', local_endpoint.format(region))
@lru_cache()
def _get_bucket():
def _get_bucket(bucket_name=None, l_region=None):
endpoint = 'https://oss-cn-{}.aliyuncs.com'.format(l_region or region) if l_region else local_endpoint
bucket_name = bucket_name or config['bucket_name']
bucket = oss2.Bucket(oss2.Auth(config['access_key_id'], config['access_key_secret']),
local_endpoint, config['bucket_name'])
endpoint, bucket_name)
return bucket
......@@ -41,14 +45,14 @@ def oss_delete_file(origin_file):
return True if len(res) > 0 else False
def oss_batch_delete_files(files_list):
def oss_batch_delete_files(files_list, bucket_name=None, l_region=None):
""" 批量删除云端文件
"""
bucket = _get_bucket()
bucket = _get_bucket(bucket_name, l_region)
bucket_files = []
for origin_file in files_list:
if origin_file.startswith('http://') or origin_file.startswith('https://'):
bucket_files.append(origin_file.split('/')[-1])
bucket_files.append('/'.join(origin_file.split('/')[3:]))
else:
bucket_files.append(origin_file)
......@@ -60,11 +64,15 @@ def oss_download_file(origin_file, local_file):
"""
下载视频文件
"""
old_origin_file = origin_file
try:
bucket = _get_bucket()
if origin_file.startswith('http://') or origin_file.startswith('https://'):
origin_file = origin_file.split('/')[-1]
origin_file = '/'.join(origin_file.split('/')[3:])
bucket.get_object_to_file(origin_file, local_file)
except NoSuchKey:
urllib.request.urlretrieve(old_origin_file, local_file)
return local_file
......@@ -73,3 +81,5 @@ if __name__ == '__main__':
# print(oss_download_file('https://test-qzwjtest.oss-cn-hangzhou.aliyuncs.com/test-2.mp4', 't.mp4'))
# print(oss_delete_file('https://test-qzwjtest.oss-cn-hangzhou.aliyuncs.com/test-2.mp4'))
oss_download_file('D00268229_2020-10-23_14-07-13.mp4', '/tmp/v3/videos/D00268229_2020-10-23_14-07-13.mp4')
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论