You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
import aiohttp import asyncio import json from aiohttp import FormData from loguru import logger
from config import UPLOAD_URL
async def upload_file(file_path, url=UPLOAD_URL): """
异步上传 :param file_path: :param url: :return: """
if file_path: async with aiohttp.ClientSession() as session: data = FormData() data.add_field("file",open(file_path, 'rb'),content_type='multipart/form-data;charset=utf-8"') data.add_field("output", "json") async with session.post(url, data=data) as response:
result = await response.text() # 返回结果为json字符串 result = json.loads(result) # logger.info(f"upload file {result}") if 'src' in result.keys(): video_path = result['src'] return video_path
async def download_file(url, filename): """
下载发消息的资源文件 :param url: :return: """
async with aiohttp.ClientSession() as session: logger.info(f"Starting download file from {url}") async with session.get(url) as response: assert response.status == 200 with open(filename, "wb") as f: while True: chunk = await response.content.readany() if not chunk: break f.write(chunk) logger.info(f"Downloaded {filename} from {url}") return filename
if __name__ == '__main__': loop = asyncio.get_event_loop() # loop.run_until_complete(upload_file("resources/photo_2024-01-17_02-05-00.jpg")) loop.run_until_complete(download_file("http://172.18.1.180:9980/group17/default/20240507/17/07/3/image.jpg", "../resources/image.jpg"))
|