Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(services/s3): Add object versioning for S3 #4873

Merged
merged 6 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions core/src/services/s3/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,16 @@ impl Access for S3Backend {
let status = resp.status();

match status {
StatusCode::OK => parse_into_metadata(path, resp.headers()).map(RpStat::new),
StatusCode::OK => {
let headers = resp.headers();
let mut meta = parse_into_metadata(path, headers)?;

if let Some(v) = parse_header_to_str(headers, "x-amz-version-id")? {
meta.set_version(v);
}

Ok(RpStat::new(meta))
}
_ => Err(parse_error(resp)),
}
}
Expand Down Expand Up @@ -1124,13 +1133,13 @@ impl Access for S3Backend {
Ok((RpWrite::default(), w))
}

async fn delete(&self, path: &str, _: OpDelete) -> Result<RpDelete> {
async fn delete(&self, path: &str, args: OpDelete) -> Result<RpDelete> {
// This would delete the bucket, do not perform
if self.core.root == "/" && path == "/" {
return Ok(RpDelete::default());
}

let resp = self.core.s3_delete_object(path).await?;
let resp = self.core.s3_delete_object(path, &args).await?;

let status = resp.status();

Expand Down
34 changes: 32 additions & 2 deletions core/src/services/s3/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ mod constants {
pub const RESPONSE_CONTENT_DISPOSITION: &str = "response-content-disposition";
pub const RESPONSE_CONTENT_TYPE: &str = "response-content-type";
pub const RESPONSE_CACHE_CONTROL: &str = "response-cache-control";

pub const S3_QUERY_VERSION_ID: &str = "versionId";
}

pub struct S3Core {
Expand Down Expand Up @@ -312,6 +314,13 @@ impl S3Core {
percent_encode_path(override_cache_control)
))
}
if let Some(version) = args.version() {
query_args.push(format!(
"{}={}",
constants::S3_QUERY_VERSION_ID,
percent_decode_path(version)
))
}
if !query_args.is_empty() {
url.push_str(&format!("?{}", query_args.join("&")));
}
Expand Down Expand Up @@ -367,6 +376,13 @@ impl S3Core {
percent_encode_path(override_cache_control)
))
}
if let Some(version) = args.version() {
query_args.push(format!(
"{}={}",
constants::S3_QUERY_VERSION_ID,
percent_decode_path(version)
))
}
if !query_args.is_empty() {
url.push_str(&format!("?{}", query_args.join("&")));
}
Expand Down Expand Up @@ -463,10 +479,24 @@ impl S3Core {
self.send(req).await
}

pub async fn s3_delete_object(&self, path: &str) -> Result<Response<Buffer>> {
pub async fn s3_delete_object(&self, path: &str, args: &OpDelete) -> Result<Response<Buffer>> {
let p = build_abs_path(&self.root, path);

let url = format!("{}/{}", self.endpoint, percent_encode_path(&p));
let mut url = format!("{}/{}", self.endpoint, percent_encode_path(&p));

let mut query_args = Vec::new();

if let Some(version) = args.version() {
query_args.push(format!(
"{}={}",
constants::S3_QUERY_VERSION_ID,
percent_encode_path(version)
))
}

if !query_args.is_empty() {
url.push_str(&format!("?{}", query_args.join("&")));
}

let mut req = Request::delete(&url)
.body(Buffer::new())
Expand Down
Loading