Skip to content

Commit

Permalink
feat: add copy api for lakefs service. (#5114)
Browse files Browse the repository at this point in the history
* 1

* 1
  • Loading branch information
liugddx committed Sep 19, 2024
1 parent f9f31f3 commit 465e17c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
12 changes: 12 additions & 0 deletions core/src/services/lakefs/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ impl Access for LakefsBackend {
read: true,
write: true,
delete: true,
copy: true,
..Default::default()
});
am.into()
Expand Down Expand Up @@ -303,4 +304,15 @@ impl Access for LakefsBackend {
_ => Err(parse_error(resp).await?),
}
}

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

let status = resp.status();

match status {
StatusCode::CREATED => Ok(RpCopy::default()),
_ => Err(parse_error(resp).await?),
}
}
}
35 changes: 33 additions & 2 deletions core/src/services/lakefs/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
// specific language governing permissions and limitations
// under the License.

use std::fmt::Debug;

use http::header;
use http::Request;
use http::Response;
use serde::Deserialize;
use std::collections::HashMap;
use std::fmt::Debug;

use crate::raw::*;
use crate::*;
Expand Down Expand Up @@ -194,6 +194,37 @@ impl LakefsCore {

self.client.send(req).await
}

pub async fn copy_object(&self, path: &str, dest: &str) -> Result<Response<Buffer>> {
let p = build_abs_path(&self.root, path)
.trim_end_matches('/')
.to_string();
let d = build_abs_path(&self.root, dest)
.trim_end_matches('/')
.to_string();

let url = format!(
"{}/api/v1/repositories/{}/branches/{}/objects/copy?dest_path={}",
self.endpoint,
self.repository,
self.branch,
percent_encode_path(&d)
);

let mut req = Request::post(&url);

let auth_header_content = format_authorization_by_basic(&self.username, &self.password)?;
req = req.header(header::AUTHORIZATION, auth_header_content);
req = req.header(header::CONTENT_TYPE, "application/json");
let mut map = HashMap::new();
map.insert("src_path", p);

let req = req
.body(serde_json::to_vec(&map).unwrap().into())
.map_err(new_request_build_error)?;

self.client.send(req).await
}
}

#[derive(Deserialize, Eq, PartialEq, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion core/src/services/lakefs/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This service can be used to:
- [x] write
- [ ] create_dir
- [x] delete
- [ ] copy
- [x] copy
- [ ] rename
- [x] list
- [ ] ~~presign~~
Expand Down

0 comments on commit 465e17c

Please sign in to comment.