From 6b8d59bd62b21a429960ff556f69490bae8abd63 Mon Sep 17 00:00:00 2001 From: Ron Toland Date: Tue, 5 Apr 2022 15:03:57 -0700 Subject: [PATCH 01/11] add metadata support to object struct --- google-cloud/src/storage/bucket.rs | 1 + google-cloud/src/storage/object.rs | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/google-cloud/src/storage/bucket.rs b/google-cloud/src/storage/bucket.rs index dc58c166..032dffd2 100644 --- a/google-cloud/src/storage/bucket.rs +++ b/google-cloud/src/storage/bucket.rs @@ -83,6 +83,7 @@ impl Bucket { client.clone(), self.name.clone(), resource.name, + resource.metadata )) } diff --git a/google-cloud/src/storage/object.rs b/google-cloud/src/storage/object.rs index 1a876fbe..e537f47f 100644 --- a/google-cloud/src/storage/object.rs +++ b/google-cloud/src/storage/object.rs @@ -8,6 +8,7 @@ pub struct Object { pub(crate) client: Client, pub(crate) name: String, pub(crate) bucket: String, + pub(crate) metadata: HashMap, } impl Object { @@ -15,11 +16,13 @@ impl Object { client: Client, bucket: impl Into, name: impl Into, + metadata: impl Into>, ) -> Object { Object { client, name: name.into(), bucket: bucket.into(), + metadata: metadata.into(), } } @@ -33,6 +36,11 @@ impl Object { self.bucket.as_str() } + /// Get the object's metadata + pub fn metadata(&self) -> &HashMap { + self.metadata + } + // /// Insert a new object into the bucket. // pub async fn writer(&mut self, object: Object) -> Result<(), Error> { // Ok(()) From dce1212fdecaa8d60665b82da71310b091178e4a Mon Sep 17 00:00:00 2001 From: Ron Toland Date: Tue, 5 Apr 2022 15:04:15 -0700 Subject: [PATCH 02/11] add ability to list objects in bucket --- google-cloud/src/storage/bucket.rs | 32 ++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/google-cloud/src/storage/bucket.rs b/google-cloud/src/storage/bucket.rs index 032dffd2..0f7cef2c 100644 --- a/google-cloud/src/storage/bucket.rs +++ b/google-cloud/src/storage/bucket.rs @@ -87,6 +87,38 @@ impl Bucket { )) } + /// List objects stored in the bucket. + pub async fn list(&mut self, opts: &HashMap) -> Result { + let client = &mut self.client; + let inner = &client.client; + let uri = format!( + "{}/b/{}/o", + Client::ENDPOINT, + utf8_percent_encode(&self.name, NON_ALPHANUMERIC), + utf8_percent_encode(name, NON_ALPHANUMERIC), + ); + + let token = client.token_manager.lock().await.token().await?; + let request = inner + .get(uri.as_str()) + .query(&opts) + .header("authorization", token) + .send(); + let response = request.await?; + let resources = response. + error_for_status()? + .json::() + .await?; + + let objects = resources + .items + .into_iter() + .map(|resource| Object::new(self.clone(), resource.name, resource.bucket, resource.metadata)) + .collect(); + + Ok(objects) + } + /// Delete the bucket. pub async fn delete(self) -> Result<(), Error> { let client = self.client; From 69ac4c113a6593b0aea3ff0334dfb1aea2e654fc Mon Sep 17 00:00:00 2001 From: Ron Toland Date: Tue, 5 Apr 2022 16:12:43 -0700 Subject: [PATCH 03/11] add objectresources and hashmap imports --- google-cloud/src/storage/api/object.rs | 9 +++++++++ google-cloud/src/storage/bucket.rs | 12 +++++++----- google-cloud/src/storage/object.rs | 4 +++- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/google-cloud/src/storage/api/object.rs b/google-cloud/src/storage/api/object.rs index 08c6d05e..229f841a 100644 --- a/google-cloud/src/storage/api/object.rs +++ b/google-cloud/src/storage/api/object.rs @@ -4,6 +4,15 @@ use serde::{Deserialize, Serialize}; use crate::storage::api::object_acl::ObjectAclResource; +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct ObjectResources { + /// Value: "storage#objects" + pub kind: String, + #[serde(default)] + pub items: Vec, +} + #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct ObjectResource { diff --git a/google-cloud/src/storage/bucket.rs b/google-cloud/src/storage/bucket.rs index 0f7cef2c..80361f06 100644 --- a/google-cloud/src/storage/bucket.rs +++ b/google-cloud/src/storage/bucket.rs @@ -1,8 +1,10 @@ use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC}; -use crate::storage::api::object::ObjectResource; +use crate::storage::api::object::*; use crate::storage::{Client, Error, Object}; +use std::collections::HashMap; + /// Represents a Cloud Storage bucket. #[derive(Clone)] pub struct Bucket { @@ -56,6 +58,7 @@ impl Bucket { client.clone(), self.name.clone(), resource.name, + resource.metadata, )) } @@ -88,20 +91,19 @@ impl Bucket { } /// List objects stored in the bucket. - pub async fn list(&mut self, opts: &HashMap) -> Result { + pub async fn list(&mut self, list_options: &HashMap) -> Result { let client = &mut self.client; let inner = &client.client; let uri = format!( "{}/b/{}/o", Client::ENDPOINT, utf8_percent_encode(&self.name, NON_ALPHANUMERIC), - utf8_percent_encode(name, NON_ALPHANUMERIC), ); let token = client.token_manager.lock().await.token().await?; let request = inner .get(uri.as_str()) - .query(&opts) + .query(&list_options) .header("authorization", token) .send(); let response = request.await?; @@ -113,7 +115,7 @@ impl Bucket { let objects = resources .items .into_iter() - .map(|resource| Object::new(self.clone(), resource.name, resource.bucket, resource.metadata)) + .map(|resource| Object::new(client.clone(), resource.name, resource.bucket, resource.metadata)) .collect(); Ok(objects) diff --git a/google-cloud/src/storage/object.rs b/google-cloud/src/storage/object.rs index e537f47f..588abd05 100644 --- a/google-cloud/src/storage/object.rs +++ b/google-cloud/src/storage/object.rs @@ -2,6 +2,8 @@ use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC}; use crate::storage::{Client, Error}; +use std::collections::HashMap; + /// Represents a Cloud Storage bucket. #[derive(Clone)] pub struct Object { @@ -16,7 +18,7 @@ impl Object { client: Client, bucket: impl Into, name: impl Into, - metadata: impl Into>, + metadata: impl Into, ) -> Object { Object { client, From d28506f80e18f5fc3dcb4053aa3a97b5ea2550e8 Mon Sep 17 00:00:00 2001 From: Ron Toland Date: Tue, 5 Apr 2022 16:16:35 -0700 Subject: [PATCH 04/11] bump version to 0.3.0 --- google-cloud/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google-cloud/Cargo.toml b/google-cloud/Cargo.toml index 4c20e637..c96ea340 100644 --- a/google-cloud/Cargo.toml +++ b/google-cloud/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "google-cloud" -version = "0.2.1" +version = "0.3.0" description = "Asynchronous Rust bindings for Google Cloud Platform gRPC APIs" authors = ["Nicolas Polomack "] edition = "2018" From 387b918e163325eb9a7f81517932fb964d0396dd Mon Sep 17 00:00:00 2001 From: Ron Toland Date: Tue, 5 Apr 2022 16:21:02 -0700 Subject: [PATCH 05/11] use K, V for hashmap generics --- google-cloud/src/storage/bucket.rs | 2 +- google-cloud/src/storage/object.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/google-cloud/src/storage/bucket.rs b/google-cloud/src/storage/bucket.rs index 80361f06..c30188ca 100644 --- a/google-cloud/src/storage/bucket.rs +++ b/google-cloud/src/storage/bucket.rs @@ -91,7 +91,7 @@ impl Bucket { } /// List objects stored in the bucket. - pub async fn list(&mut self, list_options: &HashMap) -> Result { + pub async fn list(&mut self, list_options: &HashMap) -> Result { let client = &mut self.client; let inner = &client.client; let uri = format!( diff --git a/google-cloud/src/storage/object.rs b/google-cloud/src/storage/object.rs index 588abd05..097bdddb 100644 --- a/google-cloud/src/storage/object.rs +++ b/google-cloud/src/storage/object.rs @@ -18,7 +18,7 @@ impl Object { client: Client, bucket: impl Into, name: impl Into, - metadata: impl Into, + metadata: impl Into>, ) -> Object { Object { client, From d7ecffc1a11ceb4ee7da348121b3685a16ef8f96 Mon Sep 17 00:00:00 2001 From: Ron Toland Date: Tue, 5 Apr 2022 16:36:28 -0700 Subject: [PATCH 06/11] metadata for object is string, string --- google-cloud/src/storage/object.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/google-cloud/src/storage/object.rs b/google-cloud/src/storage/object.rs index 097bdddb..79c7f8bb 100644 --- a/google-cloud/src/storage/object.rs +++ b/google-cloud/src/storage/object.rs @@ -18,13 +18,13 @@ impl Object { client: Client, bucket: impl Into, name: impl Into, - metadata: impl Into>, + metadata: HashMap, ) -> Object { Object { client, name: name.into(), bucket: bucket.into(), - metadata: metadata.into(), + metadata: metadata, } } From ba0da0f88e95b8435cfe552e5e3354bc915de3d7 Mon Sep 17 00:00:00 2001 From: Ron Toland Date: Tue, 5 Apr 2022 16:41:58 -0700 Subject: [PATCH 07/11] better handling of object metadata --- google-cloud/src/storage/bucket.rs | 6 +++--- google-cloud/src/storage/object.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/google-cloud/src/storage/bucket.rs b/google-cloud/src/storage/bucket.rs index c30188ca..87c03ad7 100644 --- a/google-cloud/src/storage/bucket.rs +++ b/google-cloud/src/storage/bucket.rs @@ -58,7 +58,7 @@ impl Bucket { client.clone(), self.name.clone(), resource.name, - resource.metadata, + resource.metadata.unwrap_or(HashMap::new()), )) } @@ -86,7 +86,7 @@ impl Bucket { client.clone(), self.name.clone(), resource.name, - resource.metadata + resource.metadata.unwrap_or(HashMap::new()), )) } @@ -115,7 +115,7 @@ impl Bucket { let objects = resources .items .into_iter() - .map(|resource| Object::new(client.clone(), resource.name, resource.bucket, resource.metadata)) + .map(|resource| Object::new(client.clone(), resource.name, resource.bucket, resource.metadata.unwrap_or(HashMap::new())) .collect(); Ok(objects) diff --git a/google-cloud/src/storage/object.rs b/google-cloud/src/storage/object.rs index 79c7f8bb..304bef4b 100644 --- a/google-cloud/src/storage/object.rs +++ b/google-cloud/src/storage/object.rs @@ -40,7 +40,7 @@ impl Object { /// Get the object's metadata pub fn metadata(&self) -> &HashMap { - self.metadata + &self.metadata } // /// Insert a new object into the bucket. From 18d020bc268f9b819c95e0949923a326d82e501f Mon Sep 17 00:00:00 2001 From: Ron Toland Date: Tue, 5 Apr 2022 16:43:16 -0700 Subject: [PATCH 08/11] fix missing paren --- google-cloud/src/storage/bucket.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google-cloud/src/storage/bucket.rs b/google-cloud/src/storage/bucket.rs index 87c03ad7..bf37fda6 100644 --- a/google-cloud/src/storage/bucket.rs +++ b/google-cloud/src/storage/bucket.rs @@ -115,7 +115,7 @@ impl Bucket { let objects = resources .items .into_iter() - .map(|resource| Object::new(client.clone(), resource.name, resource.bucket, resource.metadata.unwrap_or(HashMap::new())) + .map(|resource| Object::new(client.clone(), resource.name, resource.bucket, resource.metadata.unwrap_or(HashMap::new()))) .collect(); Ok(objects) From 31d26769e716a1d5c6c8dd33b6ad04d4bf44c11f Mon Sep 17 00:00:00 2001 From: Ron Toland Date: Tue, 5 Apr 2022 16:55:13 -0700 Subject: [PATCH 09/11] adjust type params and return type for list fn --- google-cloud/src/storage/bucket.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google-cloud/src/storage/bucket.rs b/google-cloud/src/storage/bucket.rs index bf37fda6..5a6fa340 100644 --- a/google-cloud/src/storage/bucket.rs +++ b/google-cloud/src/storage/bucket.rs @@ -91,7 +91,7 @@ impl Bucket { } /// List objects stored in the bucket. - pub async fn list(&mut self, list_options: &HashMap) -> Result { + pub async fn list(&mut self, list_options: &HashMap) -> Result, Error> { let client = &mut self.client; let inner = &client.client; let uri = format!( From 647bd3a5b2dee722ad1520f2f6693a155fc9477b Mon Sep 17 00:00:00 2001 From: Ron Toland Date: Tue, 5 Apr 2022 16:57:33 -0700 Subject: [PATCH 10/11] constrain type of V --- google-cloud/src/storage/bucket.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/google-cloud/src/storage/bucket.rs b/google-cloud/src/storage/bucket.rs index 5a6fa340..2584f283 100644 --- a/google-cloud/src/storage/bucket.rs +++ b/google-cloud/src/storage/bucket.rs @@ -3,6 +3,7 @@ use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC}; use crate::storage::api::object::*; use crate::storage::{Client, Error, Object}; +use serde::Serialize; use std::collections::HashMap; /// Represents a Cloud Storage bucket. @@ -91,7 +92,7 @@ impl Bucket { } /// List objects stored in the bucket. - pub async fn list(&mut self, list_options: &HashMap) -> Result, Error> { + pub async fn list(&mut self, list_options: &HashMap) -> Result, Error> { let client = &mut self.client; let inner = &client.client; let uri = format!( From c20d7256059ac9e3476272dd9f355795f79b74bc Mon Sep 17 00:00:00 2001 From: Ron Toland Date: Wed, 6 Apr 2022 10:04:44 -0700 Subject: [PATCH 11/11] support id in object struct --- google-cloud/src/storage/bucket.rs | 4 +++- google-cloud/src/storage/object.rs | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/google-cloud/src/storage/bucket.rs b/google-cloud/src/storage/bucket.rs index 2584f283..4945424b 100644 --- a/google-cloud/src/storage/bucket.rs +++ b/google-cloud/src/storage/bucket.rs @@ -58,6 +58,7 @@ impl Bucket { Ok(Object::new( client.clone(), self.name.clone(), + resource.id, resource.name, resource.metadata.unwrap_or(HashMap::new()), )) @@ -86,6 +87,7 @@ impl Bucket { Ok(Object::new( client.clone(), self.name.clone(), + resource.id, resource.name, resource.metadata.unwrap_or(HashMap::new()), )) @@ -116,7 +118,7 @@ impl Bucket { let objects = resources .items .into_iter() - .map(|resource| Object::new(client.clone(), resource.name, resource.bucket, resource.metadata.unwrap_or(HashMap::new()))) + .map(|resource| Object::new(client.clone(), resource.id, resource.name, resource.bucket, resource.metadata.unwrap_or(HashMap::new()))) .collect(); Ok(objects) diff --git a/google-cloud/src/storage/object.rs b/google-cloud/src/storage/object.rs index 304bef4b..25ada2c2 100644 --- a/google-cloud/src/storage/object.rs +++ b/google-cloud/src/storage/object.rs @@ -8,6 +8,7 @@ use std::collections::HashMap; #[derive(Clone)] pub struct Object { pub(crate) client: Client, + pub(crate) id: String, pub(crate) name: String, pub(crate) bucket: String, pub(crate) metadata: HashMap, @@ -17,17 +18,24 @@ impl Object { pub(crate) fn new( client: Client, bucket: impl Into, + id: impl Into, name: impl Into, metadata: HashMap, ) -> Object { Object { client, + id: id.into(), name: name.into(), bucket: bucket.into(), metadata: metadata, } } + /// Get the object's id. + pub fn id(&self) -> &str { + self.id.as_str() + } + /// Get the object's name. pub fn name(&self) -> &str { self.name.as_str()