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 4ae8e61222 feat(services/gdrive): support stat for binary file
revisions (#9310)
4ae8e61222 is described below
commit 4ae8e61222e6d282b2a1ec99db4e87dccacf5919
Author: Xuanwo <[email protected]>
AuthorDate: Sat Nov 28 08:34:55 2026 +0800
feat(services/gdrive): support stat for binary file revisions (#9310)
feat(services/gdrive): stat binary file revisions
---
core/services/gdrive/src/backend.rs | 51 +++++++++++++++++++++++--------
core/services/gdrive/src/core.rs | 61 ++++++++++++++++++++++++++++++++++---
core/services/gdrive/src/docs.md | 11 ++++---
website/data/capabilities.json | 1 +
4 files changed, 102 insertions(+), 22 deletions(-)
diff --git a/core/services/gdrive/src/backend.rs
b/core/services/gdrive/src/backend.rs
index b81b760f73..68912e5b11 100644
--- a/core/services/gdrive/src/backend.rs
+++ b/core/services/gdrive/src/backend.rs
@@ -23,6 +23,7 @@ use http::StatusCode;
use super::core::GdriveFile;
use super::core::GdriveRecentPathState;
+use super::core::GdriveRevision;
use super::core::metadata_from_gdrive_file;
use super::core::normalize_dir_path;
use super::core::parse_error;
@@ -138,6 +139,7 @@ impl Builder for GdriveBuilder {
let info = ServiceInfo::new(GDRIVE_SCHEME, &root, "");
let capability = Capability {
stat: true,
+ stat_with_version: true,
read: true,
read_with_version: true,
@@ -261,12 +263,19 @@ impl Service for GdriveBackend {
Ok(RpCreateDir::default())
}
- async fn stat(&self, ctx: &OperationContext, path: &str, _args: OpStat) ->
Result<RpStat> {
+ async fn stat(&self, ctx: &OperationContext, path: &str, args: OpStat) ->
Result<RpStat> {
+ let version = args.version();
+ if matches!(version, Some("" | "." | "..")) {
+ return Err(Error::new(
+ ErrorKind::NotFound,
+ "invalid Google Drive revision ID",
+ ));
+ }
let path = build_absolute_path(&self.core.root, path);
match self.core.recent_entry_for_path(&path).await {
GdriveRecentPathState::Present(metadata) => {
- if metadata.mode().is_dir() && path.ends_with('/') {
+ if version.is_none() && metadata.mode().is_dir() &&
path.ends_with('/') {
return Ok(RpStat::new(*metadata));
}
}
@@ -291,7 +300,15 @@ impl Service for GdriveBackend {
}
},
};
- let mut resp = self.core.gdrive_stat_by_id(ctx, &file_id).await?;
+ let stat_by_id = async |file_id: &str| match version {
+ Some(revision_id) => {
+ self.core
+ .gdrive_stat_revision_by_id(ctx, file_id, revision_id)
+ .await
+ }
+ None => self.core.gdrive_stat_by_id(ctx, file_id).await,
+ };
+ let mut resp = stat_by_id(&file_id).await?;
if resp.status() == StatusCode::NOT_FOUND {
file_id = self
@@ -302,21 +319,29 @@ impl Service for GdriveBackend {
ErrorKind::NotFound,
format!("path not found: {path}"),
))?;
- resp = self.core.gdrive_stat_by_id(ctx, &file_id).await?;
+ resp = stat_by_id(&file_id).await?;
}
if resp.status() != StatusCode::OK {
- return Err(parse_error(
- ErrorContext::new(ServiceOperation("GetFile")),
- resp,
- ));
+ let operation = if version.is_some() {
+ ServiceOperation("GetRevision")
+ } else {
+ ServiceOperation("GetFile")
+ };
+ return Err(parse_error(ErrorContext::new(operation), resp));
}
- let bs = resp.into_body();
- let gdrive_file: GdriveFile =
-
serde_json::from_reader(bs.reader()).map_err(new_json_deserialize_error)?;
-
- Ok(RpStat::new(metadata_from_gdrive_file(&gdrive_file)?))
+ let body = resp.into_body();
+ let metadata = if version.is_some() {
+ let revision: GdriveRevision =
+
serde_json::from_reader(body.reader()).map_err(new_json_deserialize_error)?;
+ revision.into_metadata()?
+ } else {
+ let file: GdriveFile =
+
serde_json::from_reader(body.reader()).map_err(new_json_deserialize_error)?;
+ metadata_from_gdrive_file(&file)?
+ };
+ Ok(RpStat::new(metadata))
}
fn read(&self, ctx: &OperationContext, path: &str, args: OpRead) ->
Result<Self::Reader> {
// These values cannot address a revision as a URL path segment.
diff --git a/core/services/gdrive/src/core.rs b/core/services/gdrive/src/core.rs
index ca7de2d4f1..c84cb46863 100644
--- a/core/services/gdrive/src/core.rs
+++ b/core/services/gdrive/src/core.rs
@@ -256,6 +256,29 @@ impl GdriveCore {
ctx.http_transport().send(req).await
}
+ fn revision_url(file_id: &str, revision_id: &str) -> String {
+ let file_id = percent_encode_path(file_id).replace('/', "%2F");
+ let revision_id = percent_encode_path(revision_id).replace('/', "%2F");
+
format!("https://www.googleapis.com/drive/v3/files/{file_id}/revisions/{revision_id}")
+ }
+
+ pub async fn gdrive_stat_revision_by_id(
+ &self,
+ ctx: &OperationContext,
+ file_id: &str,
+ revision_id: &str,
+ ) -> Result<Response<Buffer>> {
+ let url = Self::revision_url(file_id, revision_id);
+ let mut req =
Request::get(format!("{url}?fields=id,size,mimeType,modifiedTime"))
+ .extension(Operation::Stat)
+ .extension(ServiceOperation("GetRevision"))
+ .body(Buffer::new())
+ .map_err(new_request_build_error)?;
+ self.sign(ctx, &mut req).await?;
+
+ ctx.http_transport().send(req).await
+ }
+
pub async fn gdrive_get(
&self,
ctx: &OperationContext,
@@ -287,11 +310,7 @@ impl GdriveCore {
};
let url = if let Some(revision_id) = version {
- let file_id = percent_encode_path(&path_id).replace('/', "%2F");
- let revision_id = percent_encode_path(revision_id).replace('/',
"%2F");
- format!(
-
"https://www.googleapis.com/drive/v3/files/{file_id}/revisions/{revision_id}?alt=media"
- )
+ format!("{}?alt=media", Self::revision_url(&path_id, revision_id))
} else {
format!(
"https://www.googleapis.com/drive/v3/files/{path_id}?alt=media&supportsAllDrives=true"
@@ -1060,6 +1079,38 @@ pub struct GdriveFile {
pub parents: Vec<String>,
}
+#[derive(Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub(super) struct GdriveRevision {
+ id: String,
+ mime_type: String,
+ size: Option<String>,
+ modified_time: String,
+}
+
+impl GdriveRevision {
+ pub(super) fn into_metadata(self) -> Result<Metadata> {
+ let Some(size) = self.size else {
+ return Err(Error::new(
+ ErrorKind::Unsupported,
+ "version stat requires a Google Drive binary file revision",
+ ));
+ };
+ let size = size.parse::<u64>().map_err(|e| {
+ Error::new(ErrorKind::Unexpected, "parse revision content
length").set_source(e)
+ })?;
+ let modified_time =
self.modified_time.parse::<Timestamp>().map_err(|e| {
+ Error::new(ErrorKind::Unexpected, "parse revision last modified
time").set_source(e)
+ })?;
+ let mut metadata = MetadataBuilder::file(size);
+ metadata
+ .content_type(self.mime_type)
+ .last_modified(modified_time)
+ .version(self.id);
+ Ok(metadata.build())
+ }
+}
+
/// refer to
https://developers.google.com/drive/api/reference/rest/v3/files/list
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
diff --git a/core/services/gdrive/src/docs.md b/core/services/gdrive/src/docs.md
index 171621bcfb..3317101825 100644
--- a/core/services/gdrive/src/docs.md
+++ b/core/services/gdrive/src/docs.md
@@ -27,10 +27,13 @@ Concurrent directory creation through an operator and its
clones reuses work for
### Binary file revisions
For binary files, `stat` exposes the `headRevisionId` when Drive provides it.
-Pass that ID to the `version` option of a read to select that revision. Drive
-can purge older revisions and may require a revision to be marked Keep Forever
-before allowing its download. A missing revision returns `NotFound`. A download
-that Drive denies with HTTP 403 returns `PermissionDenied`.
+Pass that ID to `stat_with(path).version(id)` or `read_with(path).version(id)`
+to select the revision. Version-selected stat returns the revision’s content
+length, MIME type, last-modified timestamp, and ID from Drive.
+
+Drive can purge older revisions and may require a revision to be marked Keep
+Forever before allowing its download. A missing revision returns `NotFound`.
+A download that Drive denies with HTTP 403 returns `PermissionDenied`.
### Resumable uploads and retries
diff --git a/website/data/capabilities.json b/website/data/capabilities.json
index 7b0cdb52d9..4dd201b0f1 100644
--- a/website/data/capabilities.json
+++ b/website/data/capabilities.json
@@ -1953,6 +1953,7 @@
"rename": true,
"shared": true,
"stat": true,
+ "stat_with_version": true,
"write": true,
"write_can_multi": true
}