OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

FastAPI and aioboto3: Streaming Download

  • Thread starter Thread starter ffempegging
  • Start date Start date
F

ffempegging

Guest
I would like to use an API endpoint as a proxy for streaming a file to the client. I am using FastAPI, as well as aioboto3.

Currently, the implementation of the endpoint looks like this:

Code:
@app.get('/s3/{file_name:path}')
async def file_proxy(file_name: str):
    async with session.client('s3', { config }):
        file = await s3.get_object(Bucket='bucket-name', Key=file_name)
        return StreamingResponse(content=file['Body'].iter_chunks())

The issue is this will await and block the function until the entire file is finished downloading. But, instead of this, I want to download and stream the file continuously, in chunks of n MiB, for example. How can I do this?
<p>I would like to use an API endpoint as a proxy for streaming a file to the client. I am using FastAPI, as well as <a href="https://github.com/terricain/aioboto3" rel="nofollow noreferrer">aioboto3</a>.</p>
<p>Currently, the implementation of the endpoint looks like this:</p>
<pre class="lang-py prettyprint-override"><code>@app.get('/s3/{file_name:path}')
async def file_proxy(file_name: str):
async with session.client('s3', { config }):
file = await s3.get_object(Bucket='bucket-name', Key=file_name)
return StreamingResponse(content=file['Body'].iter_chunks())
</code></pre>
<p>The issue is this will <code>await</code> and block the function until the entire file is finished downloading. But, instead of this, I want to download and stream the file continuously, in chunks of <code>n</code> MiB, for example. How can I do this?</p>
 

Latest posts

Top