diff --git a/Cargo.lock b/Cargo.lock index 2a8e763a51..711a39b707 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10196,7 +10196,7 @@ dependencies = [ [[package]] name = "pink-s3" -version = "0.4.1" +version = "0.5.0" dependencies = [ "base16", "chrono", diff --git a/crates/pink-libs/s3/Cargo.toml b/crates/pink-libs/s3/Cargo.toml index 934675ac89..1f3c37d598 100644 --- a/crates/pink-libs/s3/Cargo.toml +++ b/crates/pink-libs/s3/Cargo.toml @@ -3,7 +3,7 @@ description = "Simple S3 client for Phala's pink" homepage = "https://github.com/Phala-Network/phala-blockchain" license = "Apache-2.0" name = "pink-s3" -version = "0.4.1" +version = "0.5.0" edition = "2021" keywords = ["phat-contract", "pink", "ink", "S3"] diff --git a/crates/pink-libs/s3/README.md b/crates/pink-libs/s3/README.md index c88ef79286..aef0ce2aac 100644 --- a/crates/pink-libs/s3/README.md +++ b/crates/pink-libs/s3/README.md @@ -25,9 +25,15 @@ let value = b"bar"; s3.put(bucket, object_key, value).unwrap(); let head = s3.head(bucket, object_key).unwrap(); -assert_eq!(head.content_length, value.len() as u64); - -let v = s3.get(bucket, object_key).unwrap(); +let mut head_content_length: u64 = 0; +for (k, v) in head.headers { + if k.to_ascii_lowercase() == "content-length" { + head_content_length = v.parse().expect("expect length") + } +} +assert_eq!(head_content_length, value.len() as u64); + +let v = s3.get(bucket, object_key).unwrap().body; assert_eq!(v, value); s3.delete(bucket, object_key).unwrap(); diff --git a/crates/pink-libs/s3/example/lib.rs b/crates/pink-libs/s3/example/lib.rs index 204e54d995..58704b2f53 100755 --- a/crates/pink-libs/s3/example/lib.rs +++ b/crates/pink-libs/s3/example/lib.rs @@ -34,9 +34,15 @@ mod example { s3.put(bucket, object_key, value).unwrap(); let head = s3.head(bucket, object_key).unwrap(); - assert_eq!(head.content_length, value.len() as u64); + let mut head_content_length: u64 = 0; + for (k, v) in head.headers { + if k.to_ascii_lowercase() == "content-length" { + head_content_length = v.parse().expect("expect length") + } + } + assert_eq!(head_content_length, value.len() as u64); - let v = s3.get(bucket, object_key).unwrap(); + let v = s3.get(bucket, object_key).unwrap().body; assert_eq!(v, value); s3.delete(bucket, object_key).unwrap(); diff --git a/crates/pink-libs/s3/src/lib.rs b/crates/pink-libs/s3/src/lib.rs index c10b40c982..2ae022e290 100644 --- a/crates/pink-libs/s3/src/lib.rs +++ b/crates/pink-libs/s3/src/lib.rs @@ -4,7 +4,8 @@ #[macro_use] extern crate alloc; -use pink::chain_extension::HttpResponse; +pub use pink::chain_extension::HttpResponse; + use pink_extension as pink; use scale::{Decode, Encode}; @@ -27,12 +28,6 @@ pub enum Error { InvalidEndpoint, } -/// Infomation of a S3 object -pub struct Head { - /// The size of the object - pub content_length: u64, -} - /// The S3 client pub struct S3<'a> { endpoint: &'a str, @@ -72,37 +67,27 @@ impl<'a> S3<'a> { /// Get object metadata from given bucket /// /// Returns Error::RequestFailed(404) it does not exist. - pub fn head(&self, bucket_name: &str, object_key: &str) -> Result { - let response = self.request("HEAD", bucket_name, object_key, None)?; - for (k, v) in response.headers { - if k.to_ascii_lowercase() == "content-length" { - return Ok(Head { - content_length: v.parse().or(Err(Error::RequestFailed(600)))?, - }); - } - } - Err(Error::RequestFailed(response.status_code)) + pub fn head(&self, bucket_name: &str, object_key: &str) -> Result { + self.request("HEAD", bucket_name, object_key, None) } /// Get object value from bucket `bucket_name` with key `object_key`. /// /// Returns Error::RequestFailed(404) it does not exist. - pub fn get(&self, bucket_name: &str, object_key: &str) -> Result, Error> { - Ok(self.request("GET", bucket_name, object_key, None)?.body) + pub fn get(&self, bucket_name: &str, object_key: &str) -> Result { + self.request("GET", bucket_name, object_key, None) } /// Put an value into bucket `bucket_name` with key `object_key`. - pub fn put(&self, bucket_name: &str, object_key: &str, value: &[u8]) -> Result<(), Error> { + pub fn put(&self, bucket_name: &str, object_key: &str, value: &[u8]) -> Result { self.request("PUT", bucket_name, object_key, Some(value)) - .map(|_| ()) } /// Delete given object from bucket `bucket_name` with key `object_key`. /// /// Returns Error::RequestFailed(404) it does not exist. - pub fn delete(&self, bucket_name: &str, object_key: &str) -> Result<(), Error> { + pub fn delete(&self, bucket_name: &str, object_key: &str) -> Result { self.request("DELETE", bucket_name, object_key, None) - .map(|_| ()) } fn request(