Created
July 16, 2023 15:39
-
-
Save jaroel/227134a8ae992de30a37c92f247d4da7 to your computer and use it in GitHub Desktop.
ftp proxy with FastAPI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aioftp | |
from fastapi import FastAPI, HTTPException, Request | |
from fastapi.responses import StreamingResponse | |
app = FastAPI() | |
@app.get('/fetch/{filename}', name='fetch') | |
async def ftp_fetch(request: Request, filename: str): | |
return StreamingResponse( | |
stream_file(filename), | |
media_type='audio/mpeg', | |
headers={ | |
'content-disposition': 'attachment; filename="{}"'.format(filename) | |
}, | |
) | |
async def stream_file(filename): | |
async with aioftp.Client.context(hostname='ftp-server', username='username', password='Welcome123') as client: | |
async with client.download_stream(filename) as stream: | |
async for block in stream.iter_by_block(): | |
yield block |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment