You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by xu...@apache.org on 2023/04/11 00:21:50 UTC

[incubator-opendal] branch async-presign created (now 799d35b2)

This is an automated email from the ASF dual-hosted git repository.

xuanwo pushed a change to branch async-presign
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git


      at 799d35b2 refactor: Change presign to async for future refactor

This branch includes the following new commits:

     new 799d35b2 refactor: Change presign to async for future refactor

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[incubator-opendal] 01/01: refactor: Change presign to async for future refactor

Posted by xu...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xuanwo pushed a commit to branch async-presign
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git

commit 799d35b2eb9bf3bd69d2b4d744cc1fd72a57c7fb
Author: Xuanwo <gi...@xuanwo.io>
AuthorDate: Tue Apr 11 08:21:30 2023 +0800

    refactor: Change presign to async for future refactor
    
    Signed-off-by: Xuanwo <gi...@xuanwo.io>
---
 bindings/nodejs/src/lib.rs          |  9 ++++++---
 core/src/layers/error_context.rs    |  4 ++--
 core/src/layers/logging.rs          |  3 ++-
 core/src/layers/metrics.rs          |  4 ++--
 core/src/layers/tracing.rs          |  4 ++--
 core/src/raw/accessor.rs            |  6 +++---
 core/src/raw/layer.rs               |  8 ++++----
 core/src/services/oss/backend.rs    |  2 +-
 core/src/services/s3/backend.rs     |  2 +-
 core/src/types/operator/operator.rs | 20 ++++++++++----------
 core/tests/behavior/presign.rs      |  6 +++---
 11 files changed, 36 insertions(+), 32 deletions(-)

