Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add if-match support for obs #2023

Merged
merged 3 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, AsyncBody::Empty, None)?;

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 @@ -110,13 +116,18 @@ impl ObsCore {
size: Option<usize>,
content_type: Option<&str>,
body: AsyncBody,
if_match: Option<&str>,
haohuaijin marked this conversation as resolved.
Show resolved Hide resolved
) -> Result<Request<AsyncBody>> {
let p = build_abs_path(&self.root, path);

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

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 @@ -48,6 +48,7 @@ impl oio::Write for ObsWriter {
Some(bs.len()),
self.op.content_type(),
AsyncBody::Bytes(bs),
self.op.if_match(),
)?;

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