Skip to content

Commit

Permalink
s3s: add S3ServiceBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
Nugine committed Apr 11, 2023
1 parent 0954eef commit c1b941f
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 146 deletions.
3 changes: 2 additions & 1 deletion codegen/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ pub fn codegen(ops: &Operations, rust_types: &RustTypes, g: &mut Codegen) {
"use crate::s3_trait::S3;",
"",
"use std::borrow::Cow;",
"use std::sync::Arc;",
"",
]);

Expand Down Expand Up @@ -596,7 +597,7 @@ fn codegen_op_http_call(op: &Operation, g: &mut Codegen) {
g.ln("}");
g.lf();

g.ln("async fn call(&self, s3: &dyn S3, req: &mut http::Request) -> S3Result<http::Response> {");
g.ln("async fn call(&self, s3: &Arc<dyn S3>, req: &mut http::Request) -> S3Result<http::Response> {");

let method = op.name.to_snake_case();

Expand Down
33 changes: 19 additions & 14 deletions crates/s3s-fs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use s3s_fs::FileSystem;
use s3s_fs::Result;

use s3s::auth::SimpleAuth;
use s3s::service::S3Service;
use s3s::service::S3ServiceBuilder;

use std::net::TcpListener;
use std::path::PathBuf;
Expand Down Expand Up @@ -48,20 +48,25 @@ async fn main() -> Result {
setup_tracing();
let opt = Opt::parse();

// Setup S3 service
// Setup S3 provider
let fs = FileSystem::new(opt.root)?;
let mut service = S3Service::new(Box::new(fs));

// Enable authentication
if let (Some(ak), Some(sk)) = (opt.access_key, opt.secret_key) {
let auth = SimpleAuth::from_single(ak, sk);
service.set_auth(Box::new(auth));
}

// Enable parsing virtual-hosted-style requests
if let Some(domain_name) = opt.domain_name {
service.set_base_domain(domain_name);
}

// Setup S3 service
let service = {
let mut b = S3ServiceBuilder::new(fs);

// Enable authentication
if let (Some(ak), Some(sk)) = (opt.access_key, opt.secret_key) {
b.set_auth(SimpleAuth::from_single(ak, sk));
}

// Enable parsing virtual-hosted-style requests
if let Some(domain_name) = opt.domain_name {
b.set_base_domain(domain_name);
}

b.build()
};

// Run server
let listener = TcpListener::bind((opt.host.as_str(), opt.port))?;
Expand Down
28 changes: 17 additions & 11 deletions crates/s3s-fs/tests/it_aws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
clippy::must_use_candidate, //
)]

use s3s::service::S3Service;
use s3s::auth::SimpleAuth;
use s3s::service::S3ServiceBuilder;
use s3s_fs::FileSystem;

use std::env;
use std::fs;
Expand Down Expand Up @@ -49,21 +51,25 @@ fn config() -> &'static SdkConfig {
static CONFIG: Lazy<SdkConfig> = Lazy::new(|| {
setup_tracing();

// Fake credentials
let cred = Credentials::for_tests();

let conn = {
fs::create_dir_all(FS_ROOT).unwrap();
let fs = s3s_fs::FileSystem::new(FS_ROOT).unwrap();
// Setup S3 provider
fs::create_dir_all(FS_ROOT).unwrap();
let fs = FileSystem::new(FS_ROOT).unwrap();

let auth = s3s::auth::SimpleAuth::from_single(cred.access_key_id(), cred.secret_access_key());

let mut service = S3Service::new(Box::new(fs));
service.set_auth(Box::new(auth));
service.set_base_domain(DOMAIN_NAME);

s3s_aws::Connector::from(service.into_shared())
// Setup S3 service
let service = {
let mut b = S3ServiceBuilder::new(fs);
b.set_auth(SimpleAuth::from_single(cred.access_key_id(), cred.secret_access_key()));
b.set_base_domain(DOMAIN_NAME);
b.build()
};

// Convert to aws http connector
let conn = s3s_aws::Connector::from(service.into_shared());

// Setup aws sdk config
SdkConfig::builder()
.credentials_provider(SharedCredentialsProvider::new(cred))
.http_connector(conn)
Expand Down
35 changes: 20 additions & 15 deletions crates/s3s-proxy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![deny(clippy::all)]

use s3s::auth::SimpleAuth;
use s3s::service::S3Service;
use s3s::service::S3ServiceBuilder;

use std::error::Error;
use std::net::TcpListener;
Expand Down Expand Up @@ -42,22 +42,27 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
setup_tracing();
let opt = Opt::parse();

// Setup S3 service
// Setup S3 provider
let conf = aws_config::from_env().endpoint_url(&opt.endpoint_url).load().await;
let proxy = s3s_aws::Proxy::from(aws_sdk_s3::Client::new(&conf));
let mut service = S3Service::new(Box::new(proxy));

// Enable authentication
if let Some(cred_provider) = conf.credentials_provider() {
let cred = cred_provider.provide_credentials().await?;
let auth = SimpleAuth::from_single(cred.access_key_id(), cred.secret_access_key());
service.set_auth(Box::new(auth));
}

// Enable parsing virtual-hosted-style requests
if let Some(domain_name) = opt.domain_name {
service.set_base_domain(domain_name);
}

// Setup S3 service
let service = {
let mut b = S3ServiceBuilder::new(proxy);

// Enable authentication
if let Some(cred_provider) = conf.credentials_provider() {
let cred = cred_provider.provide_credentials().await?;
b.set_auth(SimpleAuth::from_single(cred.access_key_id(), cred.secret_access_key()));
}

// Enable parsing virtual-hosted-style requests
if let Some(domain_name) = opt.domain_name {
b.set_base_domain(domain_name);
}

b.build()
};

// Run server
let listener = TcpListener::bind((opt.host.as_str(), opt.port))?;
Expand Down
Loading

0 comments on commit c1b941f

Please sign in to comment.