diff --git a/bindings/nodejs/src/lib.rs b/bindings/nodejs/src/lib.rs
index bbf831e8..04d6c7cd 100644
--- a/bindings/nodejs/src/lib.rs
+++ b/bindings/nodejs/src/lib.rs
@@ -484,10 +484,11 @@ impl Operator {
     /// console.log("headers: ", req.headers);
     /// ```
     #[napi]
-    pub fn presign_read(&self, path: String, expires: u32) -> Result<PresignedRequest> {
+    pub async fn presign_read(&self, path: String, expires: u32) -> Result<PresignedRequest> {
         let res = self
             .0
             .presign_read(&path, Duration::seconds(expires as i64))
+            .await
             .map_err(format_napi_error)?;
         Ok(PresignedRequest::new(res))
     }
@@ -506,10 +507,11 @@ impl Operator {
     /// console.log("headers: ", req.headers);
     /// ```
     #[napi]
-    pub fn presign_write(&self, path: String, expires: u32) -> Result<PresignedRequest> {
+    pub async fn presign_write(&self, path: String, expires: u32) -> Result<PresignedRequest> {
         let res = self
             .0
             .presign_write(&path, Duration::seconds(expires as i64))
+            .await
             .map_err(format_napi_error)?;
         Ok(PresignedRequest::new(res))
     }
@@ -528,10 +530,11 @@ impl Operator {
     /// console.log("headers: ", req.headers);
     /// ```
     #[napi]
-    pub fn presign_stat(&self, path: String, expires: u32) -> Result<PresignedRequest> {
+    pub async fn presign_stat(&self, path: String, expires: u32) -> Result<PresignedRequest> {
         let res = self
             .0
             .presign_stat(&path, Duration::seconds(expires as i64))
+            .await
             .map_err(format_napi_error)?;
         Ok(PresignedRequest::new(res))
     }
diff --git a/core/src/layers/error_context.rs b/core/src/layers/error_context.rs
index 2546c5c4..e1271650 100644
--- a/core/src/layers/error_context.rs
+++ b/core/src/layers/error_context.rs
@@ -202,8 +202,8 @@ impl<A: Accessor> LayeredAccessor for ErrorContextAccessor<A> {
             .await
     }
 
-    fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
-        self.inner.presign(path, args).map_err(|err| {
+    async fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
+        self.inner.presign(path, args).await.map_err(|err| {
             err.with_operation(Operation::Presign)
                 .with_context("service", self.meta.scheme())
                 .with_context("path", path)
diff --git a/core/src/layers/logging.rs b/core/src/layers/logging.rs
index e4f9b052..6776e2ed 100644
--- a/core/src/layers/logging.rs
+++ b/core/src/layers/logging.rs
@@ -576,7 +576,7 @@ impl<A: Accessor> LayeredAccessor for LoggingAccessor<A> {
             .await
     }
 
-    fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
+    async fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
         debug!(
             target: LOGGING_TARGET,
             "service={} operation={} path={} -> started",
@@ -587,6 +587,7 @@ impl<A: Accessor> LayeredAccessor for LoggingAccessor<A> {
 
         self.inner
             .presign(path, args)
+            .await
             .map(|v| {
                 debug!(
                     target: LOGGING_TARGET,
diff --git a/core/src/layers/metrics.rs b/core/src/layers/metrics.rs
index 636235a6..99f18bb4 100644
--- a/core/src/layers/metrics.rs
+++ b/core/src/layers/metrics.rs
@@ -630,11 +630,11 @@ impl<A: Accessor> LayeredAccessor for MetricsAccessor<A> {
         })
     }
 
-    fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
+    async fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
         self.handle.requests_total_presign.increment(1);
 
         let start = Instant::now();
-        let result = self.inner.presign(path, args);
+        let result = self.inner.presign(path, args).await;
         let dur = start.elapsed().as_secs_f64();
 
         self.handle.requests_duration_seconds_presign.record(dur);
diff --git a/core/src/layers/tracing.rs b/core/src/layers/tracing.rs
index ffbf08c0..ac013b08 100644
--- a/core/src/layers/tracing.rs
+++ b/core/src/layers/tracing.rs
@@ -198,8 +198,8 @@ impl<A: Accessor> LayeredAccessor for TracingAccessor<A> {
     }
 
     #[tracing::instrument(level = "debug", skip(self))]
-    fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
-        self.inner.presign(path, args)
+    async fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
+        self.inner.presign(path, args).await
     }
 
     #[tracing::instrument(level = "debug", skip(self))]
diff --git a/core/src/raw/accessor.rs b/core/src/raw/accessor.rs
index 979235df..4bf41c39 100644
--- a/core/src/raw/accessor.rs
+++ b/core/src/raw/accessor.rs
@@ -232,7 +232,7 @@ pub trait Accessor: Send + Sync + Debug + Unpin + 'static {
     /// # Behavior
     ///
     /// - This API is optional, return [`std::io::ErrorKind::Unsupported`] if not supported.
-    fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
+    async fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
         let (_, _) = (path, args);
 
         Err(Error::new(
@@ -453,8 +453,8 @@ impl<T: Accessor + ?Sized> Accessor for Arc<T> {
         self.as_ref().batch(args).await
     }
 
-    fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
-        self.as_ref().presign(path, args)
+    async fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
+        self.as_ref().presign(path, args).await
     }
 
     fn blocking_create(&self, path: &str, args: OpCreate) -> Result<RpCreate> {
diff --git a/core/src/raw/layer.rs b/core/src/raw/layer.rs
index 02a63c56..bb49abdd 100644
--- a/core/src/raw/layer.rs
+++ b/core/src/raw/layer.rs
@@ -183,8 +183,8 @@ pub trait LayeredAccessor: Send + Sync + Debug + Unpin + 'static {
         self.inner().batch(args).await
     }
 
-    fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
-        self.inner().presign(path, args)
+    async fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
+        self.inner().presign(path, args).await
     }
 
     fn blocking_create(&self, path: &str, args: OpCreate) -> Result<RpCreate> {
@@ -269,8 +269,8 @@ impl<L: LayeredAccessor> Accessor for L {
         (self as &L).batch(args).await
     }
 
-    fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
-        (self as &L).presign(path, args)
+    async fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
+        (self as &L).presign(path, args).await
     }
 
     fn blocking_create(&self, path: &str, args: OpCreate) -> Result<RpCreate> {
diff --git a/core/src/services/oss/backend.rs b/core/src/services/oss/backend.rs
index c6276982..713b7572 100644
--- a/core/src/services/oss/backend.rs
+++ b/core/src/services/oss/backend.rs
@@ -536,7 +536,7 @@ impl Accessor for OssBackend {
         ))
     }
 
-    fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
+    async fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
         // We will not send this request out, just for signing.
         let mut req = match args.operation() {
             PresignOperation::Stat(_) => self.oss_head_object_request(path, true)?,
diff --git a/core/src/services/s3/backend.rs b/core/src/services/s3/backend.rs
index e68c376f..52ae138e 100644
--- a/core/src/services/s3/backend.rs
+++ b/core/src/services/s3/backend.rs
@@ -1222,7 +1222,7 @@ impl Accessor for S3Backend {
         ))
     }
 
-    fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
+    async fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
         // We will not send this request out, just for signing.
         let mut req = match args.operation() {
             PresignOperation::Stat(v) => self.s3_head_object_request(path, v.if_none_match())?,
diff --git a/core/src/types/operator/operator.rs b/core/src/types/operator/operator.rs
index 67afac85..ac1dbcf8 100644
--- a/core/src/types/operator/operator.rs
+++ b/core/src/types/operator/operator.rs
@@ -1074,12 +1074,12 @@ impl Operator {
     /// #    Ok(())
     /// # }
     /// ```
-    pub fn presign_stat(&self, path: &str, expire: Duration) -> Result<PresignedRequest> {
+    pub async fn presign_stat(&self, path: &str, expire: Duration) -> Result<PresignedRequest> {
         let path = normalize_path(path);
 
         let op = OpPresign::new(OpStat::new(), expire);
 
-        let rp = self.inner().presign(&path, op)?;
+        let rp = self.inner().presign(&path, op).await?;
         Ok(rp.into_presigned_request())
     }
 
@@ -1109,12 +1109,12 @@ impl Operator {
     /// ```shell
     /// curl "https://s3.amazonaws.com/examplebucket/test.txt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=access_key_id/20130721/us-east-1/s3/aws4_request&X-Amz-Date=20130721T201207Z&X-Amz-Expires=86400&X-Amz-SignedHeaders=host&X-Amz-Signature=<signature-value>" -O /tmp/test.txt
     /// ```
-    pub fn presign_read(&self, path: &str, expire: Duration) -> Result<PresignedRequest> {
+    pub async fn presign_read(&self, path: &str, expire: Duration) -> Result<PresignedRequest> {
         let path = normalize_path(path);
 
         let op = OpPresign::new(OpRead::new(), expire);
 
-        let rp = self.inner().presign(&path, op)?;
+        let rp = self.inner().presign(&path, op).await?;
         Ok(rp.into_presigned_request())
     }
 
@@ -1139,7 +1139,7 @@ impl Operator {
     /// #    Ok(())
     /// # }
     /// ```
-    pub fn presign_read_with(
+    pub async fn presign_read_with(
         &self,
         path: &str,
         op: OpRead,
@@ -1149,7 +1149,7 @@ impl Operator {
 
         let op = OpPresign::new(op, expire);
 
-        let rp = self.inner().presign(&path, op)?;
+        let rp = self.inner().presign(&path, op).await?;
         Ok(rp.into_presigned_request())
     }
 
@@ -1179,8 +1179,8 @@ impl Operator {
     /// ```shell
     /// curl -X PUT "https://s3.amazonaws.com/examplebucket/test.txt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=access_key_id/20130721/us-east-1/s3/aws4_request&X-Amz-Date=20130721T201207Z&X-Amz-Expires=86400&X-Amz-SignedHeaders=host&X-Amz-Signature=<signature-value>" -d "Hello, World!"
     /// ```
-    pub fn presign_write(&self, path: &str, expire: Duration) -> Result<PresignedRequest> {
-        self.presign_write_with(path, OpWrite::new(), expire)
+    pub async fn presign_write(&self, path: &str, expire: Duration) -> Result<PresignedRequest> {
+        self.presign_write_with(path, OpWrite::new(), expire).await
     }
 
     /// Presign an operation for write with option described in OpenDAL [rfc-0661](../../docs/rfcs/0661-path-in-accessor.md)
@@ -1208,7 +1208,7 @@ impl Operator {
     /// #    Ok(())
     /// # }
     /// ```
-    pub fn presign_write_with(
+    pub async fn presign_write_with(
         &self,
         path: &str,
         op: OpWrite,
@@ -1218,7 +1218,7 @@ impl Operator {
 
         let op = OpPresign::new(op, expire);
 
-        let rp = self.inner().presign(&path, op)?;
+        let rp = self.inner().presign(&path, op).await?;
         Ok(rp.into_presigned_request())
     }
 }
diff --git a/core/tests/behavior/presign.rs b/core/tests/behavior/presign.rs
index a6c473e9..188d781c 100644
--- a/core/tests/behavior/presign.rs
+++ b/core/tests/behavior/presign.rs
@@ -84,7 +84,7 @@ pub async fn test_presign_write(op: Operator) -> Result<()> {
     debug!("Generate a random file: {}", &path);
     let (content, size) = gen_bytes();
 
-    let signed_req = op.presign_write(&path, Duration::hours(1))?;
+    let signed_req = op.presign_write(&path, Duration::hours(1)).await?;
     debug!("Generated request: {signed_req:?}");
 
     let client = reqwest::Client::new();
@@ -118,7 +118,7 @@ pub async fn test_presign_stat(op: Operator) -> Result<()> {
     op.write(&path, content.clone())
         .await
         .expect("write must succeed");
-    let signed_req = op.presign_stat(&path, Duration::hours(1))?;
+    let signed_req = op.presign_stat(&path, Duration::hours(1)).await?;
     debug!("Generated request: {signed_req:?}");
     let client = reqwest::Client::new();
     let mut req = client.request(
@@ -150,7 +150,7 @@ pub async fn test_presign_read(op: Operator) -> Result<()> {
         .await
         .expect("write must succeed");
 
-    let signed_req = op.presign_read(&path, Duration::hours(1))?;
+    let signed_req = op.presign_read(&path, Duration::hours(1)).await?;
     debug!("Generated request: {signed_req:?}");
 
     let client = reqwest::Client::new();