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 d3a93511b5 fix(services/gcs): report read etags in the JSON API form 
(#8953)
d3a93511b5 is described below

commit d3a93511b5f910031e9332eb8f4c165ee49eae77
Author: Chitral Verma <[email protected]>
AuthorDate: Tue Nov 24 04:48:51 2026 +0530

    fix(services/gcs): report read etags in the JSON API form (#8953)
---
 core/services/gcs/src/reader.rs | 83 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 83 insertions(+)

diff --git a/core/services/gcs/src/reader.rs b/core/services/gcs/src/reader.rs
index 50031dcc7e..cc5be8064d 100644
--- a/core/services/gcs/src/reader.rs
+++ b/core/services/gcs/src/reader.rs
@@ -67,6 +67,16 @@ impl oio::StreamRead for GcsReader {
         let (rp, stream) = match status {
             StatusCode::OK | StatusCode::PARTIAL_CONTENT => {
                 let mut metadata = parse_into_metadata(path, 
resp.headers())?.into_builder();
+                // GCS sends the JSON API etag unquoted, as stat and list 
report
+                // it, and matches only that form in preconditions. Some
+                // emulators quote it.
+                if let Some(etag) = parse_etag(resp.headers())? {
+                    let etag = etag
+                        .strip_prefix('"')
+                        .and_then(|etag| etag.strip_suffix('"'))
+                        .unwrap_or(etag);
+                    metadata.etag(etag);
+                }
                 if let Some(generation) = parse_header_to_str(resp.headers(), 
X_GOOG_GENERATION)? {
                     metadata.version(generation);
                 }
@@ -82,3 +92,76 @@ impl oio::StreamRead for GcsReader {
         Ok((rp, Box::new(stream) as Box<dyn oio::ReadStreamDyn>))
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use std::sync::Arc;
+
+    use futures::stream;
+    use http::header::CONTENT_RANGE;
+    use http::header::ETAG;
+    use opendal_core::raw::oio::StreamRead;
+    use reqsign_core::Context;
+    use reqsign_core::ProvideCredentialChain;
+    use reqsign_google::RequestSigner;
+    use reqsign_google::TokenCredentialProvider;
+
+    use super::super::core::GcsCore;
+    use super::*;
+
+    struct EtagTransport(&'static str);
+
+    impl HttpTransport for EtagTransport {
+        async fn fetch(&self, _: http::Request<Buffer>) -> 
Result<Response<HttpBody>> {
+            let body = Buffer::from("data");
+            Response::builder()
+                .status(StatusCode::PARTIAL_CONTENT)
+                .header(CONTENT_RANGE, "bytes 0-3/4")
+                .header(ETAG, self.0)
+                .header(X_GOOG_GENERATION, "7")
+                .body(HttpBody::new(stream::iter([Ok(body)]), Some(4)))
+                .map_err(new_request_build_error)
+        }
+    }
+
+    async fn read_etag(header: &'static str) -> Option<String> {
+        let backend = GcsBackend {
+            core: Arc::new(GcsCore {
+                info: ServiceInfo::new("gcs", "/", "test-bucket"),
+                capability: Capability::default(),
+                endpoint: "https://storage.googleapis.com".to_string(),
+                bucket: "test-bucket".to_string(),
+                root: "/".to_string(),
+                sign_ctx: Context::new(),
+                credential_provider: Arc::new(
+                    
ProvideCredentialChain::new().push(TokenCredentialProvider::new("test-token")),
+                ),
+                request_signer: Arc::new(RequestSigner::new("storage")),
+                signers: HttpTransportCache::new(),
+                predefined_acl: None,
+                default_storage_class: None,
+                skip_signature: true,
+            }),
+        };
+        let ctx = OperationContext::default()
+            .with_http_transport(HttpTransporter::new(EtagTransport(header)));
+        let reader = GcsReader::new(backend, ctx, "object", OpRead::new());
+        let (rp, _) = reader.open(BytesRange::new(0, Some(4))).await.unwrap();
+        rp.into_metadata()
+            .and_then(|meta| meta.etag().map(str::to_string))
+    }
+
+    #[tokio::test]
+    async fn test_read_etag_matches_json_api_form() {
+        // GCS sends the JSON API resource etag unquoted, as stat and list 
report it.
+        assert_eq!(
+            read_etag("CKWasoTgyPkCEAE=").await.as_deref(),
+            Some("CKWasoTgyPkCEAE=")
+        );
+        // Emulators such as fake-gcs-server quote it.
+        assert_eq!(
+            read_etag("\"CKWasoTgyPkCEAE=\"").await.as_deref(),
+            Some("CKWasoTgyPkCEAE=")
+        );
+    }
+}

Reply via email to