@@ -8,34 +8,9 @@ use std::error::Error;
8
8
9
9
#[ tokio:: main]
10
10
async fn main ( ) -> Result < ( ) , Box < dyn Error + Send + Sync > > {
11
- let account = std:: env:: var ( "ADLSGEN2_STORAGE_ACCOUNT" )
12
- . expect ( "Set env variable ADLSGEN2_STORAGE_ACCOUNT first!" ) ;
13
- let master_key = std:: env:: var ( "ADLSGEN2_STORAGE_MASTER_KEY" )
14
- . expect ( "Set env variable ADLSGEN2_STORAGE_MASTER_KEY first!" ) ;
15
-
16
- let now = Utc :: now ( ) ;
17
- let file_system_name = format ! ( "azurerustsdk-datalake-example01-{}" , now. timestamp( ) ) ;
18
-
19
- let http_client = new_http_client ( ) ;
20
-
21
- let storage_account_client =
22
- StorageAccountClient :: new_access_key ( http_client. clone ( ) , & account, & master_key) ;
23
-
24
- let resource_id = "https://storage.azure.com/" ;
25
- println ! ( "getting bearer token for '{}'..." , resource_id) ;
26
- let bearer_token = DefaultAzureCredential :: default ( )
27
- . get_token ( resource_id)
28
- . await ?;
29
- println ! ( "token expires on {}\n " , bearer_token. expires_on) ;
30
-
31
- let storage_client = storage_account_client. as_storage_client ( ) ;
32
- let data_lake_client = DataLakeClient :: new (
33
- storage_client,
34
- account,
35
- bearer_token. token . secret ( ) . to_owned ( ) ,
36
- None ,
37
- ) ;
11
+ let data_lake_client = create_data_lake_client ( ) . await . unwrap ( ) ;
38
12
13
+ let file_system_name = format ! ( "azurerustsdk-datalake-example01-{}" , Utc :: now( ) . timestamp( ) ) ;
39
14
let file_system_client = data_lake_client
40
15
. clone ( )
41
16
. into_file_system_client ( file_system_name. to_string ( ) ) ;
@@ -52,67 +27,81 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
52
27
. await ?;
53
28
println ! ( "create file response == {:?}\n " , create_file_response) ;
54
29
55
- println ! ( "creating file '{}' (overwrite)..." , file_path) ;
56
- let create_file_response = file_system_client
57
- . create_file ( Context :: default ( ) , file_path, FileCreateOptions :: default ( ) )
58
- . await ?;
59
- println ! ( "create file response == {:?}\n " , create_file_response) ;
30
+ let string1 = "some data" ;
31
+ let data1 = bytes:: Bytes :: from ( string1) ;
32
+ let data1_length = data1. len ( ) as i64 ;
33
+
34
+ let string2 = "some more data" ;
35
+ let data2 = bytes:: Bytes :: from ( string2) ;
36
+ let data2_length = data2. len ( ) as i64 ;
60
37
61
- println ! ( "creating file '{}' if not exists..." , file_path) ;
62
- let create_file_if_not_exists_result = file_system_client
63
- . create_file_if_not_exists ( Context :: default ( ) , file_path)
64
- . await ;
65
- println ! (
66
- "create file result (should fail) == {:?}\n " ,
67
- create_file_if_not_exists_result
68
- ) ;
69
-
70
- println ! ( "appending to file '{}'..." , file_path) ;
71
- let bytes = bytes:: Bytes :: from ( "some data" ) ;
72
- let file_length = bytes. len ( ) as i64 ;
38
+ println ! ( "appending '{}' to file '{}'..." , string1, file_path) ;
73
39
let append_to_file_response = file_system_client
74
40
. append_to_file (
75
41
Context :: default ( ) ,
76
42
file_path,
77
- bytes ,
43
+ data1 ,
78
44
0 ,
79
45
FileAppendOptions :: default ( ) ,
80
46
)
81
47
. await ?;
82
48
println ! ( "append to file response == {:?}\n " , append_to_file_response) ;
83
49
50
+ println ! ( "appending '{}' to file '{}'..." , string2, file_path) ;
51
+ let append_to_file_response = file_system_client
52
+ . append_to_file (
53
+ Context :: default ( ) ,
54
+ file_path,
55
+ data2,
56
+ data1_length,
57
+ FileAppendOptions :: default ( ) ,
58
+ )
59
+ . await ?;
60
+ println ! ( "append to file response == {:?}\n " , append_to_file_response) ;
61
+
84
62
println ! ( "flushing file '{}'..." , file_path) ;
85
63
let flush_file_response = file_system_client
86
64
. flush_file (
87
65
Context :: default ( ) ,
88
66
file_path,
89
- file_length ,
67
+ data1_length + data2_length ,
90
68
true ,
91
69
FileFlushOptions :: default ( ) ,
92
70
)
93
71
. await ?;
94
72
println ! ( "flush file response == {:?}\n " , flush_file_response) ;
95
73
96
- let destination_file_path = "some/path/example-file-renamed.txt" ;
97
- println ! (
98
- "renaming file '{}' to '{}'..." ,
99
- file_path, destination_file_path
100
- ) ;
101
- let rename_file_response = file_system_client
102
- . rename_file (
103
- Context :: default ( ) ,
104
- file_path,
105
- destination_file_path,
106
- FileRenameOptions :: default ( ) ,
107
- )
108
- . await ?;
109
- println ! ( "rename file response == {:?}\n " , rename_file_response) ;
110
-
111
74
println ! ( "deleting file system..." ) ;
112
75
let delete_fs_response = file_system_client. delete ( ) . execute ( ) . await ?;
113
76
println ! ( "delete file system response == {:?}\n " , delete_fs_response) ;
114
77
115
- println ! ( "data lake example 01 done." ) ;
116
-
117
78
Ok ( ( ) )
118
79
}
80
+
81
+ async fn create_data_lake_client ( ) -> Result < DataLakeClient , Box < dyn Error + Send + Sync > > {
82
+ let account = std:: env:: var ( "ADLSGEN2_STORAGE_ACCOUNT" )
83
+ . expect ( "Set env variable ADLSGEN2_STORAGE_ACCOUNT first!" ) ;
84
+ let master_key = std:: env:: var ( "ADLSGEN2_STORAGE_MASTER_KEY" )
85
+ . expect ( "Set env variable ADLSGEN2_STORAGE_MASTER_KEY first!" ) ;
86
+
87
+ let http_client = new_http_client ( ) ;
88
+
89
+ let storage_account_client =
90
+ StorageAccountClient :: new_access_key ( http_client. clone ( ) , & account, & master_key) ;
91
+
92
+ let resource_id = "https://storage.azure.com/" ;
93
+ println ! ( "getting bearer token for '{}'..." , resource_id) ;
94
+ let bearer_token = DefaultAzureCredential :: default ( )
95
+ . get_token ( resource_id)
96
+ . await ?;
97
+ println ! ( "token expires on {}\n " , bearer_token. expires_on) ;
98
+
99
+ let storage_client = storage_account_client. as_storage_client ( ) ;
100
+
101
+ Ok ( DataLakeClient :: new (
102
+ storage_client,
103
+ account,
104
+ bearer_token. token . secret ( ) . to_owned ( ) ,
105
+ None ,
106
+ ) )
107
+ }
0 commit comments