yoshidan / google-cloud-rust

Google Cloud Client Libraries for Rust.
MIT License
243 stars 87 forks source link

Cloud Storage Upload lifetimes #203

Closed ranger-ross closed 11 months ago

ranger-ross commented 11 months ago

First, thanks for creating this library its very helpful.

I am trying to use client.upload_object() to upload some data to Cloud Storage. However I am running into some lifetime issue with the data: T field where my data does not live long enough.

In the documentation the example use a hardcoded "hello world".as_bytes(). But when I try to make the String dynamic I run into lifetime issues. Here is a simple example (modified from example in the docs)

async fn run_simple(client:Client, data: &str) {
   let upload_type = UploadType::Simple(Media::new("filename"));
   let result = client.upload_object(&UploadObjectRequest{
       bucket: "bucket".to_string(),
       ..Default::default()
   }, data.as_bytes(), &upload_type).await;
}

This code does not compile due to lifetime issues.

Sorry if this is a nooby question with an obvious answer. I am still fairly new to Rust, perhaps adding another example would make it more obvious how to use upload_object with non 'static data

yoshidan commented 11 months ago

data type must be Into<Body>. Therefore, it is necessary to use &static str, String, Bytes, etc. https://docs.rs/reqwest/latest/reqwest/struct.Body.html#trait-implementations

async fn run_simple(client:Client, data: String) {
   let upload_type = UploadType::Simple(Media::new("filename"));
   let result = client.upload_object(&UploadObjectRequest{
       bucket: "bucket".to_string(),
       ..Default::default()
   }, data, &upload_type).await;
}
ranger-ross commented 11 months ago

I see, using String as the datatype works. Sorry for the nooby question, I should have looked more closely at Into<Body> and how it functions.

Thank you so much