Skip to content

Commit

Permalink
Fixed google_storage_upload_stream_bytes error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
abdolence committed Oct 1, 2022
1 parent 02bc57d commit 2a74c3c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 35 deletions.
2 changes: 0 additions & 2 deletions examples/gcs-rest-client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.finish();
tracing::subscriber::set_global_default(subscriber)?;


// Detect Google project ID using environment variables PROJECT_ID/GCP_PROJECT_ID
// or GKE metadata server when the app runs inside GKE
let google_project_id = gcloud_sdk::GoogleEnvironment::detect_google_project_id().await
Expand All @@ -24,6 +23,5 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

println!("{:?}", response);


Ok(())
}
71 changes: 38 additions & 33 deletions gcloud-sdk/src/proto_ext/storage_upload_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,45 @@ use reqwest::Body;
pub type BoxStreamWithSync<'a, T> =
std::pin::Pin<Box<dyn futures::Stream<Item = T> + Send + 'a + Sync>>;

pub async fn google_storage_upload_stream_bytes<T: ToString>(
google_rest_api: &GoogleRestApi,
bucket_name: String,
filename: String,
content_type: T,
bytes_stream: BoxStreamWithSync<'static, crate::error::Result<bytes::Bytes>>,
) -> crate::error::Result<crate::google_rest_apis::storage_v1::object::Object> {
let upload_url = format!(
"https://storage.googleapis.com/upload/storage/v1/b/{}/o",
bucket_name
);
impl GoogleRestApi {
pub async fn google_storage_v1_upload_stream_bytes<T: ToString>(
&self,
bucket_name: String,
filename: String,
content_type: T,
bytes_stream: BoxStreamWithSync<
'static,
std::result::Result<bytes::Bytes, Box<(dyn std::error::Error + Send + Sync + 'static)>>,
>,
) -> crate::error::Result<crate::google_rest_apis::storage_v1::object::Object> {
let upload_url = format!(
"https://storage.googleapis.com/upload/storage/v1/b/{}/o",
bucket_name
);

let response = google_rest_api
.with_google_token(
google_rest_api.client.post(
&create_hyper_uri_with_params(
upload_url.as_str(),
&vec![
("name", Some(filename).as_ref()),
("uploadType", Some("media".into()).as_ref()),
],
)
.to_string(),
),
)
.await?
.header(reqwest::header::CONTENT_TYPE, content_type.to_string())
.body(Body::wrap_stream(bytes_stream))
.send()
.await?;
let response = self
.with_google_token(
self.client.post(
&create_hyper_uri_with_params(
upload_url.as_str(),
&vec![
("name", Some(filename).as_ref()),
("uploadType", Some("media".into()).as_ref()),
],
)
.to_string(),
),
)
.await?
.header(reqwest::header::CONTENT_TYPE, content_type.to_string())
.body(Body::wrap_stream(bytes_stream))
.send()
.await?;

let json_result = response
.json::<crate::google_rest_apis::storage_v1::object::Object>()
.await?;
let json_result = response
.json::<crate::google_rest_apis::storage_v1::object::Object>()
.await?;

Ok(json_result)
Ok(json_result)
}
}

0 comments on commit 2a74c3c

Please sign in to comment.