ADD 增加S3上传下载节点

This commit is contained in:
2025-04-01 11:09:44 +08:00
parent 7babcd2869
commit f506eb538b
4 changed files with 128 additions and 57 deletions

View File

@@ -25,20 +25,18 @@ class COSDownload:
CATEGORY = "不忘科技-自定义节点🚩"
def download(self, cos_bucket, cos_key):
loguru.logger.info("download to {}".format(os.path.join(
cos_key_in = cos_key.replace("/",os.sep)
destination = 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.dirname(os.path.abspath(__file__))),
"download",
os.path.dirname(cos_key),
),
exist_ok=True,
)
os.path.dirname(cos_key_in),
os.path.basename(cos_key_in)
)
loguru.logger.info(f"COS DOWNLOAD to {destination}")
os.makedirs(
os.path.dirname(destination),
exist_ok=True,
)
for i in range(0, 10):
try:
with open(
@@ -58,23 +56,13 @@ class COSDownload:
response = client.download_file(
Bucket=cos_bucket,
Key=cos_key,
DestFilePath=os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
"download",
os.path.dirname(cos_key),
os.path.basename(cos_key),
),
DestFilePath=destination
)
break
except CosClientError or CosServiceError as e:
print(f"下载失败 {e}")
raise Exception(f"COS下载失败 bucket {cos_bucket}; key {cos_key}")
return (
os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
"download",
os.path.dirname(cos_key),
os.path.basename(cos_key),
),
destination,
)
@@ -87,6 +75,7 @@ class COSUpload:
"required": {
"cos_bucket": ("STRING", {"default": "bwkj-cos-1324682537"}),
"path": ("STRING", {"multiline": True}),
"subfolder": ("STRING", {"default": "test"}),
}
}
@@ -96,7 +85,18 @@ class COSUpload:
FUNCTION = "upload"
CATEGORY = "不忘科技-自定义节点🚩"
def upload(self, cos_bucket, path):
def upload(self, cos_bucket, path, subfolder):
dest_key = "/".join(
[
subfolder,
(
path.split("/")[-1]
if "/" in path
else path.split("\\")[-1]
),
]
)
loguru.logger.info(f"COS UPLOAD {path} to {os.path.join(cos_bucket, subfolder)}")
for i in range(0, 10):
try:
with open(
@@ -115,39 +115,12 @@ class COSUpload:
client = CosS3Client(config)
response = client.upload_file(
Bucket=cos_bucket,
Key="/".join(
[
yaml_config["subfolder"],
(
path.split("/")[-1]
if "/" in path
else path.split("\\")[-1]
),
]
),
Key=dest_key,
LocalFilePath=path,
)
break
except CosClientError or CosServiceError as e:
raise RuntimeError("上传失败")
data = {"prompt_id": "",
"video_url": "https://{}.cos.{}.myqcloud.com/{}".format(cos_bucket, yaml_config['region'],
'/'.join([yaml_config['subfolder'],
path.split('/')[
-1] if '/' in path else
path.split('\\')[-1], ]))
}
headers = {'Content-Type': 'application/json'}
try:
req = urllib.request.Request("", data=json.dumps(data).encode("utf-8"), headers=headers)
response = urllib.request.urlopen(req)
except:
raise RuntimeError("上报MQ状态失败")
raise Exception(f"COS上传失败 bucket {cos_bucket}; local_path {path}; subfolder {subfolder}")
return (
"/".join(
[
yaml_config["subfolder"],
path.split("/")[-1] if "/" in path else path.split("\\")[-1],
]
),
dest_key,
)