1
- use minio:: s3:: args:: { BucketExistsArgs , MakeBucketArgs } ;
1
+ // MinIO Rust Library for Amazon S3 Compatible Cloud Storage
2
+ // Copyright 2025 MinIO, Inc.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+
16
+ mod common;
17
+
18
+ use crate :: common:: { create_bucket_if_not_exists, create_client_on_play} ;
2
19
use minio:: s3:: builders:: ObjectContent ;
3
- use minio:: s3:: client:: ClientBuilder ;
4
- use minio:: s3:: creds:: StaticProvider ;
5
- use minio:: s3:: http:: BaseUrl ;
6
20
use minio:: s3:: types:: S3Api ;
21
+ use minio:: s3:: Client ;
7
22
use std:: path:: Path ;
8
23
9
24
#[ tokio:: main]
10
25
async fn main ( ) -> Result < ( ) , Box < dyn std:: error:: Error + Send + Sync > > {
11
26
env_logger:: init ( ) ; // Note: set environment variable RUST_LOG="INFO" to log info and higher
12
-
13
- let base_url = "https://play.min.io" . parse :: < BaseUrl > ( ) ?;
14
- log:: info!( "Trying to connect to MinIO at: `{:?}`" , base_url) ;
15
-
16
- let static_provider = StaticProvider :: new (
17
- "Q3AM3UQ867SPQQA43P2F" ,
18
- "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG" ,
19
- None ,
20
- ) ;
21
-
22
- let client = ClientBuilder :: new ( base_url. clone ( ) )
23
- . provider ( Some ( Box :: new ( static_provider) ) )
24
- . build ( ) ?;
27
+ let client: Client = create_client_on_play ( ) ?;
25
28
26
29
let bucket_name: & str = "file-download-rust-bucket" ;
27
30
let object_name: & str = "cat.png" ;
@@ -30,19 +33,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
30
33
let filename: & Path = Path :: new ( "./examples/cat.png" ) ;
31
34
let download_path: & str = & format ! ( "/tmp/downloads/{object_name}" ) ;
32
35
33
- // Check 'bucket_name' bucket exist or not.
34
- let exists: bool = client
35
- . bucket_exists ( & BucketExistsArgs :: new ( bucket_name) . unwrap ( ) )
36
- . await
37
- . unwrap ( ) ;
38
-
39
- // Make 'bucket_name' bucket if not exist.
40
- if !exists {
41
- client
42
- . make_bucket ( & MakeBucketArgs :: new ( bucket_name) . unwrap ( ) )
43
- . await
44
- . unwrap ( ) ;
45
- }
36
+ create_bucket_if_not_exists ( bucket_name, & client) . await ?;
46
37
47
38
if filename. exists ( ) {
48
39
log:: info!( "File '{}' exists." , & filename. to_str( ) . unwrap( ) ) ;
@@ -64,10 +55,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
64
55
65
56
let get_object = client. get_object ( bucket_name, object_name) . send ( ) . await ?;
66
57
67
- get_object
68
- . content
69
- . to_file ( & Path :: new ( download_path) )
70
- . await ?;
58
+ get_object. content . to_file ( Path :: new ( download_path) ) . await ?;
71
59
72
60
log:: info!( "Object '{object_name}' is successfully downloaded to file '{download_path}'." ) ;
73
61
0 commit comments