Skip to content

Commit

Permalink
Merge pull request #1408 from Phala-Network/update-pink-s3
Browse files Browse the repository at this point in the history
Update pink s3
  • Loading branch information
kvinwang authored Oct 12, 2023
2 parents cf1adc5 + 2dada3a commit 945d3bf
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/pink-libs/s3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down
12 changes: 9 additions & 3 deletions crates/pink-libs/s3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 8 additions & 2 deletions crates/pink-libs/s3/example/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
31 changes: 8 additions & 23 deletions crates/pink-libs/s3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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,
Expand Down Expand Up @@ -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<Head, Error> {
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<HttpResponse, Error> {
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<Vec<u8>, Error> {
Ok(self.request("GET", bucket_name, object_key, None)?.body)
pub fn get(&self, bucket_name: &str, object_key: &str) -> Result<HttpResponse, Error> {
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<HttpResponse, Error> {
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<HttpResponse, Error> {
self.request("DELETE", bucket_name, object_key, None)
.map(|_| ())
}

fn request(
Expand Down

0 comments on commit 945d3bf

Please sign in to comment.