You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by xu...@apache.org on 2025/02/24 05:06:19 UTC
(opendal) 01/01: feat(bindings/python): Add user metadata support for write
This is an automated email from the ASF dual-hosted git repository.
xuanwo pushed a commit to branch add-user-metadata
in repository https://gitbox.apache.org/repos/asf/opendal.git
commit 45a97651d0d2c531676e659dff2e875589c5948d
Author: Xuanwo <gi...@xuanwo.io>
AuthorDate: Mon Feb 24 13:05:59 2025 +0800
feat(bindings/python): Add user metadata support for write
Signed-off-by: Xuanwo <gi...@xuanwo.io>
---
bindings/python/src/operator.rs | 11 +++++++++--
bindings/python/src/options.rs | 2 ++
core/src/types/operator/operator_functions.rs | 9 +++++++++
3 files changed, 20 insertions(+), 2 deletions(-)
diff --git a/bindings/python/src/operator.rs b/bindings/python/src/operator.rs
index 910a3e7ce..e9a3598e3 100644
--- a/bindings/python/src/operator.rs
+++ b/bindings/python/src/operator.rs
@@ -121,7 +121,7 @@ impl Operator {
#[pyo3(signature = (path, bs, **kwargs))]
pub fn write(&self, path: PathBuf, bs: Vec<u8>, kwargs: Option<WriteOptions>) -> PyResult<()> {
let path = path.to_string_lossy().to_string();
- let kwargs = kwargs.unwrap_or_default();
+ let mut kwargs = kwargs.unwrap_or_default();
let mut write = self
.core
.write_with(&path, bs)
@@ -138,6 +138,9 @@ impl Operator {
if let Some(cache_control) = &kwargs.cache_control {
write = write.cache_control(cache_control);
}
+ if let Some(user_metadata) = kwargs.user_metadata.take() {
+ write = write.user_metadata(user_metadata);
+ }
write.call().map(|_| ()).map_err(format_pyerr)
}
@@ -358,7 +361,7 @@ impl AsyncOperator {
bs: &Bound<PyBytes>,
kwargs: Option<WriteOptions>,
) -> PyResult<Bound<'p, PyAny>> {
- let kwargs = kwargs.unwrap_or_default();
+ let mut kwargs = kwargs.unwrap_or_default();
let this = self.core.clone();
let bs = bs.as_bytes().to_vec();
let path = path.to_string_lossy().to_string();
@@ -378,6 +381,10 @@ impl AsyncOperator {
if let Some(cache_control) = &kwargs.cache_control {
write = write.cache_control(cache_control);
}
+ if let Some(user_metadata) = kwargs.user_metadata.take() {
+ write = write.user_metadata(user_metadata);
+ }
+
write.await.map(|_| ()).map_err(format_pyerr)
})
}
diff --git a/bindings/python/src/options.rs b/bindings/python/src/options.rs
index 16e6a4640..aabee7833 100644
--- a/bindings/python/src/options.rs
+++ b/bindings/python/src/options.rs
@@ -17,6 +17,7 @@
use dict_derive::FromPyObject;
use pyo3::pyclass;
+use std::collections::HashMap;
#[pyclass(module = "opendal")]
#[derive(FromPyObject, Default)]
@@ -26,4 +27,5 @@ pub struct WriteOptions {
pub content_type: Option<String>,
pub content_disposition: Option<String>,
pub cache_control: Option<String>,
+ pub user_metadata: Option<HashMap<String, String>>,
}
diff --git a/core/src/types/operator/operator_functions.rs b/core/src/types/operator/operator_functions.rs
index b856e83eb..e21be9a2e 100644
--- a/core/src/types/operator/operator_functions.rs
+++ b/core/src/types/operator/operator_functions.rs
@@ -19,6 +19,7 @@
//!
//! By using functions, users can add more options for operation.
+use std::collections::HashMap;
use std::ops::RangeBounds;
use crate::raw::*;
@@ -133,6 +134,14 @@ impl FunctionWrite {
self
}
+ /// Sets user metadata for this write request.
+ pub fn user_metadata(mut self, v: impl IntoIterator<Item = (String, String)>) -> Self {
+ self.0 = self.0.map_args(|(args, options, bs)| {
+ (args.with_user_metadata(HashMap::from_iter(v)), options, bs)
+ });
+ self
+ }
+
/// Call the function to consume all the input and generate a
/// result.
pub fn call(self) -> Result<Metadata> {