use anyhow::{Context, Result};
use garage_sdk::{GarageUploader, UploaderConfig};
use tracing::{error, info};
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();
let config =
UploaderConfig::from_env().context("failed to build uploader config from environment")?;
let uploader = GarageUploader::new(config).context("failed to initialize uploader")?;
match std::env::var("GARAGE_EXAMPLE_FILE") {
Ok(path) => {
info!(path = %path, "uploading local file");
match uploader.upload_from_path(&path).await {
Ok(result) => {
info!(
public_url = %result.public_url,
key = %result.key,
size = result.size,
content_type = %result.content_type,
"uploaded local file"
);
}
Err(e) => {
error!(error = %e, "failed to upload local file");
}
}
}
Err(_) => {
info!("set GARAGE_EXAMPLE_FILE to upload a local file");
}
}
match std::env::var("GARAGE_EXAMPLE_URL") {
Ok(remote_url) => {
info!(url = %remote_url, "uploading from URL");
match uploader.upload_from_url(&remote_url).await {
Ok(result) => {
info!(
public_url = %result.public_url,
key = %result.key,
size = result.size,
content_type = %result.content_type,
"uploaded from URL"
);
}
Err(e) => {
error!(error = %e, "failed to upload from URL");
}
}
}
Err(_) => {
info!("set GARAGE_EXAMPLE_URL to upload from a remote URL");
}
}
info!("uploading raw bytes");
let sample_json = r#"{"message": "Hello, World!"}"#;
match uploader
.upload_bytes(
sample_json.as_bytes().to_vec(),
"application/json",
Some("json"),
)
.await
{
Ok(result) => {
info!(
public_url = %result.public_url,
key = %result.key,
size = result.size,
"uploaded raw bytes"
);
}
Err(e) => {
error!(error = %e, "failed to upload bytes");
}
}
Ok(())
}