This commit is contained in:
2024-12-07 15:00:10 +08:00
parent ea5e994a3b
commit fb51d144c0
6 changed files with 116 additions and 35 deletions

View File

@ -1,7 +1,9 @@
import os
import requests
def upload_to_oss_use_signed_url(url, file_path):
def upload_to_oss(url, file_path):
"""
使用签名URL上传文件到OSS
:param str url: 签名URL
@ -15,3 +17,22 @@ def upload_to_oss_use_signed_url(url, file_path):
except Exception as e:
print(e)
return False
def download_from_oss(url, file_path):
"""
使用签名URL下载文件到OSS
:param str url: 签名URL
:param Union[LiteralString, str, bytes] file_path: 文件路径
:return bool: 是否成功
"""
file_dir, file_name = os.path.split(file_path)
if not os.path.exists(file_dir):
os.makedirs(file_dir)
try:
response = requests.get(url)
with open(file_path, 'wb') as f:
f.write(response.content)
return True
except Exception as e:
print(e)
return False