Skip to content

Commit

Permalink
feat(services/obs): Add Copy Support (#1876)
Browse files Browse the repository at this point in the history
feat(services/obs): add support copy

Signed-off-by: suyanhanx <suyanhanx@gmail.com>
  • Loading branch information
suyanhanx committed Apr 8, 2023
1 parent 3e42fef commit 3d7aebc
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion core/src/services/obs/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use crate::*;
///
/// - [x] read
/// - [x] write
/// - [x] copy
/// - [x] list
/// - [x] scan
/// - [ ] presign
Expand Down Expand Up @@ -314,7 +315,7 @@ impl Accessor for ObsBackend {
am.set_scheme(Scheme::Obs)
.set_root(&self.root)
.set_name(&self.bucket)
.set_capabilities(Read | Write | List | Scan)
.set_capabilities(Read | Write | Copy | List | Scan)
.set_hints(ReadStreamable);

am
Expand Down Expand Up @@ -366,6 +367,20 @@ impl Accessor for ObsBackend {
))
}

async fn copy(&self, from: &str, to: &str, _args: OpCopy) -> Result<RpCopy> {
let resp = self.obs_copy_object(from, to).await?;

let status = resp.status();

match status {
StatusCode::OK => {
resp.into_body().consume().await?;
Ok(RpCopy::default())
}
_ => Err(parse_error(resp).await?),
}
}

async fn stat(&self, path: &str, _: OpStat) -> Result<RpStat> {
// Stat root always returns a DIR.
if path == "/" {
Expand Down Expand Up @@ -500,6 +515,23 @@ impl ObsBackend {
self.client.send_async(req).await
}

async fn obs_copy_object(&self, from: &str, to: &str) -> Result<Response<IncomingAsyncBody>> {
let source = build_abs_path(&self.root, from);
let target = build_abs_path(&self.root, to);

let source = format!("/{}/{}", self.bucket, percent_encode_path(&source));
let url = format!("{}/{}", self.endpoint, percent_encode_path(&target));

let mut req = Request::put(&url)
.header("x-obs-copy-source", percent_encode_path(&source))
.body(AsyncBody::Empty)
.map_err(new_request_build_error)?;

self.signer.sign(&mut req).map_err(new_request_sign_error)?;

self.client.send_async(req).await
}

pub(crate) async fn obs_list_objects(
&self,
path: &str,
Expand Down

0 comments on commit 3d7aebc

Please sign in to comment.