Skip to content

Commit

Permalink
fix record with aws-s3
Browse files Browse the repository at this point in the history
  • Loading branch information
giangndm committed Nov 18, 2024
1 parent 8d539b3 commit 4363429
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/media_connector/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ sea-orm = { version = "1.1.0-rc.1", features = [
] }
sea-query = "0.32.0-rc.1"
serde_json = "1.0"
s3-presign = "0.0.2"
s3-presign = "0.0.3"
uuid = {version = "1.10", features = ["fast-rng", "v7"]}
reqwest = { version = "0.12", features = ["json"]}

Expand Down
20 changes: 15 additions & 5 deletions packages/media_connector/src/sql_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,33 @@ impl ConnectorSqlStorage {
.acquire_timeout(Duration::from_secs(8))
.idle_timeout(Duration::from_secs(8))
.max_lifetime(Duration::from_secs(8))
.sqlx_logging(false)
.sqlx_logging_level(log::LevelFilter::Info); // Setting default PostgreSQL schema
.sqlx_logging(true)
.sqlx_logging_level(log::LevelFilter::Info);

let db = Database::connect(opt).await.expect("Should connect to sql server");
migration::Migrator::up(&db, None).await.expect("Should run migration success");

let s3_endpoint = CustomUri::<S3Options>::try_from(cfg.s3_uri.as_str()).expect("should parse s3");
let mut s3 = Presigner::new(
Credentials::new(s3_endpoint.username.expect("Should have s3 accesskey"), s3_endpoint.password.expect("Should have s3 secretkey"), None),
let mut s3 = Presigner::new_with_root(
Credentials::new(
s3_endpoint.username.as_deref().expect("Should have s3 accesskey"),
s3_endpoint.password.as_deref().expect("Should have s3 secretkey"),
None,
),
s3_endpoint.path.first().as_ref().expect("Should have bucket name"),
s3_endpoint.query.region.as_ref().unwrap_or(&"".to_string()),
s3_endpoint.host.as_str(),
);
s3.endpoint(s3_endpoint.endpoint.as_str());
if s3_endpoint.query.path_style == Some(true) {
log::info!("[ConnectorSqlStorage] use path style");
s3.use_path_style();
}

let signed_url = s3.put("aaa.mp4", 10000).unwrap();
log::info!("[ConnectorSqlStorage] signed_url: {:?}", signed_url);
let res = reqwest::Client::new().put(signed_url).body(vec![1; 10000]).send().await.unwrap();
assert_eq!(res.status().as_u16(), 200);

let s3_sub_folder = s3_endpoint.path[1..].join("/");

Self {
Expand Down
1 change: 1 addition & 0 deletions packages/media_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ pin-project-lite = "0.2"
spin = { workspace = true }
once_cell = "1.20"
derive_more = "1.0"
urlencoding = "2.1"
9 changes: 7 additions & 2 deletions packages/media_utils/src/uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub struct CustomUri<Q> {
pub username: Option<String>,
pub password: Option<String>,
pub endpoint: String,
pub host: String,
pub path: Vec<String>,
pub query: Q,
}
Expand All @@ -29,10 +30,14 @@ impl<Q: DeserializeOwned> TryFrom<&str> for CustomUri<Q> {
(false, Some(port)) => format!("http://{}:{}", host, port),
};

let username = uri.username().map(|u| urlencoding::decode(&u.to_string()).map(|u| u.to_string()).ok()).flatten();
let password = uri.password().map(|u| urlencoding::decode(&u.to_string()).map(|u| u.to_string()).ok()).flatten();

Ok(Self {
username: uri.username().map(|u| u.to_string()),
password: uri.password().map(|u| u.to_string()),
username: username.map(|u| u.to_string()),
password: password.map(|u| u.to_string()),
endpoint,
host: host.to_string(),
path,
query,
})
Expand Down

0 comments on commit 4363429

Please sign in to comment.