Skip to content

Commit

Permalink
feat:add if_none_match for write
Browse files Browse the repository at this point in the history
Signed-off-by: LeeHao <HaozzaLi@gmail.com>
  • Loading branch information
ForestLH committed Sep 19, 2024
1 parent 465e17c commit 1454439
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
12 changes: 12 additions & 0 deletions core/src/raw/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@ pub struct OpWrite {
content_disposition: Option<String>,
cache_control: Option<String>,
executor: Option<Executor>,
if_none_match: Option<String>,
user_metadata: Option<HashMap<String, String>>,
}

Expand Down Expand Up @@ -666,6 +667,17 @@ impl OpWrite {
self
}

/// Set the If-None-Match of the option
pub fn with_if_none_match(mut self, s: &str) -> Self {
self.if_none_match = Some(s.to_string());
self
}

/// Get If-None-Match from option
pub fn if_none_match(&self) -> Option<&str> {
self.if_none_match.as_deref()
}

/// Merge given executor into option.
///
/// If executor has already been set, this will do nothing.
Expand Down
4 changes: 4 additions & 0 deletions core/src/services/s3/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,10 @@ impl S3Core {
req = self.insert_checksum_header(req, &checksum);
}

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

// Set body
let req = req.body(body).map_err(new_request_build_error)?;

Expand Down
22 changes: 21 additions & 1 deletion core/src/types/operator/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,26 @@ impl Operator {
/// # }
/// ```
///
/// ## `if_none_match`
///
/// Set `if_none_match` for this `write` request.
///
/// This feature can be used to check if the file already exists.
/// This prevents overwriting of existing objects with identical key names.
/// And must use the * (asterisk) value with this parameter
///
/// If file exists, an error with kind [`ErrorKind::ConditionNotMatch`] will be returned.
///
/// ```no_run
/// # use opendal::Result;
/// use opendal::Operator;
/// # async fn test(op: Operator, etag: &str) -> Result<()> {
/// let bs = b"hello, world!".to_vec();
/// let mut metadata = op.write_with("path/to/file", bs).if_none_match("*").await?;
/// # Ok(())
/// # }
/// ```
///
/// # Examples
///
/// ```
Expand Down Expand Up @@ -1244,7 +1264,7 @@ impl Operator {
}

let context = WriteContext::new(inner, path, args, options);
let mut w = Writer::new(context).await?;
let mut w = Writer::new(context).await?; // 这个函数就会调用S3的write函数
w.write(bs).await?;
w.close().await?;
Ok(())
Expand Down
5 changes: 5 additions & 0 deletions core/src/types/operator/operator_futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,11 @@ impl<F: Future<Output = Result<()>>> FutureWrite<F> {
self.map(|(args, options, bs)| (args.with_executor(executor), options, bs))
}

/// Set the If-None-Match for this operation.
pub fn if_none_match(self, s: &str) -> Self {
self.map(|(args, options, bs)| (args.with_if_none_match(s), options, bs))
}

/// Set the user defined metadata of the op
///
/// ## Notes
Expand Down

0 comments on commit 1454439

Please sign in to comment.