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/xsl/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}")
