Skip to content

Commit

Permalink
Recognise R2 URLs (#4190) (#4194)
Browse files Browse the repository at this point in the history
  • Loading branch information
tustvold authored May 10, 2023
1 parent b3a9981 commit 615dde0
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions object_store/src/aws/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ impl AmazonS3Builder {
/// - `s3a://<bucket>/<path>`
/// - `https://s3.<bucket>.amazonaws.com`
/// - `https://<bucket>.s3.<region>.amazonaws.com`
/// - `https://ACCOUNT_ID.r2.cloudflarestorage.com/bucket`
///
/// Note: Settings derived from the URL will override any others set on this builder
///
Expand Down Expand Up @@ -849,9 +850,8 @@ impl AmazonS3Builder {
"https" => match host.splitn(4, '.').collect_tuple() {
Some(("s3", region, "amazonaws", "com")) => {
self.region = Some(region.to_string());
if let Some(bucket) =
parsed.path_segments().and_then(|mut path| path.next())
{
let bucket = parsed.path_segments().into_iter().flatten().next();
if let Some(bucket) = bucket {
self.bucket_name = Some(bucket.into());
}
}
Expand All @@ -860,6 +860,16 @@ impl AmazonS3Builder {
self.region = Some(region.to_string());
self.virtual_hosted_style_request = true;
}
Some((account, "r2", "cloudflarestorage", "com")) => {
self.region = Some("auto".to_string());
let endpoint = format!("https://{account}.r2.cloudflarestorage.com");
self.endpoint = Some(endpoint);

let bucket = parsed.path_segments().into_iter().flatten().next();
if let Some(bucket) = bucket {
self.bucket_name = Some(bucket.into());
}
}
_ => return Err(UrlNotRecognisedSnafu { url }.build().into()),
},
scheme => return Err(UnknownUrlSchemeSnafu { scheme }.build().into()),
Expand Down Expand Up @@ -1556,6 +1566,18 @@ mod tests {
assert_eq!(builder.region, Some("region".to_string()));
assert!(builder.virtual_hosted_style_request);

let mut builder = AmazonS3Builder::new();
builder
.parse_url("https://account123.r2.cloudflarestorage.com/bucket-123")
.unwrap();

assert_eq!(builder.bucket_name, Some("bucket-123".to_string()));
assert_eq!(builder.region, Some("auto".to_string()));
assert_eq!(
builder.endpoint,
Some("https://account123.r2.cloudflarestorage.com".to_string())
);

let err_cases = [
"mailto://bucket/path",
"https://s3.bucket.mydomain.com",
Expand Down

0 comments on commit 615dde0

Please sign in to comment.