This is an automated email from the ASF dual-hosted git repository.
Xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opendal.git
The following commit(s) were added to refs/heads/main by this push:
new 69e43d3780 fix(services/b2): preserve presigned upload properties
(#9307)
69e43d3780 is described below
commit 69e43d3780b7eacc478b493dd05d793d65ad0a7d
Author: Xuanwo <[email protected]>
AuthorDate: Sat Nov 28 05:19:18 2026 +0800
fix(services/b2): preserve presigned upload properties (#9307)
Use the dedicated options for their native upload fields and keep
separate custom file info. Expose the stored Content-Disposition in
file metadata using the value returned by B2.
---
core/services/b2/src/backend.rs | 70 +++++++++++++++------
core/services/b2/src/core.rs | 20 ++++--
core/services/b2/src/docs.md | 18 ++++--
core/tests/behavior/async_presign.rs | 115 ++++++++++++++++++++++++-----------
4 files changed, 159 insertions(+), 64 deletions(-)
diff --git a/core/services/b2/src/backend.rs b/core/services/b2/src/backend.rs
index 8a108a6a0d..8470ce4bb9 100644
--- a/core/services/b2/src/backend.rs
+++ b/core/services/b2/src/backend.rs
@@ -596,8 +596,6 @@ impl Service for B2Backend {
.await
}
PresignOperation::Write(write) => {
- const CACHE_CONTROL_FILE_INFO_KEY: &str = "b2-cache-control";
-
if write.checksum().is_some() {
return Err(Error::new(
ErrorKind::Unsupported,
@@ -617,23 +615,38 @@ impl Service for B2Backend {
req = req.header(http::header::CONTENT_TYPE, content_type);
req = req.header(constants::X_BZ_CONTENT_SHA1,
"do_not_verify");
- let cache_control = write.cache_control();
- if let Some(value) = cache_control {
- req = req.header(
- format!(
- "{}{CACHE_CONTROL_FILE_INFO_KEY}",
- constants::X_BZ_INFO_PREFIX
- ),
- percent_encode_path(value),
- );
+ let file_info_options = [
+ (
+ constants::CACHE_CONTROL_FILE_INFO_KEY,
+ write.cache_control(),
+ ),
+ (
+ constants::CONTENT_DISPOSITION_FILE_INFO_KEY,
+ write.content_disposition(),
+ ),
+ (
+ constants::CONTENT_ENCODING_FILE_INFO_KEY,
+ write.content_encoding(),
+ ),
+ (
+ constants::CONTENT_LANGUAGE_FILE_INFO_KEY,
+ write.content_language(),
+ ),
+ ];
+ for (key, value) in file_info_options {
+ if let Some(value) = value {
+ req = req.header(
+ format!("{}{key}", constants::X_BZ_INFO_PREFIX),
+ percent_encode_path(value),
+ );
+ }
}
if let Some(user_metadata) = write.user_metadata() {
for (key, value) in user_metadata {
- if cache_control.is_some()
- && (key.eq_ignore_ascii_case("cache-control")
- ||
key.eq_ignore_ascii_case(CACHE_CONTROL_FILE_INFO_KEY))
- {
+ if file_info_options.iter().any(|(reserved_key,
option)| {
+ option.is_some() &&
key.eq_ignore_ascii_case(reserved_key)
+ }) {
continue;
}
req = req.header(
@@ -755,7 +768,7 @@ mod tests {
r#"{"files":[{"fileName":"root/dir/","contentLength":0}]}"#
} else {
assert!(query.contains("prefix=root/file"));
-
r#"{"files":[{"fileName":"root/file","fileId":"new","contentLength":13}]}"#
+
r#"{"files":[{"fileName":"root/file","fileId":"new","contentLength":13,"fileInfo":{"content-disposition":"inline"}}]}"#
};
(StatusCode::OK, body)
}
@@ -792,7 +805,14 @@ mod tests {
async fn test_version_stat_preserves_metadata_from_selected_file() ->
Result<()> {
let op = stat_operator(
StatusCode::OK,
-
r#"{"bucketId":"bucket-id","fileName":"root/file","fileId":"old","action":"upload","contentLength":5,"contentType":"text/plain","contentMd5":"md5","fileInfo":{"Owner":"a%2Bb"}}"#,
+ r#"{
+
"bucketId":"bucket-id","fileName":"root/file","fileId":"old","action":"upload",
+
"contentLength":5,"contentType":"text/plain","contentMd5":"md5",
+ "fileInfo":{
+ "Owner":"a%2Bb",
+ "b2-content-disposition":"attachment; filename=\"OpenDAL
%20 + probe.txt\""
+ }
+ }"#,
)?;
assert!(op.info().capability().stat_with_version);
let old = op.stat_with("file").version("old").await?;
@@ -801,11 +821,25 @@ mod tests {
assert_eq!(old.content_length(), 5);
assert_eq!(old.content_type(), Some("text/plain"));
assert_eq!(old.content_md5(), Some("md5"));
- assert_eq!(old.user_metadata().unwrap().get("owner"), Some("a+b"));
+ assert_eq!(
+ old.content_disposition(),
+ Some("attachment; filename=\"OpenDAL %20 + probe.txt\"")
+ );
+ let user_metadata = old.user_metadata().unwrap();
+ assert_eq!(user_metadata.get("owner"), Some("a+b"));
+ assert_eq!(
+ user_metadata.get("b2-content-disposition"),
+ Some("attachment; filename=\"OpenDAL + probe.txt\"")
+ );
let current = op.stat("file").await?;
assert_eq!(current.version(), Some("new"));
assert_eq!(current.content_length(), 13);
+ assert_eq!(current.content_disposition(), None);
+ assert_eq!(
+ current.user_metadata().unwrap().get("content-disposition"),
+ Some("inline")
+ );
let op = stat_operator(
StatusCode::OK,
diff --git a/core/services/b2/src/core.rs b/core/services/b2/src/core.rs
index fea932b796..d69a218f91 100644
--- a/core/services/b2/src/core.rs
+++ b/core/services/b2/src/core.rs
@@ -41,6 +41,10 @@ pub(super) mod constants {
pub const X_BZ_CONTENT_SHA1: &str = "X-Bz-Content-Sha1";
pub const X_BZ_PART_NUMBER: &str = "X-Bz-Part-Number";
pub const X_BZ_INFO_PREFIX: &str = "X-Bz-Info-";
+ pub const CACHE_CONTROL_FILE_INFO_KEY: &str = "b2-cache-control";
+ pub const CONTENT_DISPOSITION_FILE_INFO_KEY: &str =
"b2-content-disposition";
+ pub const CONTENT_ENCODING_FILE_INFO_KEY: &str = "b2-content-encoding";
+ pub const CONTENT_LANGUAGE_FILE_INFO_KEY: &str = "b2-content-language";
}
/// Core of [b2](https://www.backblaze.com/cloud-storage) services support.
@@ -996,9 +1000,8 @@ pub struct File {
pub content_md5: Option<String>,
pub content_type: Option<String>,
pub file_name: String,
- /// Custom file info (user metadata) stored with the file.
- /// Keys are the original names without the `X-Bz-Info-` prefix.
- /// Values are URL-encoded when stored, decoded when read.
+ /// File info keys are lowercase and omit the `X-Bz-Info-` prefix.
+ /// B2 decodes the percent-encoded upload values before returning them in
JSON.
#[serde(default)]
pub file_info: HashMap<String, String>,
}
@@ -1022,9 +1025,14 @@ pub(super) fn parse_file_info(file: &File) ->
MetadataBuilder {
metadata.content_type(content_type);
}
- // Parse user metadata from file_info
- // B2 stores user metadata with keys stripped of the "X-Bz-Info-" prefix
- // and values are URL-encoded
+ if let Some(content_disposition) = file
+ .file_info
+ .get(constants::CONTENT_DISPOSITION_FILE_INFO_KEY)
+ {
+ metadata.content_disposition(content_disposition);
+ }
+
+ // Preserve the legacy user metadata decoding for existing callers.
if !file.file_info.is_empty() {
let user_metadata: HashMap<String, String> = file
.file_info
diff --git a/core/services/b2/src/docs.md b/core/services/b2/src/docs.md
index 9fab02790c..1c1b6827e4 100644
--- a/core/services/b2/src/docs.md
+++ b/core/services/b2/src/docs.md
@@ -39,7 +39,9 @@ but the response fails to confirm the digest, the object may
already be visible.
that file. The application key needs `readFiles` permission. A version that
has been permanently deleted, belongs to a different path, or is a hide marker
returns `NotFound`. Ordinary `stat` and `list` expose current file IDs through
-`Metadata::version()`.
+`Metadata::version()`. Current and versioned stat and list results also expose
+the stored `Content-Disposition` in `Metadata::content_disposition()` when B2
+supplies its reserved file info field.
## File version listing
@@ -78,11 +80,15 @@ method, URL, and headers to execute the request.
## Presigned uploads
-`presign_write_options` returns a single-object POST with `user_metadata` in
-`X-Bz-Info-*` headers. When `WriteOptions::cache_control` is set, OpenDAL uses
-its value for `X-Bz-Info-b2-cache-control` to set the stored Cache-Control
policy.
-In that case, OpenDAL skips the `cache-control` and `b2-cache-control`
-metadata keys with case-insensitive matching.
+`presign_write_options` returns a single-object POST. It uses `content_type`
+for `Content-Type` and defaults to `b2/x-auto`. The `cache_control`,
+`content_disposition`, `content_encoding`, and `content_language` options
+use reserved B2 file-info headers.
+
+OpenDAL sends `user_metadata` as `X-Bz-Info-*` headers. A dedicated option
+takes precedence over entries that map to the same native header, matching
+header names case-insensitively. Other entries are preserved, including
+`content-type` metadata in its own `X-Bz-Info-Content-Type` header.
## Restore
diff --git a/core/tests/behavior/async_presign.rs
b/core/tests/behavior/async_presign.rs
index 0282a515c0..bfe85ae2fb 100644
--- a/core/tests/behavior/async_presign.rs
+++ b/core/tests/behavior/async_presign.rs
@@ -159,28 +159,41 @@ async fn test_presign_write_with_metadata(op: Operator)
-> Result<()> {
}
async fn test_presign_write_b2_headers(op: Operator) -> Result<()> {
- let reserved = "x-bz-info-b2-cache-control";
- let metadata_cache_control = "x-bz-info-cache-control";
- let unrelated = "x-bz-info-application-validation";
- let unrelated_value = "b2%20trial%20%2B%20caf%C3%A9%20%25";
+ fn assert_header_values(headers: &http::HeaderMap, name: &str, expected:
&[&str]) {
+ let mut values: Vec<_> = headers
+ .get_all(name)
+ .iter()
+ .map(|value| value.to_str().unwrap())
+ .collect();
+ values.sort_unstable();
+ assert_eq!(values, expected, "{name}");
+ }
+
let path = uuid::Uuid::new_v4().to_string();
let expire = Duration::from_secs(60);
let default_request = op.presign_write(&path, expire).await?;
- let headers = default_request.header();
- assert_eq!(headers[header::CONTENT_TYPE], "b2/x-auto");
- assert!(!headers.contains_key(reserved));
- assert!(!headers.contains_key(metadata_cache_control));
- assert!(!headers.contains_key(header::CACHE_CONTROL));
+ assert_header_values(default_request.header(), "content-type",
&["b2/x-auto"]);
+ assert!(
+ !default_request
+ .header()
+ .contains_key("x-bz-info-cache-control")
+ );
let mut options = options::WriteOptions {
- content_type: Some("application/json".to_owned()),
user_metadata: Some(
[
- ("application-validation", "b2 trial + café %"),
+ ("Application-Validation", "b2 trial + café %"),
("cache-control", "no-cache"),
("Cache-Control", "no-store"),
("b2-cache-control", "max-age=30"),
("B2-Cache-Control", "max-age=60"),
+ ("B2-Content-Disposition", "inline"),
+ ("Content-Disposition", "attachment"),
+ ("B2-Content-Encoding", "identity"),
+ ("Content-Encoding", "br"),
+ ("B2-Content-Language", "de"),
+ ("Content-Language", "es"),
+ ("CoNtEnT-TyPe", "text/plain"),
]
.map(|(key, value)| (key.to_owned(), value.to_owned()))
.into(),
@@ -190,31 +203,65 @@ async fn test_presign_write_b2_headers(op: Operator) ->
Result<()> {
let metadata_only = op
.presign_write_options(&path, expire, options.clone())
.await?;
- let headers = metadata_only.header();
- assert_eq!(headers[header::CONTENT_TYPE], "application/json");
- assert_eq!(headers[unrelated], unrelated_value);
- assert!(!headers.contains_key(header::CACHE_CONTROL));
- for (name, expected) in [
- (reserved, ["max-age%3D30", "max-age%3D60"]),
- (metadata_cache_control, ["no-cache", "no-store"]),
- ] {
- let mut values: Vec<_> = headers
- .get_all(name)
- .iter()
- .map(|value| value.to_str().unwrap())
- .collect();
- values.sort_unstable();
- assert_eq!(values, expected, "{name}");
- }
-
+ options.content_type = Some("application/json".to_owned());
options.cache_control = Some("private, max-age=137".to_owned());
+ options.content_disposition =
+ Some("attachment; filename=\"OpenDAL %20 + probe.txt\"".to_owned());
+ options.content_encoding = Some("gzip".to_owned());
+ options.content_language = Some("fr-CA, en".to_owned());
let combined = op.presign_write_options(&path, expire, options).await?;
- let headers = combined.header();
- assert_eq!(headers.get_all(reserved).iter().count(), 1);
- assert_eq!(headers[reserved], "private%2C%20max-age%3D137");
- assert_eq!(headers[unrelated], unrelated_value);
- assert!(!headers.contains_key(metadata_cache_control));
- assert!(!headers.contains_key(header::CACHE_CONTROL));
+
+ assert_header_values(metadata_only.header(), "content-type",
&["b2/x-auto"]);
+ assert_header_values(combined.header(), "content-type",
&["application/json"]);
+ let reserved: [(&str, &[&str], &str); 4] = [
+ (
+ "x-bz-info-b2-cache-control",
+ &["max-age%3D30", "max-age%3D60"],
+ "private%2C%20max-age%3D137",
+ ),
+ (
+ "x-bz-info-b2-content-disposition",
+ &["inline"],
+
"attachment%3B%20filename%3D%22OpenDAL%20%2520%20%2B%20probe.txt%22",
+ ),
+ ("x-bz-info-b2-content-encoding", &["identity"], "gzip"),
+ ("x-bz-info-b2-content-language", &["de"], "fr-CA%2C%20en"),
+ ];
+ for (name, metadata_values, dedicated_value) in reserved {
+ assert_header_values(default_request.header(), name, &[]);
+ assert_header_values(metadata_only.header(), name, metadata_values);
+ assert_header_values(combined.header(), name, &[dedicated_value]);
+ }
+
+ for headers in [metadata_only.header(), combined.header()] {
+ for (name, values) in [
+ ("x-bz-info-cache-control", &["no-cache", "no-store"][..]),
+ ("x-bz-info-content-disposition", &["attachment"][..]),
+ ("x-bz-info-content-encoding", &["br"][..]),
+ ("x-bz-info-content-language", &["es"][..]),
+ ("x-bz-info-content-type", &["text/plain"][..]),
+ (
+ "x-bz-info-application-validation",
+ &["b2%20trial%20%2B%20caf%C3%A9%20%25"][..],
+ ),
+ ] {
+ assert_header_values(headers, name, values);
+ }
+ }
+ for headers in [
+ default_request.header(),
+ metadata_only.header(),
+ combined.header(),
+ ] {
+ for name in [
+ "cache-control",
+ "content-disposition",
+ "content-encoding",
+ "content-language",
+ ] {
+ assert_header_values(headers, name, &[]);
+ }
+ }
Ok(())
}