# Axum S3 file downloading

**URL:** <https://users.rust-lang.org/t/axum-s3-file-downloading/140916>\
**Category:** help\
**Created:** [June 24, 2026, 1:46pm UTC](https://users.rust-lang.org/t/axum-s3-file-downloading/140916 "2026-06-24T13:46:01Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kimishu](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kimishu/32/44928_2.png) [@Kimishu](https://users.rust-lang.org/u/Kimishu)\
**Post date:** [June 24, 2026, 1:46pm UTC](https://users.rust-lang.org/t/axum-s3-file-downloading/140916/1 "2026-06-24T13:46:01Z")

</div>

There are bunch of different crates that provides S3 interaction, like [aws\_sdk\_s3](https://github.com/awslabs/aws-sdk-rust), [s3](https://github.com/durch/rust-s3), [minio](https://github.com/minio/minio-rs)

Result I wanted is to download large file from S3 by axum endpoint, for example "/file".

The core problem I met right now: when I try to download a file, rust service started to load data to RAM. I suppose it's not right behaviour..?

```rust
// Axum + rust-s3
pub async fn download_file_handler(Path(user): Path<Uuid>) -> impl IntoResponse {
    match download_file(user).await {
        Ok(stream) => {
            let response = axum::response::Response::builder();
            let body = axum::body::Body::from_stream(stream.bytes);
            response.body(body).unwrap()
        }
        Err(err) => "File download failure".into_response(),
    }
}

pub async fn download_file(user: Uuid) -> Result<ResponseDataStream> {
        let region = s3::Region::Custom {
            region: "".to_owned(),
            endpoint: S3_ENDPOINT.to_owned(),
        };
        let creds = s3::creds::Credentials::new(
            Some(S3_ACCESS_KEY),
            Some(S3_SECRET_KEY),
            None,
            None,
            None,
        )?;

        let bucket = s3::Bucket::new(CUSTOM_BUCKET, region, creds)?.with_path_style();
        let stream = bucket
            .get_object_stream(format!("{}/{}", user, CUSTOM_FILE_NAME))
            .await?;
        Ok(stream)
    }

```

So, maybe someone have any ideas?

(I tried `axum::response::Redirection`, it works when I'm translating data directly from s3 with presigned request but it's interesting why previous way doesn't work)

---

<div class="post-metadata">

**Author:** ![jofas](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/jofas/32/30690_2.png) [@jofas](https://users.rust-lang.org/u/jofas)\
**Post date:** [June 24, 2026, 2:23pm UTC](https://users.rust-lang.org/t/axum-s3-file-downloading/140916/2 "2026-06-24T14:23:39Z")

</div>

> [@Kimishu](#):
>
> I suppose it's not right behaviour..

What did you expect would happen when you get the blob from S3? Where is the data supposed to go if not into RAM? Or do you mean the whole file is loaded at once and not streamed?

---

<div class="post-metadata">

**Author:** ![Kimishu](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kimishu/32/44928_2.png) [@Kimishu](https://users.rust-lang.org/u/Kimishu)\
**Post date:** [June 24, 2026, 2:29pm UTC](https://users.rust-lang.org/t/axum-s3-file-downloading/140916/3 "2026-06-24T14:29:31Z")

</div>

> [@jofas](#):
>
> blob

The second one. Initially I assumed that I'm taking whole file into RAM, but memory jumps only on response sending.

No matter how obvious this can be, but rust-s3 crate provides whole bytes only for `get_object` fn, not for `get_object_stream`, while aws-sdk-s3 always provides `ByteStream` on response. So, in both cases it shouldn't go into RAM entirely.

---

<div class="post-metadata">

**Author:** ![jofas](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/jofas/32/30690_2.png) [@jofas](https://users.rust-lang.org/u/jofas)\
**Post date:** [June 24, 2026, 2:36pm UTC](https://users.rust-lang.org/t/axum-s3-file-downloading/140916/4 "2026-06-24T14:36:17Z")

</div>

> [@Kimishu](#):
>
> memory jumps only on response sending

The stream from S3 doesn't get polled before sending the response.

---

<div class="post-metadata">

**Author:** ![Kimishu](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kimishu/32/44928_2.png) [@Kimishu](https://users.rust-lang.org/u/Kimishu)\
**Post date:** [June 24, 2026, 2:46pm UTC](https://users.rust-lang.org/t/axum-s3-file-downloading/140916/5 "2026-06-24T14:46:37Z")

</div>

Maybe im wrong, but in my understanding the data streaming means that data goes partially, not at once. Current situation shows that memory continuosly filling up by data, and as a result, when I'm trying to download 5Gb file, it looks like memory leak when process holds 11Gb..

---

<div class="post-metadata">

**Author:** ![jofas](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/jofas/32/30690_2.png) [@jofas](https://users.rust-lang.org/u/jofas)\
**Post date:** [June 24, 2026, 2:52pm UTC](https://users.rust-lang.org/t/axum-s3-file-downloading/140916/6 "2026-06-24T14:52:26Z")

</div>

Maybe you are processing multiple requests to that file concurrently? Have you thought about caching the file on the server, instead of streaming it from s3 upon every request?

---

<div class="post-metadata">

**Author:** ![giocri](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/giocri/32/49821_2.png) [@giocri](https://users.rust-lang.org/u/giocri)\
**Post date:** [June 24, 2026, 2:55pm UTC](https://users.rust-lang.org/t/axum-s3-file-downloading/140916/7 "2026-06-24T14:55:24Z")

</div>

well a stream is going to give you the data gradually yeah but where are you putting the stream data that you get? unless you use as you get it you will end up storing it all in ram before you use it

---

<div class="post-metadata">

**Author:** ![Kimishu](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kimishu/32/44928_2.png) [@Kimishu](https://users.rust-lang.org/u/Kimishu)\
**Post date:** [June 24, 2026, 3:10pm UTC](https://users.rust-lang.org/t/axum-s3-file-downloading/140916/8 "2026-06-24T15:10:32Z")

</div>

> [@jofas](#):
>
> Maybe you are processing multiple requests to that file concurrently? Have you thought about caching the file on the server, instead of streaming it from s3 upon every request?

Nope, it was a single request. Caching is a good idea, but right now I think that such requests will not be too frequent. And right now Im in the same network as my S3.

> [@giocri](#):
>
> well a stream is going to give you the data gradually yeah but where are you putting the stream data that you get? unless you use as you get it you will end up storing it all in ram before you use it

The thing is I'm just puts it to the Axum body, not making any buffers to store whole data. Right now it looks like I simply upload it fully to the RAM. Maybe I should test this thing with local files, not S3, as example. Maybe streams are differ

---

<div class="post-metadata">

**Author:** ![MOCKBA](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mockba/32/41423_2.png) [@MOCKBA](https://users.rust-lang.org/u/MOCKBA)\
**Post date:** [June 24, 2026, 6:31pm UTC](https://users.rust-lang.org/t/axum-s3-file-downloading/140916/9 "2026-06-24T18:31:54Z")

</div>

Server and your client part have to have a parameter - max chunk size in the memory. I realized that after attempting to download multi gigabytes file. After introducing the parameter, memory usage went down to normal. I know the parameter for my server, but no for Axum yet.

---

<div class="post-metadata">

**Author:** ![Kimishu](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kimishu/32/44928_2.png) [@Kimishu](https://users.rust-lang.org/u/Kimishu)\
**Post date:** [June 25, 2026, 10:27am UTC](https://users.rust-lang.org/t/axum-s3-file-downloading/140916/10 "2026-06-25T10:27:13Z")

</div>

Okay, I figured this out. The code above works fine. Also, I tested local file downloading with tokio + tokio-util ReaderStream. Everything is perfect, and there is no high memory usage.

Initially I got stuck while testing this with Postman and Swagger. It turns out the server couldn't write the data to disk and stored it in memory instead. As a result, swagger waits for the entire file to be loaded into RAM and then provides a link to it..  
So, I just saw the problem from the API client perspective and didn't test the browser itself.

(I found only dead topics on how to bypass this swagger restriction, so.. nevermind 🫠)

Appreciate the help!

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [September 23, 2026, 10:27am UTC](https://users.rust-lang.org/t/axum-s3-file-downloading/140916/11 "2026-09-23T10:27:24Z")

</div>

Closed on September 23, 2026, 10:27am UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
