From bf3c0c19daf8d4647fc09baa3f7fc268886357be Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sun, 23 May 2021 02:31:31 -0400 Subject: [PATCH] bucket client (but needs tokio runtime!) --- .env | 4 + .gitignore | 1 + Cargo.lock | 238 +++++++++++++++++--------------------- Cargo.toml | 4 +- src/tts_download_job.rs | 39 ++++++- src/util/bucket_client.rs | 106 +++++++++++++++++ src/util/mod.rs | 1 + 7 files changed, 261 insertions(+), 132 deletions(-) create mode 100644 src/util/bucket_client.rs diff --git a/.env b/.env index 09610ddca7..039a586908 100644 --- a/.env +++ b/.env @@ -8,3 +8,7 @@ DATABASE_URL=mysql://root:root@localhost/storyteller # We can't serve HTTPS locally without self-signed certificate nonsense. COOKIE_SECURE=false COOKIE_DOMAIN=.jungle.horse + +# Bucket config (non-secret) +REGION_NAME=us-east1 +BUCKET_NAME=dev-vocodes-private-uploads diff --git a/.gitignore b/.gitignore index e726beb732..e68a75a505 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ *secret*sh *~ .env +.env-secrets .idea/ _ignored_schema.rs production_database_secret_connect.sh diff --git a/Cargo.lock b/Cargo.lock index 5fcd5bd984..bb0df3ab05 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15,7 +15,7 @@ dependencies = [ "log", "pin-project 0.4.28", "tokio 0.2.25", - "tokio-util 0.3.1", + "tokio-util", ] [[package]] @@ -97,7 +97,7 @@ dependencies = [ "futures-core", "futures-util", "fxhash", - "h2 0.2.7", + "h2", "http", "httparse", "indexmap", @@ -334,6 +334,12 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "ahash" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8fd72866655d1904d6b0997d0b07ba561047d070fbe29de039031c641b61217" + [[package]] name = "ahash" version = "0.4.7" @@ -553,9 +559,9 @@ checksum = "065374052e7df7ee4047b1160cca5e1467a12351a40b3da123c870ba0b8eda2a" [[package]] name = "attohttpc" -version = "0.16.3" +version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fdb8867f378f33f78a811a8eb9bf108ad99430d7aad43315dd9319c827ef6247" +checksum = "fe174d1b67f7b2bafed829c09db039301eb5841f66e43be2cf60b326e7f8e2cc" dependencies = [ "http", "log", @@ -564,7 +570,6 @@ dependencies = [ "serde", "serde_json", "url 2.2.2", - "wildmatch", ] [[package]] @@ -616,27 +621,27 @@ dependencies = [ [[package]] name = "aws-creds" -version = "0.26.0" +version = "0.24.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1331d069460a674d42bd27c12b47ce578f789954c7bd7f239fd030771eca6616" +checksum = "ad53a54cb2c99990e96eefacde6f143dc6d471ab70809d26d360292be421d490" dependencies = [ - "anyhow", "attohttpc", "dirs", "rust-ini", "serde", "serde-xml-rs", "serde_derive", + "simpl", "url 2.2.2", ] [[package]] name = "aws-region" -version = "0.23.2" +version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2884b8f2aaeb4a4bf80b219b4fe1d340139ca9331679c57e0fd4a24f571a78bd" +checksum = "f610af4a396f07592014dc3410f6ad78fab931852a99bb6cfdc1ad04b9329b80" dependencies = [ - "anyhow", + "simpl", ] [[package]] @@ -948,6 +953,16 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "crypto-mac" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58bcd97a54c7ca5ce2f6eb16f6bede5b0ab5f0055fedc17d2f0b4466e21671ca" +dependencies = [ + "generic-array", + "subtle", +] + [[package]] name = "crypto-mac" version = "0.10.0" @@ -1377,28 +1392,19 @@ dependencies = [ "indexmap", "slab", "tokio 0.2.25", - "tokio-util 0.3.1", + "tokio-util", "tracing", "tracing-futures", ] [[package]] -name = "h2" -version = "0.3.3" +name = "hashbrown" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "825343c4eef0b63f541f8903f395dc5beb362a979b5799a84062527ef1e37726" +checksum = "96282e96bfcd3da0d3aa9938bedf1e50df3269b6db08b4876d2da0bb1a0841cf" dependencies = [ - "bytes 1.0.1", - "fnv", - "futures-core", - "futures-sink", - "futures-util", - "http", - "indexmap", - "slab", - "tokio 1.5.0", - "tokio-util 0.6.7", - "tracing", + "ahash 0.3.8", + "autocfg 1.0.1", ] [[package]] @@ -1416,7 +1422,7 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d99cf782f0dc4372d26846bec3de7804ceb5df083c2d4462c0b8d2330e894fa8" dependencies = [ - "hashbrown", + "hashbrown 0.9.1", ] [[package]] @@ -1443,13 +1449,23 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "hmac" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "deae6d9dbb35ec2c502d62b8f7b1c000a0822c3b0794ba36b3149c0a1c840dff" +dependencies = [ + "crypto-mac 0.9.1", + "digest", +] + [[package]] name = "hmac" version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1441c6b1e930e2817404b5046f1f989899143a12bf92de603b69f4e0aee1e15" dependencies = [ - "crypto-mac", + "crypto-mac 0.10.0", "digest", ] @@ -1477,13 +1493,12 @@ dependencies = [ [[package]] name = "http-body" -version = "0.4.2" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60daa14be0e0786db0f03a9e57cb404c9d756eed2b6c62b9ea98ec5743ec75a9" +checksum = "13d5ff830006f7646652e057693569bfe0d51760c0085a071769d142a205111b" dependencies = [ - "bytes 1.0.1", + "bytes 0.5.6", "http", - "pin-project-lite 0.2.6", ] [[package]] @@ -1494,9 +1509,9 @@ checksum = "f3a87b616e37e93c22fb19bcd386f02f3af5ea98a25670ad0fce773de23c5e68" [[package]] name = "httpdate" -version = "1.0.0" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05842d0d43232b23ccb7060ecb0f0626922c21f30012e97b767b30afd4a5d4b9" +checksum = "494b4d60369511e7dea41cf646832512a94e542f68bb9c49e54518e0f468eb47" [[package]] name = "humantime" @@ -1509,23 +1524,23 @@ dependencies = [ [[package]] name = "hyper" -version = "0.14.7" +version = "0.13.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e5f105c494081baa3bf9e200b279e27ec1623895cd504c7dbef8d0b080fcf54" +checksum = "8a6f157065790a3ed2f88679250419b5cdd96e714a0d65f7797fd337186e96bb" dependencies = [ - "bytes 1.0.1", + "bytes 0.5.6", "futures-channel", "futures-core", "futures-util", - "h2 0.3.3", + "h2", "http", "http-body", "httparse", "httpdate", "itoa", "pin-project 1.0.7", - "socket2 0.4.0", - "tokio 1.5.0", + "socket2 0.3.19", + "tokio 0.2.25", "tower-service", "tracing", "want", @@ -1533,15 +1548,15 @@ dependencies = [ [[package]] name = "hyper-tls" -version = "0.5.0" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905" +checksum = "d979acc56dcb5b8dddba3917601745e877576475aa046df3226eabdecef78eed" dependencies = [ - "bytes 1.0.1", + "bytes 0.5.6", "hyper", "native-tls", - "tokio 1.5.0", - "tokio-native-tls", + "tokio 0.2.25", + "tokio-tls", ] [[package]] @@ -1573,7 +1588,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "824845a0bf897a9042383849b02c1bc219c2383772efcd5c6f9766fa4b81aef3" dependencies = [ "autocfg 1.0.1", - "hashbrown", + "hashbrown 0.9.1", ] [[package]] @@ -1634,9 +1649,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "86e46349d67dc03bdbdb28da0337a355a53ca1d5156452722c36fe21d0e6389b" dependencies = [ "base64 0.13.0", - "crypto-mac", + "crypto-mac 0.10.0", "digest", - "hmac", + "hmac 0.10.1", "serde", "serde_json", "sha2", @@ -1759,17 +1774,6 @@ version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" -[[package]] -name = "maybe-async" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3919783e441e38b0512fae9eebd87c445c42e39508231fba2a8b9fd7ba88e999" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "md-5" version = "0.9.1" @@ -1809,15 +1813,6 @@ dependencies = [ "unicase", ] -[[package]] -name = "minidom" -version = "0.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "332592c2149fc7dd40a64fc9ef6f0d65607284b474cef9817d1fc8c7e7b3608e" -dependencies = [ - "quick-xml", -] - [[package]] name = "miniz_oxide" version = "0.4.4" @@ -2091,12 +2086,12 @@ dependencies = [ [[package]] name = "ordered-multimap" -version = "0.3.1" +version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c672c7ad9ec066e428c00eb917124a06f08db19e2584de982cc34b1f4c12485" +checksum = "e88f947c6799d5eff50e6cf8a2365c17ac4aa8f8f43aceeedc29b616d872a358" dependencies = [ "dlv-list", - "hashbrown", + "hashbrown 0.7.2", ] [[package]] @@ -2263,15 +2258,6 @@ version = "1.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" -[[package]] -name = "quick-xml" -version = "0.20.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26aab6b48e2590e4a64d1ed808749ba06257882b461d01ca71baeb747074a6dd" -dependencies = [ - "memchr", -] - [[package]] name = "quote" version = "1.0.9" @@ -2452,12 +2438,12 @@ dependencies = [ [[package]] name = "reqwest" -version = "0.11.3" +version = "0.10.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2296f2fac53979e8ccbc4a1136b25dcefd37be9ed7e4a1f6b05a6029c84ff124" +checksum = "0718f81a8e14c4dbb3b34cf23dc6aaf9ab8a0dfec160c534b3dbca1aaa21f47c" dependencies = [ "base64 0.13.0", - "bytes 1.0.1", + "bytes 0.5.6", "encoding_rs", "futures-core", "futures-util", @@ -2470,14 +2456,15 @@ dependencies = [ "lazy_static", "log", "mime", + "mime_guess", "native-tls", "percent-encoding 2.1.0", "pin-project-lite 0.2.6", "serde", "serde_json", "serde_urlencoded", - "tokio 1.5.0", - "tokio-native-tls", + "tokio 0.2.25", + "tokio-tls", "url 2.2.2", "wasm-bindgen", "wasm-bindgen-futures", @@ -2532,9 +2519,9 @@ dependencies = [ [[package]] name = "rust-ini" -version = "0.16.1" +version = "0.15.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55b134767a87e0b086f73a4ce569ac9ce7d202f39c8eab6caa266e2617e73ac6" +checksum = "7a3679dd538c876a7b606f3bb951c8a20fc281a0ff7795f59f7cb490e3f979e1" dependencies = [ "cfg-if 0.1.10", "ordered-multimap", @@ -2542,12 +2529,11 @@ dependencies = [ [[package]] name = "rust-s3" -version = "0.27.0-rc4" +version = "0.26.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c93272c1d654d492f8ab30b94cd43d98f2700b1db55b2576aff7712ce40e3ef" +checksum = "a04bdd0f5118d06ef0e2daa658a9e5135282bcecfd6c46b11fddf47b1d5d736d" dependencies = [ - "anyhow", - "async-trait", + "async-std", "aws-creds", "aws-region", "base64 0.13.0", @@ -2555,21 +2541,20 @@ dependencies = [ "chrono", "futures", "hex", - "hmac", + "hmac 0.9.0", "http", "log", - "maybe-async", "md5", - "minidom", "percent-encoding 2.1.0", "reqwest", "serde", "serde-xml-rs", "serde_derive", "sha2", - "tokio 1.5.0", - "tokio-stream", + "simpl", + "tokio 0.2.25", "url 2.2.2", + "uuid", ] [[package]] @@ -2748,6 +2733,12 @@ dependencies = [ "libc", ] +[[package]] +name = "simpl" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a30f10c911c0355f80f1c2faa8096efc4a58cdf8590b954d5b395efa071c711" + [[package]] name = "simple_asn1" version = "0.5.2" @@ -2985,10 +2976,11 @@ dependencies = [ "data-encoding", "derive_more", "diesel", + "dotenv", "easyenv", "futures", "hex", - "hmac", + "hmac 0.10.1", "hostname", "jwt", "lazy_static", @@ -3186,6 +3178,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6703a273949a90131b290be1fe7b039d0fc884aa1935860dfcbe056f28cd8092" dependencies = [ "bytes 0.5.6", + "fnv", "futures-core", "iovec", "lazy_static", @@ -3196,6 +3189,7 @@ dependencies = [ "pin-project-lite 0.1.12", "signal-hook-registry", "slab", + "tokio-macros", "winapi 0.3.9", ] @@ -3206,9 +3200,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "83f0c8e7c0addab50b663055baf787d0af7f413a46e6e7fb9559a4e4db7137a5" dependencies = [ "autocfg 1.0.1", - "bytes 1.0.1", "libc", - "memchr", "mio 0.7.11", "once_cell", "parking_lot", @@ -3218,24 +3210,24 @@ dependencies = [ ] [[package]] -name = "tokio-native-tls" -version = "0.3.0" +name = "tokio-macros" +version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7d995660bd2b7f8c1568414c1126076c13fbb725c40112dc0120b78eb9b717b" +checksum = "e44da00bfc73a25f814cd8d7e57a68a5c31b74b3152a0a1d1f590c97ed06265a" dependencies = [ - "native-tls", - "tokio 1.5.0", + "proc-macro2", + "quote", + "syn", ] [[package]] -name = "tokio-stream" -version = "0.1.6" +name = "tokio-tls" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8864d706fdb3cc0843a49647ac892720dac98a6eeb818b77190592cf4994066" +checksum = "9a70f4fcd7b3b24fb194f837560168208f669ca8cb70d0c4b862944452396343" dependencies = [ - "futures-core", - "pin-project-lite 0.2.6", - "tokio 1.5.0", + "native-tls", + "tokio 0.2.25", ] [[package]] @@ -3252,20 +3244,6 @@ dependencies = [ "tokio 0.2.25", ] -[[package]] -name = "tokio-util" -version = "0.6.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1caa0b0c8d94a049db56b5acf8cba99dc0623aab1b26d5b5f5e2d945846b3592" -dependencies = [ - "bytes 1.0.1", - "futures-core", - "futures-sink", - "log", - "pin-project-lite 0.2.6", - "tokio 1.5.0", -] - [[package]] name = "toml" version = "0.5.8" @@ -3437,6 +3415,12 @@ dependencies = [ "percent-encoding 2.1.0", ] +[[package]] +name = "uuid" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7" + [[package]] name = "v_escape" version = "0.15.0" @@ -3627,12 +3611,6 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c168940144dd21fd8046987c16a46a33d5fc84eec29ef9dcddc2ac9e31526b7c" -[[package]] -name = "wildmatch" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f44b95f62d34113cf558c93511ac93027e03e9c29a60dd0fd70e6e025c7270a" - [[package]] name = "winapi" version = "0.2.8" diff --git a/Cargo.toml b/Cargo.toml index a12bd83942..7539f7ec29 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,7 @@ async-std = { version = "1.6", features = [ "attributes" ] } base64 = "0.13.0" chrono = { version = "0.4.19", features = ["serde"] } derive_more = "0.99.14" +dotenv = "0.15.0" easyenv = "0.0.5" futures = "0.3.15" hex = "0.4.3" @@ -55,7 +56,8 @@ sqlx = { version = "0.5", features = [ "mysql", "runtime-async-std-native-tls", # Files data-encoding = "2.3.2" ring = "0.17.0-alpha.10" -rust-s3 = "0.27.0-rc4" +#rust-s3 = "0.27.0-rc4" +rust-s3 = "0.26.3" tempdir = "0.3.7" # Web diff --git a/src/tts_download_job.rs b/src/tts_download_job.rs index 6e215d4623..f06a4b89cf 100644 --- a/src/tts_download_job.rs +++ b/src/tts_download_job.rs @@ -10,6 +10,7 @@ pub mod util; use anyhow::anyhow; use chrono::Utc; +use crate::util::bucket_client::BucketClient; use crate::job::job_queries::TtsUploadJobRecord; use crate::job::job_queries::insert_tts_model; use crate::job::job_queries::mark_tts_upload_job_done; @@ -21,7 +22,7 @@ use crate::util::random_token::random_token; use log::{warn, info}; use sqlx::MySqlPool; use sqlx::mysql::MySqlPoolOptions; -use std::path::PathBuf; +use std::path::{PathBuf, Path}; use std::process::Command; use std::time::Duration; use tempdir::TempDir; @@ -30,12 +31,19 @@ use std::io::{BufReader, Read}; use ring::digest::{Context, Digest, SHA256}; use data_encoding::{HEXUPPER, HEXLOWER, HEXLOWER_PERMISSIVE}; +// Buckets +const ENV_ACCESS_KEY : &'static str = "ACCESS_KEY"; +const ENV_SECRET_KEY : &'static str = "SECRET_KEY"; +const ENV_REGION_NAME : &'static str = "REGION_NAME"; +const ENV_BUCKET_NAME : &'static str = "BUCKET_NAME"; + const DEFAULT_RUST_LOG: &'static str = "debug,actix_web=info"; const DEFAULT_TEMP_DIR: &'static str = "/tmp"; struct Downloader { pub download_temp_directory: PathBuf, pub mysql_pool: MySqlPool, + pub bucket_client: BucketClient, // Command to run pub download_script: String, } @@ -44,6 +52,8 @@ struct Downloader { async fn main() -> AnyhowResult<()> { easyenv::init_all_with_default_logging(Some(DEFAULT_RUST_LOG)); + let _ = dotenv::from_filename(".env-secrets").ok(); + info!("Obtaining hostname..."); let server_hostname = hostname::get() @@ -53,6 +63,19 @@ async fn main() -> AnyhowResult<()> { info!("Hostname: {}", &server_hostname); + // Bucket stuff + let access_key = easyenv::get_env_string_required(ENV_ACCESS_KEY)?; + let secret_key = easyenv::get_env_string_required(ENV_SECRET_KEY)?; + let region_name = easyenv::get_env_string_required(ENV_REGION_NAME)?; + let bucket_name = easyenv::get_env_string_required(ENV_BUCKET_NAME)?; + + let bucket_client = BucketClient::create( + &access_key, + &secret_key, + ®ion_name, + &bucket_name, + )?; + let temp_directory = easyenv::get_env_string_or_default( "DOWNLOAD_TEMP_DIR", DEFAULT_TEMP_DIR); @@ -80,6 +103,7 @@ async fn main() -> AnyhowResult<()> { let downloader = Downloader { download_temp_directory: temp_directory, mysql_pool, + bucket_client, download_script, }; @@ -231,6 +255,19 @@ async fn process_job(downloader: &Downloader, job: &TtsUploadJobRecord) -> Anyho info!("File hash: {}", private_bucket_hash); + let object_name = format!( + "user_uploaded_tts_models/{}/{}/{}/{}.bin", + &private_bucket_hash[0..1], + &private_bucket_hash[1..2], + &private_bucket_hash[2..3], + &private_bucket_hash, + ); + + info!("Destination bucket path: {}", object_name); + + let file_path = PathBuf::from(download_filename); + downloader.bucket_client.upload_filename(&object_name, &file_path).await?; + info!("Saving model record..."); let id = insert_tts_model(&downloader.mysql_pool, job, &private_bucket_hash).await?; diff --git a/src/util/bucket_client.rs b/src/util/bucket_client.rs new file mode 100644 index 0000000000..1da9ec95b2 --- /dev/null +++ b/src/util/bucket_client.rs @@ -0,0 +1,106 @@ +use anyhow::bail; +use log::info; +use s3::bucket::Bucket; +use s3::creds::Credentials; +use s3::region::Region; +use std::path::{PathBuf, Path}; +use std::str::FromStr; +use std::fs::File; +use std::io::Read; + +#[derive(Clone)] +pub struct BucketClient { + bucket: Bucket, +} + +impl BucketClient { + + pub fn create( + access_key: &str, + secret_key: &str, + region_name: &str, + bucket_name: &str) -> anyhow::Result + { + let credentials = Credentials::new( + Some(&access_key), + Some(&secret_key), + None, + None, + None, + )?; + // NB: The GCS buckets aren't supported by default. + let region = Region::Custom { + region: region_name.to_owned(), + endpoint: "https://storage.googleapis.com".to_owned(), + }; + let bucket = Bucket::new(&bucket_name, region, credentials)?; + + Ok(Self { + bucket, + }) + } + + pub async fn upload_file(&self, object_name: &str, bytes: &[u8]) -> anyhow::Result<()> { + info!("Filename for bucket: {}", object_name); + + let (_, code) = self.bucket.put_object(object_name, bytes).await?; + + info!("upload code: {}", code); + + Ok(()) + } + + pub fn upload_file_blocking(&self, object_name: &str, bytes: &[u8]) -> anyhow::Result<()> { + info!("Filename for bucket: {}", object_name); + + let (_, code) = self.bucket.put_object_blocking(object_name, bytes)?; + + info!("upload code: {}", code); + + Ok(()) + } + + pub async fn upload_filename(&self, object_name: &str, filename: &Path) -> anyhow::Result<()> { + // TODO: does a newer version of this crate handle streaming/buffering file contents? + let mut file = File::open(filename)?; + let mut buffer : Vec = Vec::new(); + file.read_to_end(&mut buffer)?; + + self.upload_file(object_name, &buffer).await + } + + pub fn upload_filename_blocking(&self, object_name: &str, filename: &Path) -> anyhow::Result<()> { + // TODO: does a newer version of this crate handle streaming/buffering file contents? + let mut file = File::open(filename)?; + let mut buffer : Vec = Vec::new(); + file.read_to_end(&mut buffer)?; + + self.upload_file_blocking(object_name, &buffer) + } + + pub async fn download_file(&self, path: &str) -> anyhow::Result> { + info!("downloading from bucket: {}", path); + let (bytes, code) = self.bucket.get_object(path).await?; + + match code { + 404 => bail!("File not found in bucket: {}", path), + _ => {}, + } + + info!("download code: {}", code); + Ok(bytes) + } + + pub fn download_file_blocking(&self, path: &str) -> anyhow::Result> { + info!("downloading from bucket: {}", path); + let (bytes, code) = self.bucket.get_object_blocking(path)?; + + match code { + 404 => bail!("File not found in bucket: {}", path), + _ => {}, + } + + info!("download code: {}", code); + Ok(bytes) + } +} diff --git a/src/util/mod.rs b/src/util/mod.rs index 3dda00a986..61b5049457 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1,4 +1,5 @@ pub mod anyhow_result; +pub mod bucket_client; pub mod cookie_manager; pub mod filesystem; pub mod ip_address;