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" 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 dc58c166..4945424b 100644 --- a/google-cloud/src/storage/bucket.rs +++ b/google-cloud/src/storage/bucket.rs @@ -1,8 +1,11 @@ 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 serde::Serialize; +use std::collections::HashMap; + /// Represents a Cloud Storage bucket. #[derive(Clone)] pub struct Bucket { @@ -55,7 +58,9 @@ impl Bucket { Ok(Object::new( client.clone(), self.name.clone(), + resource.id, resource.name, + resource.metadata.unwrap_or(HashMap::new()), )) } @@ -82,10 +87,43 @@ impl Bucket { Ok(Object::new( client.clone(), self.name.clone(), + resource.id, resource.name, + resource.metadata.unwrap_or(HashMap::new()), )) } + /// List objects stored in the bucket. + pub async fn list(&mut self, list_options: &HashMap) -> Result, Error> { + let client = &mut self.client; + let inner = &client.client; + let uri = format!( + "{}/b/{}/o", + Client::ENDPOINT, + utf8_percent_encode(&self.name, NON_ALPHANUMERIC), + ); + + let token = client.token_manager.lock().await.token().await?; + let request = inner + .get(uri.as_str()) + .query(&list_options) + .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(client.clone(), resource.id, resource.name, resource.bucket, resource.metadata.unwrap_or(HashMap::new()))) + .collect(); + + Ok(objects) + } + /// Delete the bucket. pub async fn delete(self) -> Result<(), Error> { let client = self.client; diff --git a/google-cloud/src/storage/object.rs b/google-cloud/src/storage/object.rs index 1a876fbe..25ada2c2 100644 --- a/google-cloud/src/storage/object.rs +++ b/google-cloud/src/storage/object.rs @@ -2,27 +2,40 @@ 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 { pub(crate) client: Client, + pub(crate) id: String, pub(crate) name: String, pub(crate) bucket: String, + pub(crate) metadata: HashMap, } 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() @@ -33,6 +46,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(())