{"id":414493,"date":"2024-06-29T23:47:43","date_gmt":"2024-06-29T23:47:43","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=414493"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=414493","title":{"rendered":"<span>\u0410\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b\u043e\u0432\u044b\u0439 api-\u0441\u0435\u0440\u0432\u0438\u0441<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0418\u0434\u0435\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u0443\u044e \u0441\u0442\u0430\u0442\u044c\u044e \u0440\u043e\u0434\u0438\u043b\u0430\u0441\u044c \u043f\u043e\u0441\u043b\u0435 \u043c\u043e\u0435\u0433\u043e \u0444\u0435\u0439\u043b\u0430 \u043f\u043e\u00a0\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430. \u0421\u0443\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438\u00a0\u0431\u044b\u043b\u0430 \u043f\u0440\u043e\u0441\u0442\u0430\u00a0\u2014 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0435\u0440\u0432\u0435\u0440 \u0441\u00a0\u0431\u0430\u0437\u043e\u0432\u044b\u043c\u0438 \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0438 \u043e\u0442\u0434\u0430\u0447\u0438 \u0444\u0430\u0439\u043b\u043e\u0432 \u0438 \u0441\u0435\u0440\u0432\u0438\u0441\u043d\u044b\u043c\u0438 \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438 \u043f\u043e\u00a0\u0441\u043f\u0435\u0446\u0438\u0444\u0438\u0447\u043d\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0444\u0430\u0439\u043b\u043e\u0432. \u041e\u0431\u043c\u0435\u043d \u0434\u0430\u043d\u043d\u044b\u043c\u0438 (\u0442\u0435\u043b\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435) \u044f \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b \u0447\u0435\u0440\u0435\u0437 json, \u043f\u0440\u043e\u00a0\u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d \u0438\u0434\u0435\u044e \u0443\u043f\u0443\u0441\u0442\u0438\u043b. \u041f\u043e\u00a0\u043d\u0430\u0447\u0430\u043b\u0443 \u0432\u0441\u0451\u00a0\u0431\u044b\u043b\u043e \u0445\u043e\u0440\u043e\u0448\u043e, \u0444\u0430\u0439\u043b\u044b \u043d\u0435\u00a0\u043f\u0440\u0435\u0432\u044b\u0448\u0430\u043b\u0438 \u0440\u0430\u0437\u043c\u0435\u0440 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043c\u0435\u0433\u0430\u0431\u0430\u0439\u0442\u043e\u0432, \u043c\u0435\u0442\u043e\u0434\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438\u0441\u044c \u0440\u0435\u0434\u043a\u043e. \u041d\u043e\u00a0\u0431\u0443\u043a\u0432\u0430\u043b\u044c\u043d\u043e \u0447\u0435\u0440\u0435\u0437 \u043f\u0430\u0440\u0443 \u043c\u0435\u0441\u044f\u0446\u0435\u0432 \u0440\u0430\u0437\u043c\u0435\u0440\u044b \u0444\u0430\u0439\u043b\u043e\u0432 \u0441\u0442\u0430\u043b\u0438 \u0438\u0437\u043c\u0435\u0440\u044f\u0442\u044c\u0441\u044f \u0434\u0435\u0441\u044f\u0442\u043a\u0430\u043c\u0438 \u043c\u0435\u0433\u0430\u0431\u0430\u0439\u0442, \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0441\u043e\u0442\u043d\u0438 \u0432\u00a0\u043c\u0438\u043d\u0443\u0442\u0443. \u0421\u0435\u0440\u0432\u0438\u0441 \u0441\u0442\u0430\u043b \u0442\u043e\u0440\u043c\u043e\u0437\u0438\u0442\u044c, \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u043b\u0438 \u043e\u0448\u0438\u0431\u043a\u0438 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e\u0433\u043e \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a\u00a0\u0444\u0430\u0439\u043b\u0430\u043c. \u00ab\u041d\u0438\u043a\u043e\u0433\u0434\u0430 \u0428\u0442\u0438\u0440\u043b\u0438\u0446 \u043d\u0435\u00a0\u0431\u044b\u043b \u0442\u0430\u043a \u0431\u043b\u0438\u0437\u043e\u043a \u043a\u00a0\u043f\u0440\u043e\u0432\u0430\u043b\u0443\u00bb. <\/p>\n<p>\u0412\u00a0\u044d\u0442\u043e\u043c \u043a\u0435\u0439\u0441\u0435 \u044f \u043f\u043e\u043a\u0430\u0436\u0443 \u043a\u0430\u043a\u00a0\u044f \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u043b \u043a\u043e\u0434 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432. <\/p>\n<p>\u0412\u00a0\u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u0431\u0443\u0434\u0443\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u044b \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <a href=\"https:\/\/docs.python.org\/3\/library\/asyncio.html\" rel=\"noopener noreferrer nofollow\">asyncio<\/a>, <a href=\"https:\/\/docs.aiohttp.org\/en\/stable\/\" rel=\"noopener noreferrer nofollow\">aiohttp<\/a> \u0434\u043b\u044f\u00a0\u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0435\u043d\u0438\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0441\u0442\u0438 \u0441\u0435\u0440\u0432\u0438\u0441\u0430.<\/p>\n<p>\u041d\u0430\u00a0\u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u043e\u0440\u0446\u0438\u044f\u043c\u0438 \u043f\u043e\u00a0HTTP \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0443\u00a0\u2014 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/Chunked_transfer_encoding\" rel=\"noopener noreferrer nofollow\">Chunked transfer encoding<\/a>. \u0417\u0430\u0447\u0435\u043c \u043e\u043d \u043d\u0443\u0436\u0435\u043d \u0438 \u043a\u0430\u043a\u00a0\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u043e \u0442\u0443\u0442\u00a0\u2014 \u0432 <a href=\"https:\/\/en.wikipedia.org\/wiki\/Chunked_transfer_encoding#Rationale\" rel=\"noopener noreferrer nofollow\">\u0430\u043d\u0433\u043b\u043e\u044f\u0437\u044b\u0447\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0412\u0438\u043a\u0438\u043f\u0435\u0434\u0438\u0438<\/a>. \u0425\u043e\u0440\u043e\u0448\u043e, \u0447\u0442\u043e\u00a0aiohttp \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0442\u0430\u043a\u043e\u0439 \u043c\u0435\u0442\u043e\u0434 \u0438\u0437 \u00ab\u043a\u043e\u0440\u043e\u0431\u043a\u0438\u00bb\u00a0\u2014 <a href=\"https:\/\/docs.aiohttp.org\/en\/stable\/web_reference.html?highlight=enable_chunked_encoding#aiohttp.web.StreamResponse.enable_chunked_encoding\" rel=\"noopener noreferrer nofollow\">StreamResponse.enable_chunked_encoding<\/a>. <\/p>\n<p>\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430\u00a0\u043f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u043d\u0438\u0437\u0443 \u0441\u0442\u0430\u0442\u044c\u0438.<\/p>\n<h2>\u041f\u0438\u0448\u0435\u043c \u0441\u043a\u0440\u0438\u043f\u0442 \u043f\u043e\u0442\u043e\u043a\u043e\u0432\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 post-\u043c\u0435\u0442\u043e\u0434\u0430 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u0430<\/h2>\n<p>\u042d\u0442\u043e\u0442 \u0441\u043a\u0440\u0438\u043f\u0442 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439, \u0435\u0433\u043e \u0437\u0430\u0434\u0430\u0447\u0430\u00a0\u2014 \u0441\u0442\u0440\u0438\u043c\u0438\u0442\u044c \u0431\u0430\u0439\u0442\u043e\u0432\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0432\u00a0\u0444\u0430\u0439\u043b\u043e\u0432\u044b\u0439 \u0441\u0435\u0440\u0432\u0438\u0441. \u041e\u0441\u043d\u043e\u0432\u0430 \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u0432\u0437\u044f\u0442\u0430 \u0438\u0437 <a href=\"https:\/\/docs.aiohttp.org\/en\/stable\/client_quickstart.html#streaming-uploads\" rel=\"noopener noreferrer nofollow\">\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/a> aiohttp, \u0432\u00a0\u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043a\u0430\u043a\u00a0\u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u0444\u0430\u0439\u043b\u044b, \u043d\u0435\u00a0\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u044f \u0438\u0445 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0432\u00a0\u043f\u0430\u043c\u044f\u0442\u044c.<\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0432\u00a0\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u044f \u0434\u043e\u0431\u0430\u0432\u0438\u043b \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e\u0431\u00a0\u0438\u043c\u0435\u043d\u0438 \u0444\u0430\u0439\u043b\u0430. \u0414\u043b\u044f\u00a0\u044d\u0442\u043e\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u043b \u0432\u00a0\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0440\u0430\u0437\u0434\u0435\u043b <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Headers\/Content-Disposition\" rel=\"noopener noreferrer nofollow\">CONTENT\u2011DISPOSITION<\/a>.<\/p>\n<p>\u0418\u0442\u043e\u0433\u043e \u0441\u043a\u0440\u0438\u043f\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a: <\/p>\n<pre><code class=\"python\">async def file_sender(file_name: str, chunk_size: int) -> Generator[bytes, None, None]:     \"\"\"     \u0413\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u0441\u0447\u0438\u0442\u044b\u0432\u0430\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u0430 \u043f\u043e \u0447\u0430\u0441\u0442\u044f\u043c             \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b:                     file_name (str): \u0438\u043c\u044f \u0444\u0430\u0439\u043b\u0430, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u043f\u0443\u0442\u044c                     chunk_size (int): \u0440\u0430\u0437\u043c\u0435\u0440 \u043f\u043e\u0440\u0446\u0438\u0438 \u0434\u043b\u044f \u0441\u0447\u0438\u0442\u044b\u0432\u0430\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u0430 \u0432 \u043f\u0430\u043c\u044f\u0442\u044c             \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435:                     chunk (bytes): \u0447\u0430\u0441\u0442\u044c \u0431\u0430\u0439\u0442\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430 \u0444\u0430\u0439\u043b\u0430     \"\"\"      async with aiofiles.open(file_name, 'rb') as f:         chunk = await f.read(chunk_size)          while chunk:             yield chunk             chunk = await f.read(chunk_size)   async def main() -> None:     \"\"\"\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 post-\u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432 \u0430\u0434\u0440\u0435\u0441 \u0444\u0430\u0439\u043b\u043e\u0432\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430\"\"\"      args = get_args()  # \u043a\u043e\u0434 \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0432 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438 \u0434\u043b\u044f \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0442      url = urljoin(f'{args.protocol}:\/\/{args.host}:{args.port}', args.url)     headers = {         'CONTENT-DISPOSITION': f'attachment;filename={os.path.basename(args.path)}',     }      async with aiohttp.ClientSession() as session:         async with session.post(                 url,                 headers=headers,                 data=file_sender(file_name=args.path, chunk_size=args.chunk_size)         ) as resp:             logger.info(await resp.text())<\/code><\/pre>\n<h2>\u0420\u0430\u0437\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c post-\u043c\u0435\u0442\u043e\u0434 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u0430<\/h2>\n<p>\u0412\u00a0\u043f\u0435\u0440\u0432\u043e\u043c \u043f\u0440\u0438\u0431\u043b\u0438\u0436\u0435\u043d\u0438\u0438 \u043a\u043e\u0434 \u0445\u0435\u043d\u0434\u043b\u0435\u0440\u0430 \u043f\u043e\u00a0\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0435 post\u2011\u043c\u0435\u0442\u043e\u0434\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"python\">from aiofile import async_open  async def save_archive(request: Request) -> web.Response:     \"\"\"\u0425\u0435\u043d\u0434\u043b\u0435\u0440 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0431\u0430\u0439\u0442\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430 \u0438\u0437 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432 \u0444\u0430\u0439\u043b\"\"\"      async with async_open(file_path, 'bw') as afp:         # https:\/\/docs.aiohttp.org\/en\/stable\/streams.html#asynchronous-iteration-support         # \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044e \u043f\u043e \u0431\u043b\u043e\u043a\u0430\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0438\u0445 \u0432\u0432\u043e\u0434\u0430 \u0432 \u043f\u043e\u0442\u043e\u043a         async for data in request.content.iter_any():             await afp.write(data)      return web.Response(text='file accepted')<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u044f \u043d\u0430\u0443\u0447\u0438\u043b \u0445\u044d\u043d\u0434\u043b\u0435\u0440 \u0434\u043e\u0441\u0442\u0430\u0432\u0430\u0442\u044c \u0438\u0437\u00a0\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0438\u043c\u044f \u0444\u0430\u0439\u043b\u0430 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0432\u00a0\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445. \u0422\u0430\u0431\u043b\u0438\u0446\u0430 \u0438\u043c\u0435\u0435\u0442 \u0434\u0432\u0430 \u043f\u043e\u043b\u044f\u00a0\u2014 \u0430\u0439\u0434\u0438\u0448\u043d\u0438\u043a \u0438 \u0438\u043c\u044f \u0444\u0430\u0439\u043b\u0430, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0438\u0437 <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Headers\/Content-Disposition\" rel=\"noopener noreferrer nofollow\">CONTENT\u2011DISPOSITION<\/a>.<\/p>\n<p>\u0418\u0442\u043e\u0433\u043e\u0432\u044b\u0439 \u043a\u043e\u0434 \u0445\u0435\u043d\u0434\u043b\u0435\u0440\u0430:<\/p>\n<pre><code class=\"python\">async def save_file(request: Request) -> web.Response:     \"\"\"     \u0425\u0435\u043d\u0434\u043b\u0435\u0440 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0431\u0430\u0439\u0442\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430 \u0438\u0437 http-\u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432 \u0444\u0430\u0439\u043b             \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b:                     request (aiohttp.web_request): \u043e\u0431\u044a\u0435\u043a\u0442 http-\u0437\u0430\u043f\u0440\u043e\u0441\u0430             \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435:                     response (aiohttp.Response): \u043e\u0431\u044a\u0435\u043a\u0442 \u043e\u0442\u0432\u0435\u0442\u0430     \"\"\"      _, params = cgi.parse_header(request.headers['CONTENT-DISPOSITION'])     file_name = params['filename']     file_id = str(uuid.uuid4())     file_path = os.path.join(app['folder'], file_id)      async with async_open(file_path, 'bw') as afp:         # https:\/\/docs.aiohttp.org\/en\/stable\/streams.html#asynchronous-iteration-support         # \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044e \u043f\u043e \u0431\u043b\u043e\u043a\u0430\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0438\u0445 \u0432\u0432\u043e\u0434\u0430 \u0432 \u043f\u043e\u0442\u043e\u043a         async for data in request.content.iter_any():             await afp.write(data)      await logger.debug(f'\u0424\u0430\u0439\u043b \u043f\u0440\u0438\u043d\u044f\u0442 {file_name} \u0438 \u0437\u0430\u043f\u0438\u0441\u0430\u043d \u043d\u0430 \u0434\u0438\u0441\u043a')      async with engine.begin() as conn:         response = await conn.execute(files.insert().values(id=file_id, name=file_name))         await logger.debug(f'\u0424\u0430\u0439\u043b \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d \u043f\u043e\u0434 id={response.inserted_primary_key[0]}')      return web.Response(status=201, reason='OK', text=response.inserted_primary_key[0])<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0444\u0430\u0439\u043b \u043c\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u043f\u043e\u0434\u00a0\u0438\u043c\u0435\u043d\u0435\u043c \u0430\u0439\u0434\u0438\u0448\u043d\u0438\u043a\u0430. \u0412\u00a0\u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u044d\u0442\u043e <a href=\"https:\/\/docs.python.org\/3\/library\/uuid.html#uuid.uuid4\" rel=\"noopener noreferrer nofollow\">UUID4<\/a>. \u0414\u0430\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u0435\u043c \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u043b \u043c\u043d\u0435 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0441\u00a0\u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435\u043c \u0444\u0430\u0439\u043b\u043e\u0432 \u0441\u00a0\u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u043c \u0438\u043c\u0435\u043d\u0435\u043c. <\/p>\n<p>\u0414\u043b\u044f\u00a0\u0440\u0430\u0431\u043e\u0442\u044b \u0441\u00a0\u0411\u0414 \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b <a href=\"https:\/\/www.sqlalchemy.org\/\" rel=\"noopener noreferrer nofollow\">sqlalchemy<\/a>. \u041a\u043e\u0434 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a\u00a0\u0411\u0414 \u043d\u0438\u0436\u0435:<\/p>\n<pre><code class=\"python\">metadata = sqlalchemy.MetaData() engine = create_async_engine(os.environ[\"FILE_SERVICE_DATABASE_URL\"], echo=True) files = sqlalchemy.Table(     \"file\",     metadata,     sqlalchemy.Column(\"id\", sqlalchemy.String(38), primary_key=True),     sqlalchemy.Column(\"name\", sqlalchemy.String(255)), )   async def init_db():     \"\"\"     C\u043e\u0437\u0434\u0430\u0435\u0442 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043e \u0444\u0430\u0439\u043b\u0430\u0445             \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b:             \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435:                     None     \"\"\"      async with engine.begin() as conn:         await conn.run_sync(metadata.create_all)<\/code><\/pre>\n<h2>\u0420\u0430\u0437\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c get-\u043c\u0435\u0442\u043e\u0434 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u0430<\/h2>\n<p>\u0423\u00a0\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439\u00a0\u0431\u044b\u0432\u0430\u0435\u0442 \u0440\u0430\u0437\u043d\u0430\u044f \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0441\u0435\u0442\u0435\u0432\u043e\u0433\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043e\u0442\u0434\u0430\u0447\u0438 \u0444\u0430\u0439\u043b\u0430 \u044f \u0434\u0435\u043b\u0438\u043b \u043d\u0430\u00a0\u0447\u0430\u0441\u0442\u0438. \u042d\u0442\u043e \u043e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0438\u0434\u0435\u044f \u0432\u00a0\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0435 \u0444\u0430\u0439\u043b\u043e\u0432\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430. \u0412\u0441\u0435 \u0431\u0443\u0434\u0443\u0442 \u0432\u00a0\u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u00ab\u0432\u044b\u0438\u0433\u0440\u0430\u043b\u00a0\u2014 \u0432\u044b\u0438\u0433\u0440\u0430\u043b\u00bb: \u043a\u043b\u0438\u0435\u043d\u0442 \u043d\u0430\u0447\u043d\u0435\u0442 \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u0442\u044c \u0444\u0430\u0439\u043b \u0441\u0440\u0430\u0437\u0443, \u0430\u00a0\u043d\u0430\u043c \u043d\u0435\u00a0\u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432\u00a0\u043f\u0430\u043c\u044f\u0442\u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0444\u0430\u0439\u043b \u0446\u0435\u043b\u0438\u043a\u043e\u043c: <\/p>\n<pre><code class=\"python\">async def get_file(request: Request) -> web.StreamResponse:     \"\"\"     \u0425\u0435\u043d\u0434\u043b\u0435\u0440 \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0430\u0440\u0445\u0438\u0432\u0430 \u0438 \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u043d\u0438\u044f \u0435\u0433\u043e \u0432 \u0444\u0430\u0439\u043b             \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b:                     request (aiohttp.web_request): \u043e\u0431\u044a\u0435\u043a\u0442 http-\u0437\u0430\u043f\u0440\u043e\u0441\u0430             \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435:                     response (aiohttp.StreamResponse): \u043e\u0431\u044a\u0435\u043a\u0442 \u043e\u0442\u0432\u0435\u0442\u0430 \u0432 \u0432\u0438\u0434\u0435 \u0431\u0430\u0439\u0442\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430     \"\"\"      file_id = request.match_info['id']     folder_path = os.path.join(os.getcwd(), app['folder'])      if not (os.path.exists(folder_path) and os.path.isdir(folder_path)):         await logger.warning(f'\u0417\u0430\u043f\u0440\u043e\u0448\u0435\u043d\u0430 \u043d\u0435\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0430\u044f \u043f\u0430\u043f\u043a\u0430 {folder_path}')         raise web.HTTPNotFound(text='\u0410\u0440\u0445\u0438\u0432 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0438\u043b\u0438 \u0431\u044b\u043b \u0443\u0434\u0430\u043b\u0435\u043d')      async with engine.connect() as conn:         statement = select(files.c.id, files.c.name).where(files.c.id == file_id)          file_rows = await conn.execute(statement)         file = file_rows.fetchone()          if file is None:             raise web.HTTPNotFound(text='\u0424\u0430\u0439\u043b\u0430 \u043f\u043e \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u043c\u0443 id \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442')         file_path = os.path.join(app['folder'], file_id)      response = web.StreamResponse(         status=200,         reason='OK',         headers={             'Content-Type': 'multipart\/x-mixed-replace',             'CONTENT-DISPOSITION': f'attachment;filename={file.name}'         }     )      # \u041e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0443 HTTP \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0438     await response.prepare(request)      try:         async with async_open(file_path, 'rb') as f:             chunk = await f.read(app['chunk_size'])              while chunk:                 await response.write(chunk)                 chunk = await f.read(app['chunk_size'])      except asyncio.CancelledError:         await logger.error(\"Download was interrupted \")          # \u043e\u0442\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0447\u0435\u043d\u043d\u044b\u0439 CancelledError         raise      return response<\/code><\/pre>\n<h2>\u0418\u0442\u043e\u0433\u043e<\/h2>\n<p>\u042f \u0441\u043e\u0437\u0434\u0430\u043b \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b\u043e\u0432\u044b\u0439 \u0441\u0435\u0440\u0432\u0438\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u043c\u043e\u0433 \u0440\u0430\u0437\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u041e\u0417\u0423 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0438 \u043f\u0440\u0438\u00a0\u044d\u0442\u043e\u043c\u00a0\u0431\u044b\u043b \u0441\u043f\u043e\u0441\u043e\u0431\u0435\u043d \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0432\u00a0HL\u2011\u0440\u0435\u0436\u0438\u043c\u0435, \u0447\u0442\u043e\u00a0\u0432\u0430\u0436\u043d\u043e \u0434\u043b\u044f\u00a0\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432.<\/p>\n<p>\u0420\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0444\u0430\u0439\u043b\u043e\u0432\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0434\u043b\u044f\u00a0\u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u043f\u043e <a href=\"https:\/\/github.com\/s-klimov\/async-file-service.git\" rel=\"noopener noreferrer nofollow\">\u0441\u0441\u044b\u043b\u043a\u0435<\/a>. \u0412 <a href=\"https:\/\/github.com\/s-klimov\/async-file-service\/tree\/master#readme\" rel=\"noopener noreferrer nofollow\">README.md<\/a> \u0432\u044b \u043d\u0430\u0439\u0434\u0435\u0442\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e \u043f\u043e\u00a0\u0437\u0430\u043f\u0443\u0441\u043a\u0443 \u043f\u0440\u043e\u0435\u043a\u0442\u0430.<\/p>\n<p>\u0424\u0438\u0448\u043a\u0430 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432\u00a0\u0442\u043e\u043c, \u0447\u0442\u043e\u00a0\u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0444\u0430\u0439\u043b \u0432\u044b \u0441\u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0432 \u0441\u0441\u044b\u043b\u043a\u0443 \u0432\u00a0\u0430\u0434\u0440\u0435\u0441\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0430. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 <a href=\"http:\/\/localhost:8080\/files\/ff6b51b5-4030-48ed-aa5a-1b8aba37d1c4\/\" rel=\"noopener noreferrer nofollow\">http:\/\/localhost:8080\/files\/ff6b51b5\u20134030\u201348ed\u2011aa5a-1b8aba37d1c4\/<\/a>. \u0410\u00a0\u044d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e\u00a0\u0434\u043b\u044f\u00a0\u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u0430 \u0438\u0437\u00a0\u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0432\u0430\u043c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0441\u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0443\u0440\u043b \u0432\u00a0\u0448\u0430\u0431\u043b\u043e\u043d\u0435 web\u2011\u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/729340\/\"> https:\/\/habr.com\/ru\/articles\/729340\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0418\u0434\u0435\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u0443\u044e \u0441\u0442\u0430\u0442\u044c\u044e \u0440\u043e\u0434\u0438\u043b\u0430\u0441\u044c \u043f\u043e\u0441\u043b\u0435 \u043c\u043e\u0435\u0433\u043e \u0444\u0435\u0439\u043b\u0430 \u043f\u043e\u00a0\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430. \u0421\u0443\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438\u00a0\u0431\u044b\u043b\u0430 \u043f\u0440\u043e\u0441\u0442\u0430\u00a0\u2014 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0435\u0440\u0432\u0435\u0440 \u0441\u00a0\u0431\u0430\u0437\u043e\u0432\u044b\u043c\u0438 \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0438 \u043e\u0442\u0434\u0430\u0447\u0438 \u0444\u0430\u0439\u043b\u043e\u0432 \u0438 \u0441\u0435\u0440\u0432\u0438\u0441\u043d\u044b\u043c\u0438 \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438 \u043f\u043e\u00a0\u0441\u043f\u0435\u0446\u0438\u0444\u0438\u0447\u043d\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0444\u0430\u0439\u043b\u043e\u0432. \u041e\u0431\u043c\u0435\u043d \u0434\u0430\u043d\u043d\u044b\u043c\u0438 (\u0442\u0435\u043b\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435) \u044f \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b \u0447\u0435\u0440\u0435\u0437 json, \u043f\u0440\u043e\u00a0\u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d \u0438\u0434\u0435\u044e \u0443\u043f\u0443\u0441\u0442\u0438\u043b. \u041f\u043e\u00a0\u043d\u0430\u0447\u0430\u043b\u0443 \u0432\u0441\u0451\u00a0\u0431\u044b\u043b\u043e \u0445\u043e\u0440\u043e\u0448\u043e, \u0444\u0430\u0439\u043b\u044b \u043d\u0435\u00a0\u043f\u0440\u0435\u0432\u044b\u0448\u0430\u043b\u0438 \u0440\u0430\u0437\u043c\u0435\u0440 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043c\u0435\u0433\u0430\u0431\u0430\u0439\u0442\u043e\u0432, \u043c\u0435\u0442\u043e\u0434\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438\u0441\u044c \u0440\u0435\u0434\u043a\u043e. \u041d\u043e\u00a0\u0431\u0443\u043a\u0432\u0430\u043b\u044c\u043d\u043e \u0447\u0435\u0440\u0435\u0437 \u043f\u0430\u0440\u0443 \u043c\u0435\u0441\u044f\u0446\u0435\u0432 \u0440\u0430\u0437\u043c\u0435\u0440\u044b \u0444\u0430\u0439\u043b\u043e\u0432 \u0441\u0442\u0430\u043b\u0438 \u0438\u0437\u043c\u0435\u0440\u044f\u0442\u044c\u0441\u044f \u0434\u0435\u0441\u044f\u0442\u043a\u0430\u043c\u0438 \u043c\u0435\u0433\u0430\u0431\u0430\u0439\u0442, \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0441\u043e\u0442\u043d\u0438 \u0432\u00a0\u043c\u0438\u043d\u0443\u0442\u0443. \u0421\u0435\u0440\u0432\u0438\u0441 \u0441\u0442\u0430\u043b \u0442\u043e\u0440\u043c\u043e\u0437\u0438\u0442\u044c, \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u043b\u0438 \u043e\u0448\u0438\u0431\u043a\u0438 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e\u0433\u043e \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a\u00a0\u0444\u0430\u0439\u043b\u0430\u043c. \u00ab\u041d\u0438\u043a\u043e\u0433\u0434\u0430 \u0428\u0442\u0438\u0440\u043b\u0438\u0446 \u043d\u0435\u00a0\u0431\u044b\u043b \u0442\u0430\u043a \u0431\u043b\u0438\u0437\u043e\u043a \u043a\u00a0\u043f\u0440\u043e\u0432\u0430\u043b\u0443\u00bb. <\/p>\n<p>\u0412\u00a0\u044d\u0442\u043e\u043c \u043a\u0435\u0439\u0441\u0435 \u044f \u043f\u043e\u043a\u0430\u0436\u0443 \u043a\u0430\u043a\u00a0\u044f \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u043b \u043a\u043e\u0434 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432. <\/p>\n<p>\u0412\u00a0\u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u0431\u0443\u0434\u0443\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u044b \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <a href=\"https:\/\/docs.python.org\/3\/library\/asyncio.html\" rel=\"noopener noreferrer nofollow\">asyncio<\/a>, <a href=\"https:\/\/docs.aiohttp.org\/en\/stable\/\" rel=\"noopener noreferrer nofollow\">aiohttp<\/a> \u0434\u043b\u044f\u00a0\u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0435\u043d\u0438\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0441\u0442\u0438 \u0441\u0435\u0440\u0432\u0438\u0441\u0430.<\/p>\n<p>\u041d\u0430\u00a0\u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u043e\u0440\u0446\u0438\u044f\u043c\u0438 \u043f\u043e\u00a0HTTP \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0443\u00a0\u2014 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/Chunked_transfer_encoding\" rel=\"noopener noreferrer nofollow\">Chunked transfer encoding<\/a>. \u0417\u0430\u0447\u0435\u043c \u043e\u043d \u043d\u0443\u0436\u0435\u043d \u0438 \u043a\u0430\u043a\u00a0\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u043e \u0442\u0443\u0442\u00a0\u2014 \u0432 <a href=\"https:\/\/en.wikipedia.org\/wiki\/Chunked_transfer_encoding#Rationale\" rel=\"noopener noreferrer nofollow\">\u0430\u043d\u0433\u043b\u043e\u044f\u0437\u044b\u0447\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0412\u0438\u043a\u0438\u043f\u0435\u0434\u0438\u0438<\/a>. \u0425\u043e\u0440\u043e\u0448\u043e, \u0447\u0442\u043e\u00a0aiohttp \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0442\u0430\u043a\u043e\u0439 \u043c\u0435\u0442\u043e\u0434 \u0438\u0437 \u00ab\u043a\u043e\u0440\u043e\u0431\u043a\u0438\u00bb\u00a0\u2014 <a href=\"https:\/\/docs.aiohttp.org\/en\/stable\/web_reference.html?highlight=enable_chunked_encoding#aiohttp.web.StreamResponse.enable_chunked_encoding\" rel=\"noopener noreferrer nofollow\">StreamResponse.enable_chunked_encoding<\/a>. <\/p>\n<p>\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430\u00a0\u043f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u043d\u0438\u0437\u0443 \u0441\u0442\u0430\u0442\u044c\u0438.<\/p>\n<h2>\u041f\u0438\u0448\u0435\u043c \u0441\u043a\u0440\u0438\u043f\u0442 \u043f\u043e\u0442\u043e\u043a\u043e\u0432\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 post-\u043c\u0435\u0442\u043e\u0434\u0430 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u0430<\/h2>\n<p>\u042d\u0442\u043e\u0442 \u0441\u043a\u0440\u0438\u043f\u0442 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439, \u0435\u0433\u043e \u0437\u0430\u0434\u0430\u0447\u0430\u00a0\u2014 \u0441\u0442\u0440\u0438\u043c\u0438\u0442\u044c \u0431\u0430\u0439\u0442\u043e\u0432\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0432\u00a0\u0444\u0430\u0439\u043b\u043e\u0432\u044b\u0439 \u0441\u0435\u0440\u0432\u0438\u0441. \u041e\u0441\u043d\u043e\u0432\u0430 \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u0432\u0437\u044f\u0442\u0430 \u0438\u0437 <a href=\"https:\/\/docs.aiohttp.org\/en\/stable\/client_quickstart.html#streaming-uploads\" rel=\"noopener noreferrer nofollow\">\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/a> aiohttp, \u0432\u00a0\u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043a\u0430\u043a\u00a0\u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u0444\u0430\u0439\u043b\u044b, \u043d\u0435\u00a0\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u044f \u0438\u0445 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0432\u00a0\u043f\u0430\u043c\u044f\u0442\u044c.<\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0432\u00a0\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u044f \u0434\u043e\u0431\u0430\u0432\u0438\u043b \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e\u0431\u00a0\u0438\u043c\u0435\u043d\u0438 \u0444\u0430\u0439\u043b\u0430. \u0414\u043b\u044f\u00a0\u044d\u0442\u043e\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u043b \u0432\u00a0\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0440\u0430\u0437\u0434\u0435\u043b <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Headers\/Content-Disposition\" rel=\"noopener noreferrer nofollow\">CONTENT\u2011DISPOSITION<\/a>.<\/p>\n<p>\u0418\u0442\u043e\u0433\u043e \u0441\u043a\u0440\u0438\u043f\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a: <\/p>\n<pre><code class=\"python\">async def file_sender(file_name: str, chunk_size: int) -> Generator[bytes, None, None]:     \"\"\"     \u0413\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u0441\u0447\u0438\u0442\u044b\u0432\u0430\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u0430 \u043f\u043e \u0447\u0430\u0441\u0442\u044f\u043c             \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b:                     file_name (str): \u0438\u043c\u044f \u0444\u0430\u0439\u043b\u0430, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u043f\u0443\u0442\u044c                     chunk_size (int): \u0440\u0430\u0437\u043c\u0435\u0440 \u043f\u043e\u0440\u0446\u0438\u0438 \u0434\u043b\u044f \u0441\u0447\u0438\u0442\u044b\u0432\u0430\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u0430 \u0432 \u043f\u0430\u043c\u044f\u0442\u044c             \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435:                     chunk (bytes): \u0447\u0430\u0441\u0442\u044c \u0431\u0430\u0439\u0442\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430 \u0444\u0430\u0439\u043b\u0430     \"\"\"      async with aiofiles.open(file_name, 'rb') as f:         chunk = await f.read(chunk_size)          while chunk:             yield chunk             chunk = await f.read(chunk_size)   async def main() -> None:     \"\"\"\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 post-\u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432 \u0430\u0434\u0440\u0435\u0441 \u0444\u0430\u0439\u043b\u043e\u0432\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430\"\"\"      args = get_args()  # \u043a\u043e\u0434 \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0432 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438 \u0434\u043b\u044f \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0442      url = urljoin(f'{args.protocol}:\/\/{args.host}:{args.port}', args.url)     headers = {         'CONTENT-DISPOSITION': f'attachment;filename={os.path.basename(args.path)}',     }      async with aiohttp.ClientSession() as session:         async with session.post(                 url,                 headers=headers,                 data=file_sender(file_name=args.path, chunk_size=args.chunk_size)         ) as resp:             logger.info(await resp.text())<\/code><\/pre>\n<h2>\u0420\u0430\u0437\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c post-\u043c\u0435\u0442\u043e\u0434 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u0430<\/h2>\n<p>\u0412\u00a0\u043f\u0435\u0440\u0432\u043e\u043c \u043f\u0440\u0438\u0431\u043b\u0438\u0436\u0435\u043d\u0438\u0438 \u043a\u043e\u0434 \u0445\u0435\u043d\u0434\u043b\u0435\u0440\u0430 \u043f\u043e\u00a0\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0435 post\u2011\u043c\u0435\u0442\u043e\u0434\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"python\">from aiofile import async_open  async def save_archive(request: Request) -> web.Response:     \"\"\"\u0425\u0435\u043d\u0434\u043b\u0435\u0440 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0431\u0430\u0439\u0442\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430 \u0438\u0437 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432 \u0444\u0430\u0439\u043b\"\"\"      async with async_open(file_path, 'bw') as afp:         # https:\/\/docs.aiohttp.org\/en\/stable\/streams.html#asynchronous-iteration-support         # \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044e \u043f\u043e \u0431\u043b\u043e\u043a\u0430\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0438\u0445 \u0432\u0432\u043e\u0434\u0430 \u0432 \u043f\u043e\u0442\u043e\u043a         async for data in request.content.iter_any():             await afp.write(data)      return web.Response(text='file accepted')<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u044f \u043d\u0430\u0443\u0447\u0438\u043b \u0445\u044d\u043d\u0434\u043b\u0435\u0440 \u0434\u043e\u0441\u0442\u0430\u0432\u0430\u0442\u044c \u0438\u0437\u00a0\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0438\u043c\u044f \u0444\u0430\u0439\u043b\u0430 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0432\u00a0\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445. \u0422\u0430\u0431\u043b\u0438\u0446\u0430 \u0438\u043c\u0435\u0435\u0442 \u0434\u0432\u0430 \u043f\u043e\u043b\u044f\u00a0\u2014 \u0430\u0439\u0434\u0438\u0448\u043d\u0438\u043a \u0438 \u0438\u043c\u044f \u0444\u0430\u0439\u043b\u0430, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0438\u0437 <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Headers\/Content-Disposition\" rel=\"noopener noreferrer nofollow\">CONTENT\u2011DISPOSITION<\/a>.<\/p>\n<p>\u0418\u0442\u043e\u0433\u043e\u0432\u044b\u0439 \u043a\u043e\u0434 \u0445\u0435\u043d\u0434\u043b\u0435\u0440\u0430:<\/p>\n<pre><code class=\"python\">async def save_file(request: Request) -> web.Response:     \"\"\"     \u0425\u0435\u043d\u0434\u043b\u0435\u0440 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0431\u0430\u0439\u0442\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430 \u0438\u0437 http-\u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432 \u0444\u0430\u0439\u043b             \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b:                     request (aiohttp.web_request): \u043e\u0431\u044a\u0435\u043a\u0442 http-\u0437\u0430\u043f\u0440\u043e\u0441\u0430             \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435:                     response (aiohttp.Response): \u043e\u0431\u044a\u0435\u043a\u0442 \u043e\u0442\u0432\u0435\u0442\u0430     \"\"\"      _, params = cgi.parse_header(request.headers['CONTENT-DISPOSITION'])     file_name = params['filename']     file_id = str(uuid.uuid4())     file_path = os.path.join(app['folder'], file_id)      async with async_open(file_path, 'bw') as afp:         # https:\/\/docs.aiohttp.org\/en\/stable\/streams.html#asynchronous-iteration-support         # \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044e \u043f\u043e \u0431\u043b\u043e\u043a\u0430\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0438\u0445 \u0432\u0432\u043e\u0434\u0430 \u0432 \u043f\u043e\u0442\u043e\u043a         async for data in request.content.iter_any():             await afp.write(data)      await logger.debug(f'\u0424\u0430\u0439\u043b \u043f\u0440\u0438\u043d\u044f\u0442 {file_name} \u0438 \u0437\u0430\u043f\u0438\u0441\u0430\u043d \u043d\u0430 \u0434\u0438\u0441\u043a')      async with engine.begin() as conn:         response = await conn.execute(files.insert().values(id=file_id, name=file_name))         await logger.debug(f'\u0424\u0430\u0439\u043b \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d \u043f\u043e\u0434 id={response.inserted_primary_key[0]}')      return web.Response(status=201, reason='OK', text=response.inserted_primary_key[0])<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0444\u0430\u0439\u043b \u043c\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u043f\u043e\u0434\u00a0\u0438\u043c\u0435\u043d\u0435\u043c \u0430\u0439\u0434\u0438\u0448\u043d\u0438\u043a\u0430. \u0412\u00a0\u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u044d\u0442\u043e <a href=\"https:\/\/docs.python.org\/3\/library\/uuid.html#uuid.uuid4\" rel=\"noopener noreferrer nofollow\">UUID4<\/a>. \u0414\u0430\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u0435\u043c \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u043b \u043c\u043d\u0435 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0441\u00a0\u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435\u043c \u0444\u0430\u0439\u043b\u043e\u0432 \u0441\u00a0\u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u043c \u0438\u043c\u0435\u043d\u0435\u043c. <\/p>\n<p>\u0414\u043b\u044f\u00a0\u0440\u0430\u0431\u043e\u0442\u044b \u0441\u00a0\u0411\u0414 \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b <a href=\"https:\/\/www.sqlalchemy.org\/\" rel=\"noopener noreferrer nofollow\">sqlalchemy<\/a>. \u041a\u043e\u0434 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a\u00a0\u0411\u0414 \u043d\u0438\u0436\u0435:<\/p>\n<pre><code class=\"python\">metadata = sqlalchemy.MetaData() engine = create_async_engine(os.environ[\"FILE_SERVICE_DATABASE_URL\"], echo=True) files = sqlalchemy.Table(     \"file\",     metadata,     sqlalchemy.Column(\"id\", sqlalchemy.String(38), primary_key=True),     sqlalchemy.Column(\"name\", sqlalchemy.String(255)), )   async def init_db():     \"\"\"     C\u043e\u0437\u0434\u0430\u0435\u0442 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043e \u0444\u0430\u0439\u043b\u0430\u0445             \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b:             \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435:                     None     \"\"\"      async with engine.begin() as conn:         await conn.run_sync(metadata.create_all)<\/code><\/pre>\n<h2>\u0420\u0430\u0437\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c get-\u043c\u0435\u0442\u043e\u0434 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u0430<\/h2>\n<p>\u0423\u00a0\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439\u00a0\u0431\u044b\u0432\u0430\u0435\u0442 \u0440\u0430\u0437\u043d\u0430\u044f \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0441\u0435\u0442\u0435\u0432\u043e\u0433\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043e\u0442\u0434\u0430\u0447\u0438 \u0444\u0430\u0439\u043b\u0430 \u044f \u0434\u0435\u043b\u0438\u043b \u043d\u0430\u00a0\u0447\u0430\u0441\u0442\u0438. \u042d\u0442\u043e \u043e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0438\u0434\u0435\u044f \u0432\u00a0\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0435 \u0444\u0430\u0439\u043b\u043e\u0432\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430. \u0412\u0441\u0435 \u0431\u0443\u0434\u0443\u0442 \u0432\u00a0\u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u00ab\u0432\u044b\u0438\u0433\u0440\u0430\u043b\u00a0\u2014 \u0432\u044b\u0438\u0433\u0440\u0430\u043b\u00bb: \u043a\u043b\u0438\u0435\u043d\u0442 \u043d\u0430\u0447\u043d\u0435\u0442 \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u0442\u044c \u0444\u0430\u0439\u043b \u0441\u0440\u0430\u0437\u0443, \u0430\u00a0\u043d\u0430\u043c \u043d\u0435\u00a0\u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432\u00a0\u043f\u0430\u043c\u044f\u0442\u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0444\u0430\u0439\u043b \u0446\u0435\u043b\u0438\u043a\u043e\u043c: <\/p>\n<pre><code class=\"python\">async def get_file(request: Request) -> web.StreamResponse:     \"\"\"     \u0425\u0435\u043d\u0434\u043b\u0435\u0440 \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0430\u0440\u0445\u0438\u0432\u0430 \u0438 \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u043d\u0438\u044f \u0435\u0433\u043e \u0432 \u0444\u0430\u0439\u043b             \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b:                     request (aiohttp.web_request): \u043e\u0431\u044a\u0435\u043a\u0442 http-\u0437\u0430\u043f\u0440\u043e\u0441\u0430             \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435:                     response (aiohttp.StreamResponse): \u043e\u0431\u044a\u0435\u043a\u0442 \u043e\u0442\u0432\u0435\u0442\u0430 \u0432 \u0432\u0438\u0434\u0435 \u0431\u0430\u0439\u0442\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430     \"\"\"      file_id = request.match_info['id']     folder_path = os.path.join(os.getcwd(), app['folder'])      if not (os.path.exists(folder_path) and os.path.isdir(folder_path)):         await logger.warning(f'\u0417\u0430\u043f\u0440\u043e\u0448\u0435\u043d\u0430 \u043d\u0435\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0430\u044f \u043f\u0430\u043f\u043a\u0430 {folder_path}')         raise web.HTTPNotFound(text='\u0410\u0440\u0445\u0438\u0432 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0438\u043b\u0438 \u0431\u044b\u043b \u0443\u0434\u0430\u043b\u0435\u043d')      async with engine.connect() as conn:         statement = select(files.c.id, files.c.name).where(files.c.id == file_id)          file_rows = await conn.execute(statement)         file = file_rows.fetchone()          if file is None:             raise web.HTTPNotFound(text='\u0424\u0430\u0439\u043b\u0430 \u043f\u043e \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u043c\u0443 id \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442')         file_path = os.path.join(app['folder'], file_id)      response = web.StreamResponse(         status=200,         reason='OK',         headers={             'Content-Type': 'multipart\/x-mixed-replace',             'CONTENT-DISPOSITION': f'attachment;filename={file.name}'         }     )      # \u041e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0443 HTTP \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0438     await response.prepare(request)      try:         async with async_open(file_path, 'rb') as f:             chunk = await f.read(app['chunk_size'])              while chunk:                 await response.write(chunk)                 chunk = await f.read(app['chunk_size'])      except asyncio.CancelledError:         await logger.error(\"Download was interrupted \")          # \u043e\u0442\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0447\u0435\u043d\u043d\u044b\u0439 CancelledError         raise      return response<\/code><\/pre>\n<h2>\u0418\u0442\u043e\u0433\u043e<\/h2>\n<p>\u042f \u0441\u043e\u0437\u0434\u0430\u043b \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b\u043e\u0432\u044b\u0439 \u0441\u0435\u0440\u0432\u0438\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u043c\u043e\u0433 \u0440\u0430\u0437\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u041e\u0417\u0423 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0438 \u043f\u0440\u0438\u00a0\u044d\u0442\u043e\u043c\u00a0\u0431\u044b\u043b \u0441\u043f\u043e\u0441\u043e\u0431\u0435\u043d \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0432\u00a0HL\u2011\u0440\u0435\u0436\u0438\u043c\u0435, \u0447\u0442\u043e\u00a0\u0432\u0430\u0436\u043d\u043e \u0434\u043b\u044f\u00a0\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432.<\/p>\n<p>\u0420\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0444\u0430\u0439\u043b\u043e\u0432\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0434\u043b\u044f\u00a0\u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u043f\u043e <a href=\"https:\/\/github.com\/s-klimov\/async-file-service.git\" rel=\"noopener noreferrer nofollow\">\u0441\u0441\u044b\u043b\u043a\u0435<\/a>. \u0412 <a href=\"https:\/\/github.com\/s-klimov\/async-file-service\/tree\/master#readme\" rel=\"noopener noreferrer nofollow\">README.md<\/a> \u0432\u044b \u043d\u0430\u0439\u0434\u0435\u0442\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e \u043f\u043e\u00a0\u0437\u0430\u043f\u0443\u0441\u043a\u0443 \u043f\u0440\u043e\u0435\u043a\u0442\u0430.<\/p>\n<p>\u0424\u0438\u0448\u043a\u0430 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432\u00a0\u0442\u043e\u043c, \u0447\u0442\u043e\u00a0\u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0444\u0430\u0439\u043b \u0432\u044b \u0441\u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0432 \u0441\u0441\u044b\u043b\u043a\u0443 \u0432\u00a0\u0430\u0434\u0440\u0435\u0441\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0430. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 <a href=\"http:\/\/localhost:8080\/files\/ff6b51b5-4030-48ed-aa5a-1b8aba37d1c4\/\" rel=\"noopener noreferrer nofollow\">http:\/\/localhost:8080\/files\/ff6b51b5\u20134030\u201348ed\u2011aa5a-1b8aba37d1c4\/<\/a>. \u0410\u00a0\u044d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e\u00a0\u0434\u043b\u044f\u00a0\u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u0430 \u0438\u0437\u00a0\u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0432\u0430\u043c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0441\u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0443\u0440\u043b \u0432\u00a0\u0448\u0430\u0431\u043b\u043e\u043d\u0435 web\u2011\u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/729340\/\"> https:\/\/habr.com\/ru\/articles\/729340\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-414493","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/414493","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=414493"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/414493\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=414493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=414493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=414493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}