change env vars, to nodes for s3 config

This commit is contained in:
Inoland
2025-09-30 16:52:35 +04:00
parent f10daeb4c6
commit fd9b0ef124
7 changed files with 84 additions and 58 deletions
+3 -3
View File
@@ -29,9 +29,9 @@ from .src.comfyui_ino_nodes.utils.sampler_helper import InoLoadSamplerModels, In
from .src.comfyui_ino_nodes.node_cloudreve import CloudreveInit, CloudreveSignin, CloudreveUploadFile
from .src.comfyui_ino_nodes.s3_helper.s3_helper import InoS3Config
from .src.comfyui_ino_nodes.s3_helper.s3_upload_file_node import InoS3UploadFile
from .src.comfyui_ino_nodes.s3_helper.s3_upload_image_node import InoS3UploadImage
from .src.comfyui_ino_nodes.s3_helper.s3_upload_video_node import InoS3UploadVideo
from .src.comfyui_ino_nodes.s3_helper.s3_download_file_node import InoS3DownloadFile
from .src.comfyui_ino_nodes.s3_helper.s3_download_image_node import InoS3DownloadImage
@@ -71,9 +71,9 @@ NODE_CLASS_MAPPINGS = {
"CloudreveSignin": CloudreveSignin,
"CloudreveUploadFile": CloudreveUploadFile,
"InoS3Config": InoS3Config,
"InoS3UploadFile": InoS3UploadFile,
"InoS3UploadImage": InoS3UploadImage,
"InoS3UploadVideo": InoS3UploadVideo,
"InoS3DownloadFile": InoS3DownloadFile,
"InoS3DownloadImage": InoS3DownloadImage,
}
@@ -115,9 +115,9 @@ NODE_DISPLAY_NAME_MAPPINGS = {
"CloudreveSignin": "Cloudreve Signin",
"CloudreveUploadFile": "Cloudreve Upload File",
"InoS3Config": "Ino S3 Config",
"InoS3UploadFile": "Ino S3 Upload File",
"InoS3UploadImage": "Ino S3 Upload Image",
"InoS3UploadVideo": "Ino S3 Upload Video",
"InoS3DownloadFile": "Ino S3 Download File",
"InoS3DownloadImage": "Ino S3 Download Image",
}
@@ -1,37 +0,0 @@
import os
from pathlib import Path
from inopyutils import InoS3Helper
from dotenv import load_dotenv
load_dotenv()
def get_save_path(s3_key: str, save_path: str):
save_path_obj = Path(save_path)
s3_key_obj = Path(s3_key)
if save_path.endswith('/') or save_path.endswith('\\'):
local_file_path = save_path_obj / s3_key_obj.name
elif save_path_obj.suffix:
filename_without_ext = save_path_obj.stem
s3_extension = s3_key_obj.suffix
local_file_path = save_path_obj.parent / (filename_without_ext + s3_extension)
else:
s3_extension = s3_key_obj.suffix
local_file_path = Path(save_path + s3_extension)
return local_file_path
def get_s3_instance():
try:
s3_instance = InoS3Helper(
aws_access_key_id=os.getenv("S3_ACCESS_KEY"),
aws_secret_access_key=os.getenv("S3_SECRET_KEY"),
endpoint_url=os.getenv("S3_ENDPOINT_URL"),
region_name = os.getenv("S3_REGION_NAME"),
bucket_name=os.getenv("S3_BUCKET_NAME"),
)
return s3_instance
except Exception as e:
err = f"Failed to create S3 instance: {e} Please check your environment variables."
print(err)
#logger.error(err)
@@ -1,13 +1,13 @@
from pathlib import Path
from .s3_client import get_s3_instance, get_save_path
S3_INSTANCE = get_s3_instance()
from .s3_helper import get_s3_instance, get_save_path
class InoS3DownloadFile:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"s3_config": ("STRING", {"default": ""}),
"s3_key": ("STRING", {"default": "input/example.png"}),
"save_path": ("STRING", {"default": "input/"}),
}
@@ -18,11 +18,12 @@ class InoS3DownloadFile:
RETURN_NAMES = ("success", "msg", "result", "rel_path", "abs_path", )
FUNCTION = "function"
async def function(self, s3_key, save_path):
async def function(self, s3_key, save_path, s3_config):
rel_path = get_save_path(s3_key, save_path)
abs_path = rel_path.resolve()
downloaded = await S3_INSTANCE.download_file(
s3_instance = get_s3_instance(s3_config)
downloaded = await s3_instance.download_file(
s3_key=s3_key,
local_file_path=str(rel_path)
)
@@ -3,15 +3,14 @@ import torch
import numpy as np
from PIL import Image, ImageOps, ImageSequence
from .s3_client import get_s3_instance, get_save_path
S3_INSTANCE = get_s3_instance()
from .s3_helper import get_s3_instance, get_save_path
class InoS3DownloadImage:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"s3_config": ("STRING", {"default": ""}),
"s3_key": ("STRING", {"default": "input/example.png"}),
"save_locally": ("BOOLEAN", {"default": True}),
"save_path": ("STRING", {"default": "input/example.png"})
@@ -23,10 +22,12 @@ class InoS3DownloadImage:
RETURN_NAMES = ("success", "msg", "result", "image", "mask", )
FUNCTION = "function"
async def function(self, s3_key, save_locally, save_path):
async def function(self, s3_key, save_locally, save_path, s3_config):
rel_path = get_save_path(s3_key, save_path)
abs_path = rel_path.resolve()
downloaded = await S3_INSTANCE.download_file(
s3_instance = get_s3_instance(s3_config)
downloaded = await s3_instance.download_file(
s3_key=s3_key,
local_file_path=str(rel_path)
)
@@ -0,0 +1,59 @@
from pathlib import Path
from inopyutils import InoS3Helper
def get_save_path(s3_key: str, save_path: str):
save_path_obj = Path(save_path)
s3_key_obj = Path(s3_key)
if save_path.endswith('/') or save_path.endswith('\\'):
local_file_path = save_path_obj / s3_key_obj.name
elif save_path_obj.suffix:
filename_without_ext = save_path_obj.stem
s3_extension = s3_key_obj.suffix
local_file_path = save_path_obj.parent / (filename_without_ext + s3_extension)
else:
s3_extension = s3_key_obj.suffix
local_file_path = Path(save_path + s3_extension)
return local_file_path
def get_s3_instance(s3_config):
try:
s3_instance = InoS3Helper(
aws_access_key_id=s3_config["access_key_id"],
aws_secret_access_key=s3_config["secret_access_key"],
endpoint_url=s3_config["endpoint_url"],
region_name = s3_config["region_name"],
bucket_name=s3_config["bucket_name"],
)
return s3_instance
except Exception as e:
print(f"Failed to create S3 instance: {e} Please check your environment variables.")
class InoS3Config:
@classmethod
def INPUT_TYPES(s):
return {
"required":{
"access_key_id": ("STRING", {"default": ""}),
"secret_access_key": ("STRING", {"default": ""}),
"endpoint_url": ("STRING", {"default": ""}),
"region_name": ("STRING", {"default": ""}),
"bucket_name": ("STRING", {"default": ""}),
}
}
CATEGORY = "InoS3Helper"
RETURN_TYPES = ("BOOLEAN", "STRING", )
RETURN_NAMES = ("success", "config", )
FUNCTION = "function"
async def function(self, access_key_id, secret_access_key, endpoint_url, region_name, bucket_name):
s3_config ={
"access_key_id": access_key_id,
"secret_access_key": secret_access_key,
"endpoint_url": endpoint_url,
"region_name": region_name,
"bucket_name": bucket_name,
}
return (True, s3_config, )
@@ -1,13 +1,12 @@
import os
from .s3_client import get_s3_instance
S3_INSTANCE = get_s3_instance()
from .s3_helper import get_s3_instance, get_save_path
class InoS3UploadFile:
@classmethod
def INPUT_TYPES(s):
return {
"required":{
"s3_config": ("STRING", {"default": ""}),
"s3_key": ("STRING", {"default": ""}),
"local_path": ("STRING", {"default": "input/example.png"}),
"delete_local": ("BOOLEAN", {"default": True}),
@@ -19,8 +18,9 @@ class InoS3UploadFile:
RETURN_NAMES = ("success", "msg", "result", )
FUNCTION = "function"
async def function(self, s3_key, local_path, delete_local):
uploaded = await S3_INSTANCE.upload_file(
async def function(self, s3_key, local_path, delete_local, s3_config):
s3_instance = get_s3_instance(s3_config)
uploaded = await s3_instance.upload_file(
s3_key=s3_key,
local_file_path=local_path
)
@@ -7,8 +7,8 @@ from PIL import Image
from PIL.PngImagePlugin import PngInfo
from comfy.cli_args import args
from .s3_client import get_s3_instance
S3_INSTANCE = get_s3_instance()
from .s3_helper import get_s3_instance, get_save_path
class InoS3UploadImage:
@classmethod
@@ -16,6 +16,7 @@ class InoS3UploadImage:
return {
"required": {
"images": ("IMAGE",),
"s3_config": ("STRING", {"default": ""}),
"s3_key": ("STRING", {"default": ""}),
"file_name": ("STRING", {"default": ""})
}
@@ -26,7 +27,7 @@ class InoS3UploadImage:
FUNCTION = "function"
CATEGORY = "InoS3Helper"
async def function(self, images, s3_key, file_name):
async def function(self, images, s3_key, file_name, s3_config):
results = list()
s3_image_paths = list()
@@ -46,7 +47,8 @@ class InoS3UploadImage:
s3_path:str = f"{s3_key}/{file}"
uploaded = await S3_INSTANCE.upload_file(
s3_instance = get_s3_instance(s3_config)
uploaded = await s3_instance.upload_file(
s3_key=str(s3_path),
local_file_path=str(temp_file)
)