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(core): add presign support for obs #2253

Merged
merged 2 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 38 additions & 1 deletion core/src/services/obs/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use crate::*;
/// - [ ] rename
/// - [x] list
/// - [x] scan
/// - [ ] presign
/// - [x] presign
/// - [ ] blocking
///
/// # Configuration
Expand Down Expand Up @@ -332,12 +332,49 @@ impl Accessor for ObsBackend {
list_with_delimiter_slash: true,
list_without_delimiter: true,

presign: true,
presign_stat: true,
presign_read: true,
presign_write: true,

..Default::default()
});

am
}

async fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
let mut req = match args.operation() {
PresignOperation::Stat(v) => {
self.core
.obs_get_head_object_request(path, v.if_match(), v.if_none_match())?
}
PresignOperation::Read(v) => self.core.obs_get_object_request(
path,
v.range(),
v.if_match(),
v.if_none_match(),
)?,
PresignOperation::Write(v) => self.core.obs_put_object_request(
path,
None,
v.content_type(),
v.cache_control(),
AsyncBody::Empty,
)?,
};
self.core.sign_query(&mut req, args.expire()).await?;

// We don't need this request anymore, consume it directly.
let (parts, _) = req.into_parts();

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

async fn create_dir(&self, path: &str, _: OpCreateDir) -> Result<RpCreateDir> {
let mut req =
self.core
Expand Down
75 changes: 75 additions & 0 deletions core/src/services/obs/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use std::fmt::Debug;
use std::fmt::Formatter;
use std::time::Duration;

use http::header::CACHE_CONTROL;
use http::header::CONTENT_LENGTH;
Expand Down Expand Up @@ -118,6 +119,38 @@ impl ObsCore {
self.send(req).await
}

pub fn obs_get_object_request(
suyanhanx marked this conversation as resolved.
Show resolved Hide resolved
&self,
path: &str,
range: BytesRange,
if_match: Option<&str>,
if_none_match: Option<&str>,
) -> Result<Request<AsyncBody>> {
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())
}

if let Some(if_none_match) = if_none_match {
req = req.header(IF_NONE_MATCH, if_none_match);
}

let req = req
.body(AsyncBody::Empty)
.map_err(new_request_build_error)?;

Ok(req)
}

pub fn obs_put_object_request(
&self,
path: &str,
Expand Down Expand Up @@ -180,6 +213,36 @@ impl ObsCore {
self.send(req).await
}

pub fn obs_get_head_object_request(
suyanhanx marked this conversation as resolved.
Show resolved Hide resolved
&self,
path: &str,
if_match: Option<&str>,
if_none_match: Option<&str>,
) -> Result<Request<AsyncBody>> {
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 mut req = Request::head(&url);

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

if let Some(if_none_match) = if_none_match {
req = req.header(IF_NONE_MATCH, if_none_match);
}

let req = req
.body(AsyncBody::Empty)
.map_err(new_request_build_error)?;

Ok(req)
}

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

Expand Down Expand Up @@ -254,4 +317,16 @@ impl ObsCore {

self.send(req).await
}

pub async fn sign_query<T>(&self, req: &mut Request<T>, duration: Duration) -> Result<()> {
suyanhanx marked this conversation as resolved.
Show resolved Hide resolved
let cred = if let Some(cred) = self.load_credential().await? {
cred
} else {
return Ok(());
};

self.signer
.sign_query(req, duration, &cred)
.map_err(new_request_sign_error)
}
}