Skip to content

Commit

Permalink
feat(services/s3): Add if-match support (#2033)
Browse files Browse the repository at this point in the history
* feat: s3: Add if-match support #1972

* feat: s3: Add if-match support #1972

---------

Co-authored-by: leenstx • <[email protected]>
  • Loading branch information
leenstx and leenstx • authored Apr 18, 2023
1 parent 10be774 commit e3fb7b0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
12 changes: 9 additions & 3 deletions core/src/services/s3/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ impl Accessor for S3Backend {
async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
let resp = self
.core
.s3_get_object(path, args.range(), args.if_none_match())
.s3_get_object(path, args.range(), args.if_none_match(), args.if_match())
.await?;

let status = resp.status();
Expand All @@ -967,6 +967,7 @@ impl Accessor for S3Backend {
args.content_type(),
args.content_disposition(),
args.cache_control(),
args.if_match(),
)
.await?;

Expand Down Expand Up @@ -1017,7 +1018,10 @@ impl Accessor for S3Backend {
return Ok(RpStat::new(Metadata::new(EntryMode::DIR)));
}

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

let status = resp.status();

Expand Down Expand Up @@ -1059,14 +1063,16 @@ impl Accessor for S3Backend {
// We will not send this request out, just for signing.
let mut req = match args.operation() {
PresignOperation::Stat(v) => {
self.core.s3_head_object_request(path, v.if_none_match())?
self.core
.s3_head_object_request(path, v.if_none_match(), v.if_match())?
}
PresignOperation::Read(v) => self.core.s3_get_object_request(
path,
v.range(),
v.override_content_disposition(),
v.override_cache_control(),
v.if_none_match(),
v.if_match(),
)?,
PresignOperation::Write(_) => {
self.core
Expand Down
22 changes: 20 additions & 2 deletions core/src/services/s3/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use http::header::CACHE_CONTROL;
use http::header::CONTENT_DISPOSITION;
use http::header::CONTENT_LENGTH;
use http::header::CONTENT_TYPE;
use http::header::IF_MATCH;
use http::header::IF_NONE_MATCH;
use http::HeaderValue;
use http::Request;
Expand Down Expand Up @@ -206,6 +207,7 @@ impl S3Core {
&self,
path: &str,
if_none_match: Option<&str>,
if_match: Option<&str>,
) -> Result<Request<AsyncBody>> {
let p = build_abs_path(&self.root, path);

Expand All @@ -219,6 +221,10 @@ impl S3Core {
req = req.header(IF_NONE_MATCH, if_none_match);
}

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

let req = req
.body(AsyncBody::Empty)
.map_err(new_request_build_error)?;
Expand All @@ -233,6 +239,7 @@ impl S3Core {
override_content_disposition: Option<&str>,
override_cache_control: Option<&str>,
if_none_match: Option<&str>,
if_match: Option<&str>,
) -> Result<Request<AsyncBody>> {
let p = build_abs_path(&self.root, path);

Expand Down Expand Up @@ -269,6 +276,9 @@ impl S3Core {
req = req.header(IF_NONE_MATCH, if_none_match);
}

if let Some(if_match) = if_match {
req = req.header(IF_MATCH, if_match);
}
// Set SSE headers.
// TODO: how will this work with presign?
req = self.insert_sse_headers(req, false);
Expand All @@ -285,8 +295,10 @@ impl S3Core {
path: &str,
range: BytesRange,
if_none_match: Option<&str>,
if_match: Option<&str>,
) -> Result<Response<IncomingAsyncBody>> {
let mut req = self.s3_get_object_request(path, range, None, None, if_none_match)?;
let mut req =
self.s3_get_object_request(path, range, None, None, if_none_match, if_match)?;

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

Expand Down Expand Up @@ -342,8 +354,9 @@ impl S3Core {
&self,
path: &str,
if_none_match: Option<&str>,
if_match: Option<&str>,
) -> Result<Response<IncomingAsyncBody>> {
let mut req = self.s3_head_object_request(path, if_none_match)?;
let mut req = self.s3_head_object_request(path, if_none_match, if_match)?;

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

Expand Down Expand Up @@ -476,6 +489,7 @@ impl S3Core {
content_type: Option<&str>,
content_disposition: Option<&str>,
cache_control: Option<&str>,
if_match: Option<&str>,
) -> Result<Response<IncomingAsyncBody>> {
let p = build_abs_path(&self.root, path);

Expand All @@ -495,6 +509,10 @@ impl S3Core {
req = req.header(CACHE_CONTROL, cache_control)
}

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

// Set storage class header
if let Some(v) = &self.default_storage_class {
req = req.header(HeaderName::from_static(constants::X_AMZ_STORAGE_CLASS), v);
Expand Down

0 comments on commit e3fb7b0

Please sign in to comment.