Skip to content

Commit

Permalink
feat: add if-match support for obs (#2023)
Browse files Browse the repository at this point in the history
* feat: add if-match support for obs

* chore: change parameters localtion
  • Loading branch information
haohuaijin committed Apr 17, 2023
1 parent 1006691 commit 643ddb8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
15 changes: 9 additions & 6 deletions core/src/services/obs/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,9 @@ impl Accessor for ObsBackend {
}

async fn create_dir(&self, path: &str, _: OpCreate) -> Result<RpCreate> {
let mut req = self
.core
.obs_put_object_request(path, Some(0), None, AsyncBody::Empty)?;
let mut req =
self.core
.obs_put_object_request(path, Some(0), None, None, AsyncBody::Empty)?;

self.core.sign(&mut req).await?;

Expand All @@ -335,7 +335,10 @@ impl Accessor for ObsBackend {
}

async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
let resp = self.core.obs_get_object(path, args.range()).await?;
let resp = self
.core
.obs_get_object(path, args.range(), args.if_match())
.await?;

let status = resp.status();

Expand Down Expand Up @@ -376,13 +379,13 @@ impl Accessor for ObsBackend {
}
}

async fn stat(&self, path: &str, _: OpStat) -> Result<RpStat> {
async fn stat(&self, path: &str, args: OpStat) -> Result<RpStat> {
// Stat root always returns a DIR.
if path == "/" {
return Ok(RpStat::new(Metadata::new(EntryMode::DIR)));
}

let resp = self.core.obs_get_head_object(path).await?;
let resp = self.core.obs_get_head_object(path, args.if_match()).await?;

let status = resp.status();

Expand Down
23 changes: 21 additions & 2 deletions core/src/services/obs/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::raw::*;
use crate::*;
use http::header::CONTENT_LENGTH;
use http::header::CONTENT_TYPE;
use http::header::IF_MATCH;
use http::Request;
use http::Response;
use reqsign::HuaweicloudObsCredential;
Expand Down Expand Up @@ -84,13 +85,18 @@ impl ObsCore {
&self,
path: &str,
range: BytesRange,
if_match: Option<&str>,
) -> Result<Response<IncomingAsyncBody>> {
let p = build_abs_path(&self.root, path);

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

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

if let Some(if_match) = if_match {
req = req.header(IF_MATCH, if_match);
}

if !range.is_full() {
req = req.header(http::header::RANGE, range.to_header())
}
Expand All @@ -109,6 +115,7 @@ impl ObsCore {
path: &str,
size: Option<usize>,
content_type: Option<&str>,
if_match: Option<&str>,
body: AsyncBody,
) -> Result<Request<AsyncBody>> {
let p = build_abs_path(&self.root, path);
Expand All @@ -117,6 +124,10 @@ impl ObsCore {

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

if let Some(if_match) = if_match {
req = req.header(IF_MATCH, if_match);
}

if let Some(size) = size {
req = req.header(CONTENT_LENGTH, size)
}
Expand All @@ -130,15 +141,23 @@ impl ObsCore {
Ok(req)
}

pub async fn obs_get_head_object(&self, path: &str) -> Result<Response<IncomingAsyncBody>> {
pub async fn obs_get_head_object(
&self,
path: &str,
if_match: Option<&str>,
) -> Result<Response<IncomingAsyncBody>> {
let p = build_abs_path(&self.root, path);

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

// The header 'Origin' is optional for API calling, the doc has mistake, confirmed with customer service of huaweicloud.
// https://support.huaweicloud.com/intl/en-us/api-obs/obs_04_0084.html

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

if let Some(if_match) = if_match {
req = req.header(IF_MATCH, if_match);
}

let mut req = req
.body(AsyncBody::Empty)
Expand Down
1 change: 1 addition & 0 deletions core/src/services/obs/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl oio::Write for ObsWriter {
&self.path,
Some(bs.len()),
self.op.content_type(),
self.op.if_match(),
AsyncBody::Bytes(bs),
)?;

Expand Down

0 comments on commit 643ddb8

Please sign in to comment.