0

I've created a small Github repo that mimics exactly what I'm trying to achieve: streaming file uploads to both file systems and the cloud (S3) using axum (and reqwest).

I can change all the code you see. There is no restriction. All signatures were invented by me who are in my early days with the awesome Rust.

If you want I can also eliminate reqwest, as long as I understand what to use instead! :smile:

Can you help me understand how to fix the only error left?

error[E0521]: borrowed data escapes outside of associated function
  --> src\adapter_s3.rs:56:19
   |
41 |     async fn put_file<'a>(
   |                       -- lifetime `'a` defined here
42 |         &'a self,
   |         -------- `self` is a reference that is only valid in the associated function body
...
56 |             .body(reqwest::Body::wrap_stream(ReaderStream::new(reader)))
   |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |                   |
   |                   `self` escapes the associated function body here
   |                   argument requires that `'a` must outlive `'static`

This is the relevant code I think:

async fn put_file<'a>(
    &'a self,
    filename: &'a str,
    reader: Pin<Box<dyn AsyncRead + Send + Sync + 'a>>,
) -> Result<()> {
    let presigned_url_duration = Duration::from_secs(60 * 60);

    let action = self.bucket.put_object(Some(&self.credentials), filename);

    let client = reqwest::Client::new();

    client
        .post(action.sign(presigned_url_duration))
        // .body(reader)
        // .body(reqwest::Body::wrap_stream(reader))
        .body(reqwest::Body::wrap_stream(ReaderStream::new(reader)))
        .send()
        .await?
        .error_for_status()?;

    Ok(())
}

UPDATE:

I tried with hyper too and it's the same error obviously:

hyper::Client::new()
    .request(
        hyper::Request::builder()
            .uri(action.sign(presigned_url_duration).to_string())
            .body(hyper::Body::wrap_stream(ReaderStream::new(reader)))
            .unwrap(),
    )
    .await
    .unwrap();
error[E0521]: borrowed data escapes outside of associated function
    |
66  |     async fn put_file<'a>(
    |                       -- lifetime `'a` defined here
67  |         &'a self,
    |         -------- `self` is a reference that is only valid in the associated function body
...
124 |                     .body(hyper::Body::wrap_stream(ReaderStream::new(reader)))
    |                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |                           |
    |                           `self` escapes the associated function body here
    |                           argument requires that `'a` must outlive `'static`
Fred Hors
  • 3,258
  • 3
  • 25
  • 71
  • Can you try with just `&self` instead of `&'a self`? – PitaJ Dec 15 '22 at 17:33
  • Nope, because then I have a problem with axum unfortunately. – Fred Hors Dec 15 '22 at 17:36
  • What "problem with axum" are you having? – PitaJ Dec 15 '22 at 17:37
  • This: https://github.com/tokio-rs/axum/discussions/1644#discussion-4666921. Can you please clone the small project and try editing it? It's very difficult to change a bit of it because of errors. This is the reason I published it. I'm loosing my mind from yesterday. – Fred Hors Dec 15 '22 at 17:44

0 Answers0