44 lines
1.7 KiB
Python
44 lines
1.7 KiB
Python
|
import os
|
||
|
import requests
|
||
|
|
||
|
# 配置信息
|
||
|
local_image_folder = 'downloaded_images'
|
||
|
lsky_pro_url = 'http://192.168.107.248:18089/api/v1'
|
||
|
upload_endpoint = '/upload'
|
||
|
token = '1|QJP2YEr9GIN52VBgmm5hCqV5DwBSvJLUKjnwcKB8'
|
||
|
|
||
|
def upload_image_to_lsky_pro(image_path):
|
||
|
with open(image_path, 'rb') as img_file:
|
||
|
files = {'file': img_file}
|
||
|
headers = {
|
||
|
'Authorization': f'Bearer {token}',
|
||
|
'Accept': 'application/json'
|
||
|
}
|
||
|
response = requests.post(f'{lsky_pro_url}{upload_endpoint}', files=files, headers=headers)
|
||
|
if response.status_code == 200:
|
||
|
try:
|
||
|
data = response.json()
|
||
|
if data['status']:
|
||
|
return data['data']['links']['url']
|
||
|
else:
|
||
|
print(f"Failed to upload {image_path}: {data['message']}")
|
||
|
return None
|
||
|
except requests.exceptions.JSONDecodeError as e:
|
||
|
print(f"Failed to decode JSON response for {image_path}: {e}")
|
||
|
print(f"Response content: {response.text}")
|
||
|
return None
|
||
|
else:
|
||
|
print(f"Failed to upload {image_path}: {response.status_code} - {response.text}")
|
||
|
return None
|
||
|
|
||
|
# 示例:上传本地图片并获取图床中的图片地址
|
||
|
if __name__ == "__main__":
|
||
|
# 假设你有一个本地图片路径
|
||
|
local_image_path = os.path.join(local_image_folder, '20220406100638201_217862.gif')
|
||
|
|
||
|
# 上传图片并获取图床中的图片地址
|
||
|
uploaded_url = upload_image_to_lsky_pro(local_image_path)
|
||
|
if uploaded_url:
|
||
|
print(f"Uploaded image URL: {uploaded_url}")
|
||
|
else:
|
||
|
print("Failed to upload image.")
|