From 341f81333b007cce172d04f9baab8dfaea55c66d Mon Sep 17 00:00:00 2001 From: xsl Date: Wed, 8 Oct 2025 00:16:50 +0800 Subject: [PATCH] save code --- basic_api_example.py | 69 ++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/basic_api_example.py b/basic_api_example.py index cf7f326..842dbc2 100644 --- a/basic_api_example.py +++ b/basic_api_example.py @@ -1,42 +1,41 @@ -import json -from urllib import request +# -*- coding=utf-8 +from qcloud_cos import CosConfig +from qcloud_cos import CosS3Client +import sys +import os +import logging -#This is the ComfyUI api prompt format. +# 正常情况日志级别使用 INFO,需要定位时可以修改为 DEBUG,此时 SDK 会打印和服务端的通信信息 +logging.basicConfig(level=logging.INFO, stream=sys.stdout) -#If you want it for a specific workflow you can "enable dev mode options" -#in the settings of the UI (gear beside the "Queue Size: ") this will enable -#a button on the UI to save workflows in api format. - -#keep in mind ComfyUI is pre alpha software so this format will change a bit. - -#this is the one for the default workflow - -def queue_prompt(prompt): - p = {"prompt": prompt} - - # If the workflow contains API nodes, you can add a Comfy API key to the `extra_data`` field of the payload. - # p["extra_data"] = { - # "api_key_comfy_org": "comfyui-87d01e28d*******************************************************" # replace with real key - # } - # See: https://docs.comfy.org/tutorials/api-nodes/overview - # Generate a key here: https://platform.comfy.org/login - - data = json.dumps(p).encode('utf-8') - req = request.Request("http://127.0.0.1:8188/prompt", data=data) - request.urlopen(req) +# 1. 设置用户属性, 包括 secret_id, secret_key, region等。Appid 已在 CosConfig 中移除,请在参数 Bucket 中带上 Appid。Bucket 由 BucketName-Appid 组成 +secret_id = 'AKIDDIBrNhoiOtBF5rcEdoBRUKwU2Rj2OgxA' # 用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140 +secret_key = '5wkKhttgYQD2iEZgaInEVYVPCo2BJK9l' # 用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140 +region = 'ap-beijing' #'ap-guangzhou' # 替换为用户的 region,已创建桶归属的 region 可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket + # COS 支持的所有 region 列表参见 https://cloud.tencent.com/document/product/436/6224 +token = None # 如果使用永久密钥不需要填入 token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见 https://cloud.tencent.com/document/product/436/14048 +scheme = 'https' # 指定使用 http/https 协议来访问 COS,默认为 https,可不填 -with open('/home/xsl/code/ComfyUI/change_cloth/basic_api.json', 'r', encoding='utf-8') as file: - prompt_text = file.read() +config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme) +client = CosS3Client(config) -prompt = json.loads(prompt_text) -#set the text prompt for our positive CLIPTextEncode -prompt["6"]["inputs"]["text"] = "masterpiece best quality man" - -#set the seed for our KSampler node -prompt["3"]["inputs"]["seed"] = 5 - - -queue_prompt(prompt) +#### 文件流简单上传(不支持超过5G的文件,推荐使用下方高级上传接口) +# 强烈建议您以二进制模式(binary mode)打开文件,否则可能会导致错误 +with open('out_image.png', 'rb') as fp: + response = client.put_object( + Bucket='b-1304254135', + Body=fp, + Key='out_image.png', + StorageClass='STANDARD', + EnableMD5=False + ) +print(response['ETag']) +#### 获取文件到本地 +response = client.get_object( + Bucket='b-1304254135', + Key='out_image.png' +) +response['Body'].get_stream_to_file('cos_girl.png')