Skip to content

Commit

Permalink
update: example file-file-uploader
Browse files Browse the repository at this point in the history
  • Loading branch information
HJLebbink committed Feb 27, 2024
1 parent cfdf41c commit 28da7b6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ tokio-stream = "0.1.14"
tokio-util = { version = "0.7.8", features = ["io"] }
urlencoding = "2.1.3"
xmltree = "0.10.3"
log = "0.4.20"
env_logger = "0.11.2"
home = "0.5.9"

[dependencies.reqwest]
version = "0.11.20"
Expand Down
48 changes: 34 additions & 14 deletions examples/file-uploader.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
use log::{error, info};
use minio::s3::args::{BucketExistsArgs, MakeBucketArgs, UploadObjectArgs};
use minio::s3::client::Client;
use minio::s3::creds::StaticProvider;
use minio::s3::http::BaseUrl;
use std::path::Path;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let base_url = "https://play.min.io".parse::<BaseUrl>()?;
env_logger::init(); // Note: set environment variable RUST_LOG="INFO" to log info and higher

//let base_url = "https://play.min.io".parse::<BaseUrl>()?;
let base_url: BaseUrl = "http://192.168.178.227:9000".parse::<BaseUrl>()?;

info!("Trying to connect to MinIO at: `{:?}`", base_url);

let static_provider = StaticProvider::new(
"Q3AM3UQ867SPQQA43P2F",
Expand All @@ -21,36 +28,49 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
)
.unwrap();

let bucket_name = "asiatrip";
let bucket_name: &str = "asiatrip";

// Check 'asiatrip' bucket exist or not.
let exists = client
// Check 'bucket_name' bucket exist or not.
let exists: bool = client
.bucket_exists(&BucketExistsArgs::new(&bucket_name).unwrap())
.await
.unwrap();

// Make 'asiatrip' bucket if not exist.
// Make 'bucket_name' bucket if not exist.
if !exists {
client
.make_bucket(&MakeBucketArgs::new(&bucket_name).unwrap())
.await
.unwrap();
}

// Upload '/home/user/Photos/asiaphotos.zip' as object name
// 'asiaphotos-2015.zip' to bucket 'asiatrip'.
// File we are going to upload to the bucket
let filename: &Path = Path::new("/home/user/Photos/asiaphotos.zip");

// Name of the object that will be stored in the bucket
let object_name: &str = "asiaphotos-2015.zip";

info!("filename {}", &filename.to_str().unwrap());

// Check if the file exists
let file_exists: bool = filename.exists();
if !file_exists {
error!("File `{}` does not exist!", filename.display());
()
}

// Upload 'filename' as 'object_name' to bucket 'bucket_name'.
client
.upload_object(
&mut UploadObjectArgs::new(
&bucket_name,
"asiaphotos-2015.zip",
"/home/user/Photos/asiaphotos.zip",
)
.unwrap(),
&mut UploadObjectArgs::new(&bucket_name, &object_name, &filename.to_str().unwrap())
.unwrap(),
)
.await
.unwrap();

println!("'/home/user/Photos/asiaphotos.zip' is successfully uploaded as object 'asiaphotos-2015.zip' to bucket 'asiatrip'.");
info!(
"file `{}` is successfully uploaded as object `{object_name}` to bucket `{bucket_name}`.",
filename.display()
);
Ok(())
}
2 changes: 1 addition & 1 deletion src/s3/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ impl BaseUrl {
host: self.host.clone(),
port: self.port,
path: String::from("/"),
query: query.clone()
query: query.clone(),
};

if bucket_name.is_none() {
Expand Down

0 comments on commit 28da7b6

Please sign in to comment.