-
Notifications
You must be signed in to change notification settings - Fork 866
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
Extract Common Listing and Retrieval Functionality #4220
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b435238
Factor out common cloud storage client functionality
tustvold 6b7ef28
Remove format_prefix
tustvold 7579233
Merge remote-tracking branch 'upstream/master' into client-traits
tustvold 84c5e62
Merge remote-tracking branch 'upstream/master' into client-traits
tustvold 0b13c32
Review feedback
tustvold 365d85c
Merge remote-tracking branch 'upstream/master' into client-traits
tustvold File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,17 +18,17 @@ | |
use crate::aws::checksum::Checksum; | ||
use crate::aws::credential::{AwsCredential, CredentialExt}; | ||
use crate::aws::{AwsCredentialProvider, STORE, STRICT_PATH_ENCODE_SET}; | ||
use crate::client::list::ListResponse; | ||
use crate::client::pagination::stream_paginated; | ||
use crate::client::get::GetClient; | ||
use crate::client::list::ListClient; | ||
use crate::client::list_response::ListResponse; | ||
use crate::client::retry::RetryExt; | ||
use crate::client::GetOptionsExt; | ||
use crate::multipart::UploadPart; | ||
use crate::path::DELIMITER; | ||
use crate::util::format_prefix; | ||
use crate::{ | ||
BoxStream, ClientOptions, GetOptions, ListResult, MultipartId, Path, Result, | ||
RetryConfig, StreamExt, | ||
ClientOptions, GetOptions, ListResult, MultipartId, Path, Result, RetryConfig, | ||
}; | ||
use async_trait::async_trait; | ||
use base64::prelude::BASE64_STANDARD; | ||
use base64::Engine; | ||
use bytes::{Buf, Bytes}; | ||
|
@@ -169,40 +169,6 @@ impl S3Client { | |
self.config.credentials.get_credential().await | ||
} | ||
|
||
/// Make an S3 GET request <https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObject.html> | ||
pub async fn get_request( | ||
&self, | ||
path: &Path, | ||
options: GetOptions, | ||
head: bool, | ||
) -> Result<Response> { | ||
let credential = self.get_credential().await?; | ||
let url = self.config.path_url(path); | ||
let method = match head { | ||
true => Method::HEAD, | ||
false => Method::GET, | ||
}; | ||
|
||
let builder = self.client.request(method, url); | ||
|
||
let response = builder | ||
.with_get_options(options) | ||
.with_aws_sigv4( | ||
credential.as_ref(), | ||
&self.config.region, | ||
"s3", | ||
self.config.sign_payload, | ||
None, | ||
) | ||
.send_retry(&self.config.retry_config) | ||
.await | ||
.context(GetRequestSnafu { | ||
path: path.as_ref(), | ||
})?; | ||
|
||
Ok(response) | ||
} | ||
|
||
/// Make an S3 PUT request <https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html> | ||
pub async fn put_request<T: Serialize + ?Sized + Sync>( | ||
&self, | ||
|
@@ -302,88 +268,6 @@ impl S3Client { | |
Ok(()) | ||
} | ||
|
||
/// Make an S3 List request <https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html> | ||
async fn list_request( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just moved |
||
&self, | ||
prefix: Option<&str>, | ||
delimiter: bool, | ||
token: Option<&str>, | ||
offset: Option<&str>, | ||
) -> Result<(ListResult, Option<String>)> { | ||
let credential = self.get_credential().await?; | ||
let url = self.config.bucket_endpoint.clone(); | ||
|
||
let mut query = Vec::with_capacity(4); | ||
|
||
if let Some(token) = token { | ||
query.push(("continuation-token", token)) | ||
} | ||
|
||
if delimiter { | ||
query.push(("delimiter", DELIMITER)) | ||
} | ||
|
||
query.push(("list-type", "2")); | ||
|
||
if let Some(prefix) = prefix { | ||
query.push(("prefix", prefix)) | ||
} | ||
|
||
if let Some(offset) = offset { | ||
query.push(("start-after", offset)) | ||
} | ||
|
||
let response = self | ||
.client | ||
.request(Method::GET, &url) | ||
.query(&query) | ||
.with_aws_sigv4( | ||
credential.as_ref(), | ||
&self.config.region, | ||
"s3", | ||
self.config.sign_payload, | ||
None, | ||
) | ||
.send_retry(&self.config.retry_config) | ||
.await | ||
.context(ListRequestSnafu)? | ||
.bytes() | ||
.await | ||
.context(ListResponseBodySnafu)?; | ||
|
||
let mut response: ListResponse = quick_xml::de::from_reader(response.reader()) | ||
.context(InvalidListResponseSnafu)?; | ||
let token = response.next_continuation_token.take(); | ||
|
||
Ok((response.try_into()?, token)) | ||
} | ||
|
||
/// Perform a list operation automatically handling pagination | ||
pub fn list_paginated( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is now implemented by ListClientExt |
||
&self, | ||
prefix: Option<&Path>, | ||
delimiter: bool, | ||
offset: Option<&Path>, | ||
) -> BoxStream<'_, Result<ListResult>> { | ||
let offset = offset.map(|x| x.to_string()); | ||
let prefix = format_prefix(prefix); | ||
stream_paginated( | ||
(prefix, offset), | ||
move |(prefix, offset), token| async move { | ||
let (r, next_token) = self | ||
.list_request( | ||
prefix.as_deref(), | ||
delimiter, | ||
token.as_deref(), | ||
offset.as_deref(), | ||
) | ||
.await?; | ||
Ok((r, (prefix, offset), next_token)) | ||
}, | ||
) | ||
.boxed() | ||
} | ||
|
||
pub async fn create_multipart(&self, location: &Path) -> Result<MultipartId> { | ||
let credential = self.get_credential().await?; | ||
let url = format!("{}?uploads=", self.config.path_url(location),); | ||
|
@@ -451,6 +335,104 @@ impl S3Client { | |
} | ||
} | ||
|
||
#[async_trait] | ||
impl GetClient for S3Client { | ||
const STORE: &'static str = STORE; | ||
|
||
/// Make an S3 GET request <https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObject.html> | ||
async fn get_request( | ||
&self, | ||
path: &Path, | ||
options: GetOptions, | ||
head: bool, | ||
) -> Result<Response> { | ||
let credential = self.get_credential().await?; | ||
let url = self.config.path_url(path); | ||
let method = match head { | ||
true => Method::HEAD, | ||
false => Method::GET, | ||
}; | ||
|
||
let builder = self.client.request(method, url); | ||
|
||
let response = builder | ||
.with_get_options(options) | ||
.with_aws_sigv4( | ||
credential.as_ref(), | ||
&self.config.region, | ||
"s3", | ||
self.config.sign_payload, | ||
None, | ||
) | ||
.send_retry(&self.config.retry_config) | ||
.await | ||
.context(GetRequestSnafu { | ||
path: path.as_ref(), | ||
})?; | ||
|
||
Ok(response) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl ListClient for S3Client { | ||
/// Make an S3 List request <https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html> | ||
async fn list_request( | ||
&self, | ||
prefix: Option<&str>, | ||
delimiter: bool, | ||
token: Option<&str>, | ||
offset: Option<&str>, | ||
) -> Result<(ListResult, Option<String>)> { | ||
let credential = self.get_credential().await?; | ||
let url = self.config.bucket_endpoint.clone(); | ||
|
||
let mut query = Vec::with_capacity(4); | ||
|
||
if let Some(token) = token { | ||
query.push(("continuation-token", token)) | ||
} | ||
|
||
if delimiter { | ||
query.push(("delimiter", DELIMITER)) | ||
} | ||
|
||
query.push(("list-type", "2")); | ||
|
||
if let Some(prefix) = prefix { | ||
query.push(("prefix", prefix)) | ||
} | ||
|
||
if let Some(offset) = offset { | ||
query.push(("start-after", offset)) | ||
} | ||
|
||
let response = self | ||
.client | ||
.request(Method::GET, &url) | ||
.query(&query) | ||
.with_aws_sigv4( | ||
credential.as_ref(), | ||
&self.config.region, | ||
"s3", | ||
self.config.sign_payload, | ||
None, | ||
) | ||
.send_retry(&self.config.retry_config) | ||
.await | ||
.context(ListRequestSnafu)? | ||
.bytes() | ||
.await | ||
.context(ListResponseBodySnafu)?; | ||
|
||
let mut response: ListResponse = quick_xml::de::from_reader(response.reader()) | ||
.context(InvalidListResponseSnafu)?; | ||
let token = response.next_continuation_token.take(); | ||
|
||
Ok((response.try_into()?, token)) | ||
} | ||
} | ||
|
||
fn encode_path(path: &Path) -> PercentEncode<'_> { | ||
utf8_percent_encode(path.as_ref(), &STRICT_PATH_ENCODE_SET) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is moved to be a trait implementation