mirror of
https://github.com/BlueSkyXN/WorkerJS_CloudFlare_ImageBed.git
synced 2024-11-16 03:32:26 +08:00
31 lines
986 B
Python
31 lines
986 B
Python
|
import requests
|
|||
|
|
|||
|
# 上传图片接口的URL
|
|||
|
upload_url = "https://openai.weixin.qq.com/weixinh5/webapp/h774yvzC2xlB4bIgGfX2stc4kvC85J/cos/upload"
|
|||
|
|
|||
|
# 图片路径
|
|||
|
image_path = r"F:\Download\ecabfc9aa7dace978229f8563ab5c856fe530eea.jpg"
|
|||
|
|
|||
|
# 准备上传的文件,模拟 PHP 中的 `CURLFile`,用 `files` 参数传递文件
|
|||
|
files = {
|
|||
|
'media': ('ecabfc9aa7dace978229f8563ab5c856fe530eea.jpg', open(image_path, 'rb'), 'image/jpeg')
|
|||
|
}
|
|||
|
|
|||
|
# 发送POST请求,上传图片
|
|||
|
try:
|
|||
|
response = requests.post(upload_url, files=files)
|
|||
|
response.raise_for_status() # 检查请求是否成功
|
|||
|
|
|||
|
# 解析响应数据
|
|||
|
response_data = response.json()
|
|||
|
if 'url' in response_data:
|
|||
|
print("图片上传成功,访问URL为:", response_data['url'])
|
|||
|
else:
|
|||
|
print("上传失败,服务器未返回URL:", response_data)
|
|||
|
|
|||
|
except requests.exceptions.RequestException as e:
|
|||
|
print(f"图片上传失败: {e}")
|
|||
|
finally:
|
|||
|
# 关闭文件
|
|||
|
files['media'][1].close()
|