PERF cos上传下载可选bucket

This commit is contained in:
kyj@bowong.ai 2025-03-13 17:40:31 +08:00
parent be21b58126
commit cc0b3da745
1 changed files with 16 additions and 8 deletions

View File

@ -2,6 +2,7 @@ import json
import os
import urllib
import loguru
import yaml
from qcloud_cos import CosConfig, CosS3Client, CosClientError, CosServiceError
@ -13,6 +14,7 @@ class COSDownload:
def INPUT_TYPES(s):
return {
"required": {
"cos_bucket": ("STRING", {"default": "bwkj-cos-1324682537"}),
"cos_key": ("STRING", {"multiline": True}),
}
}
@ -22,11 +24,16 @@ class COSDownload:
FUNCTION = "download"
CATEGORY = "不忘科技-自定义节点🚩"
def download(self, cos_key):
def download(self, cos_bucket, cos_key):
loguru.logger.info("download to {}".format(os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
"download",
os.path.dirname(cos_key),
)))
if os.sep in cos_key or "/" in cos_key or "\\" in cos_key:
os.makedirs(
os.path.join(
os.path.dirname(os.path.abspath(__file__)),
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
"download",
os.path.dirname(cos_key),
),
@ -49,10 +56,10 @@ class COSDownload:
)
client = CosS3Client(config)
response = client.download_file(
Bucket=yaml_config["bucket"],
Bucket=cos_bucket,
Key=cos_key,
DestFilePath=os.path.join(
os.path.dirname(os.path.abspath(__file__)),
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
"download",
os.path.dirname(cos_key),
os.path.basename(cos_key),
@ -63,7 +70,7 @@ class COSDownload:
print(f"下载失败 {e}")
return (
os.path.join(
os.path.dirname(os.path.abspath(__file__)),
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
"download",
os.path.dirname(cos_key),
os.path.basename(cos_key),
@ -78,6 +85,7 @@ class COSUpload:
def INPUT_TYPES(s):
return {
"required": {
"cos_bucket": ("STRING", {"default": "bwkj-cos-1324682537"}),
"path": ("STRING", {"multiline": True}),
}
}
@ -88,7 +96,7 @@ class COSUpload:
FUNCTION = "upload"
CATEGORY = "不忘科技-自定义节点🚩"
def upload(self, path):
def upload(self, cos_bucket, path):
for i in range(0, 10):
try:
with open(
@ -106,7 +114,7 @@ class COSUpload:
)
client = CosS3Client(config)
response = client.upload_file(
Bucket=yaml_config["bucket"],
Bucket=cos_bucket,
Key="/".join(
[
yaml_config["subfolder"],
@ -123,7 +131,7 @@ class COSUpload:
except CosClientError or CosServiceError as e:
raise RuntimeError("上传失败")
data = {"prompt_id": "",
"video_url": "https://{}.cos.{}.myqcloud.com/{}".format(yaml_config['bucket'], yaml_config['region'],
"video_url": "https://{}.cos.{}.myqcloud.com/{}".format(cos_bucket, yaml_config['region'],
'/'.join([yaml_config['subfolder'],
path.split('/')[
-1] if '/' in path else