|
| 1 | +use azure_core::prelude::IfMatchCondition; |
1 | 2 | use azure_core::prelude::*;
|
| 3 | +use azure_identity::token_credentials::DefaultCredential; |
| 4 | +use azure_identity::token_credentials::TokenCredential; |
2 | 5 | use azure_storage::core::prelude::*;
|
3 | 6 | use azure_storage::data_lake::prelude::*;
|
| 7 | +use chrono::Utc; |
4 | 8 | use futures::stream::StreamExt;
|
5 | 9 | use std::error::Error;
|
6 | 10 | use std::num::NonZeroU32;
|
7 | 11 |
|
8 | 12 | #[tokio::main]
|
9 | 13 | async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
|
10 |
| - // First we retrieve the account name and master key from environment variables. |
11 |
| - let account = std::env::var("ADSL_STORAGE_ACCOUNT") |
12 |
| - .expect("Set env variable ADSL_STORAGE_ACCOUNT first!"); |
13 |
| - let master_key = std::env::var("ADSL_STORAGE_MASTER_KEY") |
14 |
| - .expect("Set env variable ADSL_STORAGE_MASTER_KEY first!"); |
| 14 | + let account = std::env::var("ADLSGEN2_STORAGE_ACCOUNT") |
| 15 | + .expect("Set env variable ADLSGEN2_STORAGE_ACCOUNT first!"); |
| 16 | + let master_key = std::env::var("ADLSGEN2_STORAGE_MASTER_KEY") |
| 17 | + .expect("Set env variable ADLSGEN2_STORAGE_MASTER_KEY first!"); |
15 | 18 |
|
16 |
| - let file_system_name = std::env::args() |
17 |
| - .nth(1) |
18 |
| - .expect("please specify the file system name as first parameter"); |
| 19 | + let now = Utc::now(); |
| 20 | + let file_system_name = format!("azurerustsdk-datalake-example-{}", now.timestamp()); |
19 | 21 |
|
20 | 22 | let http_client = new_http_client();
|
21 | 23 |
|
22 | 24 | let storage_account_client =
|
23 | 25 | StorageAccountClient::new_access_key(http_client.clone(), &account, &master_key);
|
24 | 26 |
|
| 27 | + let resource_id = "https://storage.azure.com/"; |
| 28 | + println!("getting bearer token for '{}'...", resource_id); |
| 29 | + let bearer_token = DefaultCredential::default().get_token(resource_id).await?; |
| 30 | + println!("token expires on {}", bearer_token.expires_on); |
| 31 | + println!(); |
| 32 | + |
25 | 33 | let data_lake = storage_account_client
|
26 | 34 | .as_storage_client()
|
27 |
| - .as_data_lake_client(account)?; |
| 35 | + .as_data_lake_client(account, bearer_token.token.secret().to_owned())?; |
| 36 | + |
| 37 | + let file_system = data_lake.as_file_system_client(&file_system_name)?; |
28 | 38 |
|
29 |
| - let file_system = data_lake.as_file_system_client(file_system_name)?; |
| 39 | + let mut fs_properties = Properties::new(); |
| 40 | + fs_properties.insert("AddedVia", "Azure SDK for Rust"); |
30 | 41 |
|
31 |
| - // let's add some metadata. We call them "properties" to be consistent with the REST API definition from |
32 |
| - // [https://docs.microsoft.com/rest/api/storageservices/datalakestoragegen2/filesystem/create#request-headers](https://docs.microsoft.com/rest/api/storageservices/datalakestoragegen2/filesystem/create#request-headers) |
33 |
| - let mut properties = Properties::new(); |
34 |
| - properties.insert("AddedVia", "Azure SDK for Rust"); |
35 |
| - properties.insert("CreatedAt", chrono::Utc::now().to_string()); |
36 |
| - let response = file_system |
| 42 | + println!("creating file system '{}'...", &file_system_name); |
| 43 | + let create_fs_response = file_system |
37 | 44 | .create()
|
38 |
| - .properties(&properties) |
| 45 | + .properties(&fs_properties) |
39 | 46 | .execute()
|
40 | 47 | .await?;
|
41 |
| - println!("repsonse == {:?}", response); |
| 48 | + println!("create file system response == {:?}", create_fs_response); |
| 49 | + println!(); |
42 | 50 |
|
| 51 | + println!("listing file systems..."); |
43 | 52 | let mut stream = Box::pin(
|
44 | 53 | data_lake
|
45 | 54 | .list()
|
46 | 55 | .max_results(NonZeroU32::new(3).unwrap())
|
47 | 56 | .stream(),
|
48 | 57 | );
|
49 |
| - |
50 |
| - while let Some(response) = stream.next().await { |
51 |
| - println!("response == {:?}\n\n", response); |
| 58 | + while let Some(list_fs_response) = stream.next().await { |
| 59 | + println!("list file system response == {:?}", list_fs_response); |
| 60 | + println!(); |
52 | 61 | }
|
53 | 62 |
|
54 |
| - properties.insert("ModifiedBy", "Iota"); |
55 |
| - let response = file_system |
56 |
| - .set_properties(Some(&properties)) |
| 63 | + println!("getting file system properties..."); |
| 64 | + let get_fs_props_response = file_system.get_properties().execute().await?; |
| 65 | + println!( |
| 66 | + "get file system properties response == {:?}", |
| 67 | + get_fs_props_response |
| 68 | + ); |
| 69 | + println!(); |
| 70 | + |
| 71 | + let file_name = "example-file.txt"; |
| 72 | + |
| 73 | + println!("creating path '{}'...", file_name); |
| 74 | + let create_path_response = file_system |
| 75 | + .create_path(Context::default(), file_name, CreatePathOptions::default()) |
| 76 | + .await?; |
| 77 | + println!("create path response == {:?}", create_path_response); |
| 78 | + println!(); |
| 79 | + |
| 80 | + println!("creating path '{}' (overwrite)...", file_name); |
| 81 | + let create_path_response = file_system |
| 82 | + .create_path(Context::default(), file_name, CreatePathOptions::default()) |
| 83 | + .await?; |
| 84 | + println!("create path response == {:?}", create_path_response); |
| 85 | + println!(); |
| 86 | + |
| 87 | + println!("creating path '{}' (do not overwrite)...", file_name); |
| 88 | + let do_not_overwrite = |
| 89 | + CreatePathOptions::new().if_match_condition(IfMatchCondition::NotMatch("*")); |
| 90 | + let create_path_result = file_system |
| 91 | + .create_path(Context::default(), file_name, do_not_overwrite) |
| 92 | + .await; |
| 93 | + println!( |
| 94 | + "create path result (should fail) == {:?}", |
| 95 | + create_path_result |
| 96 | + ); |
| 97 | + println!(); |
| 98 | + |
| 99 | + println!("setting file system properties..."); |
| 100 | + fs_properties.insert("ModifiedBy", "Iota"); |
| 101 | + let set_fs_props_response = file_system |
| 102 | + .set_properties(Some(&fs_properties)) |
57 | 103 | .execute()
|
58 | 104 | .await?;
|
59 |
| - println!("response == {:?}\n\n", response); |
| 105 | + println!( |
| 106 | + "set file system properties response == {:?}", |
| 107 | + set_fs_props_response |
| 108 | + ); |
| 109 | + println!(); |
| 110 | + |
| 111 | + println!("getting file system properties..."); |
| 112 | + let get_fs_props_response = file_system.get_properties().execute().await?; |
| 113 | + println!( |
| 114 | + "get file system properties response == {:?}", |
| 115 | + get_fs_props_response |
| 116 | + ); |
| 117 | + println!(); |
60 | 118 |
|
61 |
| - let response = file_system.get_properties().execute().await?; |
62 |
| - println!("response == {:?}\n\n", response); |
| 119 | + println!("deleting file system..."); |
| 120 | + let delete_fs_response = file_system.delete().execute().await?; |
| 121 | + println!("delete file system response == {:?}", delete_fs_response); |
| 122 | + println!(); |
63 | 123 |
|
64 |
| - let response = file_system.delete().execute().await?; |
65 |
| - println!("response == {:?}\n\n", response); |
| 124 | + println!("data lake example done."); |
66 | 125 |
|
67 | 126 | Ok(())
|
68 | 127 | }
|
0 commit comments