Skip to content

Commit 6b2307a

Browse files
authored
feat(services/oss): Implement Write Returns Metadata for oss (#5688)
* feat(services/oss): Implement Write Returns Metadata for oss * add write_has_content_md5 to Capability
1 parent 6833a10 commit 6b2307a

File tree

6 files changed

+44
-5
lines changed

6 files changed

+44
-5
lines changed

core/src/services/oss/backend.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,8 @@ impl Builder for OssBuilder {
460460
Some(usize::MAX)
461461
},
462462
write_with_user_metadata: true,
463+
write_has_etag: true,
464+
write_has_version: self.config.enable_versioning,
463465

464466
delete: true,
465467
delete_with_version: self.config.enable_versioning,
@@ -541,7 +543,7 @@ impl Access for OssBackend {
541543
let headers = resp.headers();
542544
let mut meta = self.core.parse_metadata(path, resp.headers())?;
543545

544-
if let Some(v) = parse_header_to_str(headers, "x-oss-version-id")? {
546+
if let Some(v) = parse_header_to_str(headers, constants::X_OSS_VERSION_ID)? {
545547
meta.set_version(v);
546548
}
547549

core/src/services/oss/core.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ pub mod constants {
5454

5555
pub const X_OSS_FORBID_OVERWRITE: &str = "x-oss-forbid-overwrite";
5656

57+
pub const X_OSS_VERSION_ID: &str = "x-oss-version-id";
58+
5759
pub const RESPONSE_CONTENT_DISPOSITION: &str = "response-content-disposition";
5860

5961
pub const OSS_QUERY_VERSION_ID: &str = "versionId";

core/src/services/oss/writer.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
use std::sync::Arc;
1919

20-
use http::StatusCode;
20+
use http::{HeaderMap, HeaderValue, StatusCode};
2121

2222
use super::core::*;
2323
use super::error::parse_error;
@@ -41,6 +41,21 @@ impl OssWriter {
4141
op,
4242
}
4343
}
44+
45+
fn parse_metadata(headers: &HeaderMap<HeaderValue>) -> Result<Metadata> {
46+
let mut meta = Metadata::default();
47+
if let Some(etag) = parse_etag(headers)? {
48+
meta.set_etag(etag);
49+
}
50+
if let Some(md5) = parse_content_md5(headers)? {
51+
meta.set_content_md5(md5);
52+
}
53+
if let Some(version) = parse_header_to_str(headers, constants::X_OSS_VERSION_ID)? {
54+
meta.set_version(version);
55+
}
56+
57+
Ok(meta)
58+
}
4459
}
4560

4661
impl oio::MultipartWrite for OssWriter {
@@ -53,10 +68,11 @@ impl oio::MultipartWrite for OssWriter {
5368

5469
let resp = self.core.send(req).await?;
5570

71+
let meta = Self::parse_metadata(resp.headers())?;
5672
let status = resp.status();
5773

5874
match status {
59-
StatusCode::CREATED | StatusCode::OK => Ok(Metadata::default()),
75+
StatusCode::CREATED | StatusCode::OK => Ok(meta),
6076
_ => Err(parse_error(resp)),
6177
}
6278
}
@@ -145,10 +161,11 @@ impl oio::MultipartWrite for OssWriter {
145161
.oss_complete_multipart_upload_request(&self.path, upload_id, false, parts)
146162
.await?;
147163

164+
let meta = Self::parse_metadata(resp.headers())?;
148165
let status = resp.status();
149166

150167
match status {
151-
StatusCode::OK => Ok(Metadata::default()),
168+
StatusCode::OK => Ok(meta),
152169
_ => Err(parse_error(resp)),
153170
}
154171
}
@@ -198,10 +215,11 @@ impl oio::AppendWrite for OssWriter {
198215

199216
let resp = self.core.send(req).await?;
200217

218+
let meta = Self::parse_metadata(resp.headers())?;
201219
let status = resp.status();
202220

203221
match status {
204-
StatusCode::OK => Ok(Metadata::default()),
222+
StatusCode::OK => Ok(meta),
205223
_ => Err(parse_error(resp)),
206224
}
207225
}

core/src/types/capability.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ pub struct Capability {
166166
pub write_has_etag: bool,
167167
/// Indicates whether version information is available in write response
168168
pub write_has_version: bool,
169+
/// Indicates whether content MD5 checksum is available in write response
170+
pub write_has_content_md5: bool,
169171

170172
/// Indicates if directory creation is supported.
171173
pub create_dir: bool,

core/tests/behavior/async_write.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,9 @@ pub async fn test_write_returns_metadata(op: Operator) -> Result<()> {
273273
if cap.write_has_version {
274274
assert_eq!(stat_meta.version(), meta.version());
275275
}
276+
if cap.write_has_content_md5 {
277+
assert_eq!(stat_meta.content_md5(), meta.content_md5());
278+
}
276279

277280
Ok(())
278281
}
@@ -599,6 +602,9 @@ pub async fn test_writer_return_metadata(op: Operator) -> Result<()> {
599602
if cap.write_has_version {
600603
assert_eq!(stat_meta.version(), meta.version());
601604
}
605+
if cap.write_has_content_md5 {
606+
assert_eq!(stat_meta.content_md5(), meta.content_md5());
607+
}
602608

603609
Ok(())
604610
}
@@ -665,6 +671,9 @@ pub async fn test_write_with_append_returns_metadata(op: Operator) -> Result<()>
665671
if cap.write_has_version {
666672
assert_eq!(stat_meta.version(), meta.version());
667673
}
674+
if cap.write_has_content_md5 {
675+
assert_eq!(stat_meta.content_md5(), meta.content_md5());
676+
}
668677

669678
Ok(())
670679
}

core/tests/behavior/blocking_write.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ pub fn test_blocking_write_returns_metadata(op: BlockingOperator) -> Result<()>
109109
if cap.write_has_version {
110110
assert_eq!(meta.version(), stat_meta.version());
111111
}
112+
if cap.write_has_content_md5 {
113+
assert_eq!(meta.content_md5(), stat_meta.content_md5());
114+
}
112115

113116
Ok(())
114117
}
@@ -166,6 +169,9 @@ pub fn test_blocking_write_with_append_returns_metadata(op: BlockingOperator) ->
166169
if cap.write_has_version {
167170
assert_eq!(meta.version(), stat_meta.version());
168171
}
172+
if cap.write_has_content_md5 {
173+
assert_eq!(meta.content_md5(), stat_meta.content_md5());
174+
}
169175

170176
Ok(())
171177
}

0 commit comments

Comments
 (0)