From 6a3c683157c824c32d4c2bd4599635c8ef984771 Mon Sep 17 00:00:00 2001 From: colomi <1421901449@qq.com> Date: Sat, 19 Jul 2025 23:40:44 +0800 Subject: [PATCH] save code --- http_app.py | 42 ++++++++++++++++++++++++++++++++++++++++-- oss_test.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 oss_test.py diff --git a/http_app.py b/http_app.py index 5c6e82b..7aefade 100644 --- a/http_app.py +++ b/http_app.py @@ -10,6 +10,7 @@ from PIL import Image from datetime import datetime from volcenginesdkarkruntime import Ark import shutil +import oss2 APP_ROOT = os.path.dirname(os.path.abspath(__file__)) STATIC_FOLDER = os.path.join(APP_ROOT, 'static') @@ -115,7 +116,7 @@ def change(human_name, cloth_name, c_width, c_height, cloth_url, is_generated): return None, "get image type error" if not is_generated: - if cloth_len == '大腿' or cloth_len == '膝盖' or '小腿': + if cloth_len == '大腿' or cloth_len == '膝盖' or cloth_len == '小腿': takeoff_file_name = takeoff_cloth_first(human_name) if takeoff_file_name == None: return None, f"takeoff_cloth_first error {human_name}" @@ -252,6 +253,38 @@ def image_to_base64(file_path, mime_type=None): # 组合成Data URI格式 return f"data:{mime_type};base64,{encoded_string}" +def upload_to_oss(image_path, object_name=None): + # 配置信息(替换为你的实际信息) + access_key_id = 'LTAI5tB9t2RH6f1drSLvVLLZ' + access_key_secret = '91uzPI1RAFHN7n3Y6TJDGFP8w0dG1R' + endpoint = 'oss-cn-beijing.aliyuncs.com' # 替换为你的Endpoint + bucket_name = 'llyz' + + # 创建Bucket实例 + auth = oss2.Auth(access_key_id, access_key_secret) + bucket = oss2.Bucket(auth, endpoint, bucket_name) + + # 如果没有指定OSS文件名,则使用本地文件名 + if object_name is None: + object_name = image_path.split('/')[-1] # 取本地文件名 + + try: + # 上传文件 + bucket.put_object_from_file(object_name, image_path) + + # 获取文件URL(有效期默认10年) + url = bucket.sign_url('GET', object_name, 3600 * 24 * 365 * 10) + + # 或者使用公共读Bucket的URL(如果Bucket是公共读权限) + # url = f"https://{bucket_name}.{endpoint}/{object_name}" + + print(f"文件上传成功,URL: {url}") + return url + except Exception as e: + print(f"上传失败: {str(e)}") + return None + + def process_change_cloth(human_filename, cloth_filename, output_format, img_url, is_generated): w,h = get_image_dimensions(f'/home/szlc/code/ComfyUI/input/{human_filename}') @@ -265,6 +298,11 @@ def process_change_cloth(human_filename, cloth_filename, output_format, img_url, jpg_path_name = f'/home/szlc/code/ComfyUI/change_cloth/static/imgs/{jpg_name}' image.save(jpg_path_name, quality=95) + + upload_to_oss(jpg_path_name, jpg_name) # 第二个参数可选,指定OSS上的路径 + https_url = f'https://llyz.oss-cn-beijing.aliyuncs.com/{jpg_name}' + print(f"生成的HTTPS URL: {https_url}") + if 'base64' in output_format: return jsonify({ "ret":0, @@ -275,7 +313,7 @@ def process_change_cloth(human_filename, cloth_filename, output_format, img_url, return jsonify({ "ret":0, "msg":"success", - "url":f"http://112.126.94.241:18888/static/imgs/{jpg_name}" + "url":https_url }) @app.route('/change_cloth_base64', methods=['POST']) diff --git a/oss_test.py b/oss_test.py new file mode 100644 index 0000000..bfb6a14 --- /dev/null +++ b/oss_test.py @@ -0,0 +1,38 @@ +import oss2 + +def upload_to_oss(image_path, object_name=None): + # 配置信息(替换为你的实际信息) + access_key_id = 'LTAI5tB9t2RH6f1drSLvVLLZ' + access_key_secret = '91uzPI1RAFHN7n3Y6TJDGFP8w0dG1R' + endpoint = 'oss-cn-beijing.aliyuncs.com' # 替换为你的Endpoint + bucket_name = 'llyz' + + # 创建Bucket实例 + auth = oss2.Auth(access_key_id, access_key_secret) + bucket = oss2.Bucket(auth, endpoint, bucket_name) + + # 如果没有指定OSS文件名,则使用本地文件名 + if object_name is None: + object_name = image_path.split('/')[-1] # 取本地文件名 + + try: + # 上传文件 + bucket.put_object_from_file(object_name, image_path) + + # 获取文件URL(有效期默认10年) + url = bucket.sign_url('GET', object_name, 3600 * 24 * 365 * 10) + + # 或者使用公共读Bucket的URL(如果Bucket是公共读权限) + # url = f"https://{bucket_name}.{endpoint}/{object_name}" + + print(f"文件上传成功,URL: {url}") + return url + except Exception as e: + print(f"上传失败: {str(e)}") + return None + +# 使用示例 +image_path = '/home/szlc/code/ComfyUI/change_cloth/girl8010.jpg' # 本地图片路径 +upload_to_oss(image_path, 'girl8011.jpg') # 第二个参数可选,指定OSS上的路径 +https_url = f'https://llyz.oss-cn-beijing.aliyuncs.com/girl8011.jpg' +print(f"生成的HTTPS URL: {https_url}")