Skip to content

Commit

Permalink
refactor: Add RpPresign for presign operation
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <[email protected]>
  • Loading branch information
Xuanwo committed Nov 22, 2022
1 parent adea199 commit 625a561
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ pub trait Accessor: Send + Sync + Debug + 'static {
///
/// - Require capability: `Presign`
/// - This API is optional, return [`std::io::ErrorKind::Unsupported`] if not supported.
fn presign(&self, path: &str, args: OpPresign) -> Result<PresignedRequest> {
fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
match self.inner() {
Some(inner) => inner.presign(path, args),
None => Err(Error::new(
Expand Down Expand Up @@ -396,7 +396,7 @@ impl<T: Accessor> Accessor for Arc<T> {
self.as_ref().list(path, args).await
}

fn presign(&self, path: &str, args: OpPresign) -> Result<PresignedRequest> {
fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
self.as_ref().presign(path, args)
}

Expand Down
2 changes: 1 addition & 1 deletion src/layers/concurrent_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl Accessor for ConcurrentLimitAccessor {
.map(|s| set_accessor_for_object_steamer(s, self.clone()))
}

fn presign(&self, path: &str, args: OpPresign) -> Result<PresignedRequest> {
fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
self.inner.presign(path, args)
}

Expand Down
2 changes: 1 addition & 1 deletion src/layers/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ impl Accessor for LoggingAccessor {
})
}

fn presign(&self, path: &str, args: OpPresign) -> Result<PresignedRequest> {
fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
debug!(
target: "opendal::services",
"service={} operation={} path={} -> started",
Expand Down
2 changes: 1 addition & 1 deletion src/layers/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ impl Accessor for MetricsAccessor {
.map(|s| set_accessor_for_object_steamer(s, self.clone()))
}

fn presign(&self, path: &str, args: OpPresign) -> Result<PresignedRequest> {
fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
self.handle.requests_total_presign.increment(1);

let start = Instant::now();
Expand Down
2 changes: 1 addition & 1 deletion src/layers/subdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl Accessor for SubdirAccessor {
)))
}

fn presign(&self, path: &str, args: OpPresign) -> Result<PresignedRequest> {
fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
let path = self.prepend_subdir(path);

self.inner.presign(&path, args)
Expand Down
2 changes: 1 addition & 1 deletion src/layers/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl Accessor for TracingAccessor {
}

#[tracing::instrument(level = "debug", skip(self))]
fn presign(&self, path: &str, args: OpPresign) -> Result<PresignedRequest> {
fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
self.inner.presign(path, args)
}

Expand Down
3 changes: 2 additions & 1 deletion src/object/multipart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ impl ObjectMultipart {
expire,
);

self.acc.presign(&self.path, op)
let rp = self.acc.presign(&self.path, op)?;
Ok(rp.into_presigned_request())
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/object/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1236,7 +1236,8 @@ impl Object {
pub fn presign_read(&self, expire: Duration) -> Result<PresignedRequest> {
let op = OpPresign::new(OpRead::new().into(), expire);

self.acc.presign(self.path(), op)
let rp = self.acc.presign(self.path(), op)?;
Ok(rp.into_presigned_request())
}

/// Presign an operation for write.
Expand Down Expand Up @@ -1266,7 +1267,8 @@ impl Object {
pub fn presign_write(&self, expire: Duration) -> Result<PresignedRequest> {
let op = OpPresign::new(OpWrite::new(0).into(), expire);

self.acc.presign(self.path(), op)
let rp = self.acc.presign(self.path(), op)?;
Ok(rp.into_presigned_request())
}

/// Construct a multipart with existing upload id.
Expand Down
1 change: 1 addition & 0 deletions src/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ mod op_presign;
pub use op_presign::OpPresign;
pub use op_presign::PresignOperation;
pub use op_presign::PresignedRequest;
pub use op_presign::RpPresign;
mod op_read;
pub use op_read::OpRead;
pub use op_read::RpRead;
Expand Down
18 changes: 18 additions & 0 deletions src/ops/op_presign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,24 @@ impl From<OpWriteMultipart> for PresignOperation {
}
}

/// Reply for `presign` operation.
#[derive(Debug, Clone)]
pub struct RpPresign {
req: PresignedRequest,
}

impl RpPresign {
/// Create a new reply for presign.
pub fn new(req: PresignedRequest) -> Self {
RpPresign { req }
}

/// Consume reply to build a presigned request.
pub fn into_presigned_request(self) -> PresignedRequest {
self.req
}
}

/// PresignedRequest is a presigned request return by `presign`.
#[derive(Debug, Clone)]
pub struct PresignedRequest {
Expand Down
6 changes: 3 additions & 3 deletions src/services/s3/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ impl Accessor for Backend {
))))
}

fn presign(&self, path: &str, args: OpPresign) -> Result<PresignedRequest> {
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::Read(v) => self.get_object_request(path, v.range())?,
Expand All @@ -927,11 +927,11 @@ impl Accessor for Backend {
// We don't need this request anymore, consume it directly.
let (parts, _) = req.into_parts();

Ok(PresignedRequest::new(
Ok(RpPresign::new(PresignedRequest::new(
parts.method,
parts.uri,
parts.headers,
))
)))
}

async fn create_multipart(&self, path: &str, _: OpCreateMultipart) -> Result<String> {
Expand Down
2 changes: 1 addition & 1 deletion src/wrappers/error_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl<T: Accessor + 'static> Accessor for ErrorContextWrapper<T> {
})
}

fn presign(&self, path: &str, args: OpPresign) -> Result<PresignedRequest> {
fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
self.inner.presign(path, args).map_err(|err| {
err.with_operation(Operation::Presign.into_static())
.with_context("service", self.meta.scheme())
Expand Down

1 comment on commit 625a561

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for opendal ready!

✅ Preview
https://opendal-jhy42fb0p-databend.vercel.app
https://opendal-git-rp-presign.vercel.app

Built with commit 625a561.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.