From f23d2fe43191aa42f3b76bcd18a3850f89cb20be Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Fri, 09 Sep 2022 00:09:19 +0100 Subject: [PATCH] Support alternative S3-compatible crate stores --- .env | 1 - .gitignore | 2 ++ Cargo.lock | 1 + chartered-fs/Cargo.toml | 1 + chartered-fs/src/lib.rs | 22 ++++++++++++++++++---- chartered-frontend/src/components/FeaturedCrate.svelte | 2 +- 6 files changed, 21 insertions(+), 8 deletions(-) diff --git a/.env b/.env deleted file mode 100644 index 5f2cbfe..0000000 100644 --- a/.env +++ /dev/null @@ -1,1 +1,0 @@ -DATABASE_URL=chartered.db diff --git a/.gitignore b/.gitignore index 2c83db1..b65c586 100644 --- a/.gitignore +++ a/.gitignore @@ -1,4 +1,6 @@ /target .idea/ /chartered.db .DS_Store +.env + diff --git a/Cargo.lock b/Cargo.lock index d950d4d..c444881 100644 --- a/Cargo.lock +++ a/Cargo.lock @@ -669,6 +669,7 @@ "async-trait", "aws-config", "aws-sdk-s3", + "base64", "bytes", "http", "itertools", diff --git a/chartered-fs/Cargo.toml b/chartered-fs/Cargo.toml index a356d38..e2ffed4 100644 --- a/chartered-fs/Cargo.toml +++ a/chartered-fs/Cargo.toml @@ -9,6 +9,7 @@ async-trait = "0.1" aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", tag = "release-2022-08-08", package = "aws-config" } aws-sdk-s3 = { git = "https://github.com/awslabs/aws-sdk-rust", tag = "release-2022-08-08", package = "aws-sdk-s3" } +base64 = "0.13" bytes = "1.1" http = "0.2" itertools = "0.10" diff --git a/chartered-fs/src/lib.rs b/chartered-fs/src/lib.rs index 5b2a0e6..1ccefa2 100644 --- a/chartered-fs/src/lib.rs +++ a/chartered-fs/src/lib.rs @@ -10,6 +10,7 @@ model::ObjectCannedAcl, presigning::config::PresigningConfig, types::{ByteStream, SdkError}, + Endpoint, }; use bytes::Bytes; use itertools::Itertools; @@ -36,8 +37,8 @@ UuidParse(#[from] uuid::Error), #[error("path missing from uri")] MissingPath, - #[error("host missing from uri")] - MissingHost, + #[error("invalid uri: {0}")] + InvalidUri(http::uri::InvalidUri), #[error("bucket missing from uri")] MissingBucket, #[error("invalid aws presigning config: {0}")] @@ -56,13 +57,23 @@ Ok(match uri.scheme() { "s3" => { - let shared_config = aws_config::load_from_env().await; + let mut shared_config = aws_config::from_env(); + + if let Some(host) = uri.host() { + shared_config = shared_config.endpoint_resolver(Endpoint::immutable( + format!("https://{}", host) + .parse() + .map_err(Error::InvalidUri)?, + )); + } + + let shared_config = shared_config.load().await; + let client = aws_sdk_s3::Client::new(&shared_config); let mut path = uri.path_segments().ok_or(Error::MissingPath)?; Self::S3(S3 { - host: uri.host().ok_or(Error::MissingHost)?.to_string(), bucket: path.next().ok_or(Error::MissingBucket)?.to_string(), path: Itertools::intersperse(path, "/").collect(), client, @@ -198,7 +209,6 @@ #[derive(Debug)] #[allow(dead_code)] pub struct S3 { - host: String, bucket: String, path: String, client: aws_sdk_s3::Client, @@ -227,7 +237,7 @@ self.client .put_object() .key(format!("{}/{}", self.path, file_ref.reference)) - .content_md5(format!("{:x}", md5::compute(&data))) + .content_md5(base64::encode(&*md5::compute(&data))) .body(ByteStream::new(data.into())) .bucket(&self.bucket) .acl(ObjectCannedAcl::Private) diff --git a/chartered-frontend/src/components/FeaturedCrate.svelte b/chartered-frontend/src/components/FeaturedCrate.svelte index c67e2f9..4076305 100644 --- a/chartered-frontend/src/components/FeaturedCrate.svelte +++ a/chartered-frontend/src/components/FeaturedCrate.svelte @@ -8,7 +8,7 @@ export let crate: Crate; - +
{crate.organisation}/{crate.name} -- rgit 0.1.3