优化ucloud相关

This commit is contained in:
2024-07-12 12:59:23 +08:00
parent eb690ad02c
commit 0f672c7961
2 changed files with 11 additions and 13 deletions

View File

@@ -3,14 +3,14 @@ import logging
from ufile import filemanager
from config.ucloud import PUBLIC_KEY, PRIVATE_KEY, UPLOAD_SUFFIX, DOWNLOAD_SUFFIX, BUCKET
from config.ucloud import PUBLIC_KEY, PRIVATE_KEY, UPLOAD_SUFFIX, DOWNLOAD_SUFFIX, BUCKET, PRIVATE_EXPIRES
UFILE_HANDLER = filemanager.FileManager(PUBLIC_KEY, PRIVATE_KEY, UPLOAD_SUFFIX, DOWNLOAD_SUFFIX)
def get_private_url(key):
get_ufile_handler = filemanager.FileManager(PUBLIC_KEY, PRIVATE_KEY, UPLOAD_SUFFIX, DOWNLOAD_SUFFIX)
def get_private_url(key, bucket=BUCKET):
# 判断文件是否存在
_, resp = get_ufile_handler.head_file(BUCKET, key)
_, resp = UFILE_HANDLER.head_file(bucket, key)
if resp.status_code != 200:
logging.warning(f"uCloud中未找到({key})! status: {resp.status_code} error: {resp.error}")
return None
@@ -19,15 +19,13 @@ def get_private_url(key):
# url = get_ufile_handler.public_download_url(bucket, key)
# 获取私有空间下载url, expires为下载链接有效期单位为秒
url = get_ufile_handler.private_download_url(BUCKET, key, expires=3600)
url = UFILE_HANDLER.private_download_url(bucket, key, expires=PRIVATE_EXPIRES)
return url
def copy_file(source_bucket, source_key, target_bucket, target_key):
copy_ufile_handler = filemanager.FileManager(PUBLIC_KEY, PRIVATE_KEY, UPLOAD_SUFFIX, DOWNLOAD_SUFFIX)
# 拷贝文件
ret, resp = copy_ufile_handler.copy(target_bucket, target_key, source_bucket, source_key)
ret, resp = UFILE_HANDLER.copy(target_bucket, target_key, source_bucket, source_key)
if resp.status_code != 200:
logging.warning(
f"将({source_key})从({source_bucket})拷贝到({target_bucket})失败! status: {resp.status_code} error: {resp.error}")
@@ -35,11 +33,9 @@ def copy_file(source_bucket, source_key, target_bucket, target_key):
return True
def upload_file(key, file_path):
putufile_handler = filemanager.FileManager(PUBLIC_KEY, PRIVATE_KEY, UPLOAD_SUFFIX, DOWNLOAD_SUFFIX)
def upload_file(key, file_path, bucket=BUCKET):
# 普通上传文件至空间
ret, resp = putufile_handler.putfile(BUCKET, key, file_path, header=None)
ret, resp = UFILE_HANDLER.putfile(bucket, key, file_path, header=None)
if resp.status_code != 200:
logging.warning(f"上传({key})失败! status: {resp.status_code} error: {resp.error}")
return False