Skip to content

Instantly share code, notes, and snippets.

@jaroel
Created July 16, 2023 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaroel/227134a8ae992de30a37c92f247d4da7 to your computer and use it in GitHub Desktop.
Save jaroel/227134a8ae992de30a37c92f247d4da7 to your computer and use it in GitHub Desktop.
ftp proxy with FastAPI
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