Skip to content

Commit f1fe84f

Browse files
[Storage] ContainerClient (create_container, delete_container) and BlobClient (download_blob, upload_blob) (#2246)
1 parent 7e42a95 commit f1fe84f

22 files changed

+3686
-1212
lines changed

eng/emitter-package-lock.json

Lines changed: 92 additions & 92 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

eng/emitter-package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"main": "dist/src/index.js",
33
"dependencies": {
4-
"@azure-tools/typespec-rust": "0.9.0"
4+
"@azure-tools/typespec-rust": "0.9.1"
55
},
66
"devDependencies": {
77
"@azure-tools/typespec-azure-core": "0.50.0",
@@ -14,4 +14,4 @@
1414
"@typespec/versioning": "0.64.0",
1515
"@typespec/xml": "0.64.0"
1616
}
17-
}
17+
}

sdk/storage/assets.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"AssetsRepo": "Azure/azure-sdk-assets",
3+
"AssetsRepoPrefixPath": "rust",
4+
"Tag": "rust/azure_storage_blob_d76e2bb82c",
5+
"TagPrefix": "rust/azure_storage_blob"
6+
}

sdk/storage/azure_storage_blob/src/clients/blob_client.rs

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22
// Licensed under the MIT License.
33

44
use crate::{
5-
models::BlobProperties, pipeline::StorageHeadersPolicy, BlobBlobClientGetPropertiesOptions,
6-
BlobClientOptions, GeneratedBlobClient,
5+
clients::GeneratedBlobClient,
6+
models::{
7+
BlobBlobClientDownloadOptions, BlobBlobClientGetPropertiesOptions,
8+
BlobBlockBlobClientUploadOptions, BlobProperties,
9+
},
10+
pipeline::StorageHeadersPolicy,
11+
BlobClientOptions,
12+
};
13+
use azure_core::{
14+
credentials::TokenCredential, BearerTokenCredentialPolicy, Bytes, Policy, RequestContent,
15+
Response, Result, Url,
716
};
8-
use azure_core::{credentials::TokenCredential, BearerTokenCredentialPolicy, Policy, Result, Url};
917
use std::sync::Arc;
1018

1119
pub struct BlobClient {
@@ -53,6 +61,14 @@ impl BlobClient {
5361
&self.endpoint
5462
}
5563

64+
pub fn container_name(&self) -> &str {
65+
&self.container_name
66+
}
67+
68+
pub fn blob_name(&self) -> &str {
69+
&self.blob_name
70+
}
71+
5672
pub async fn get_blob_properties(
5773
&self,
5874
options: Option<BlobBlobClientGetPropertiesOptions<'_>>,
@@ -66,4 +82,39 @@ impl BlobClient {
6682
let blob_properties: BlobProperties = response.headers().get()?;
6783
Ok(blob_properties)
6884
}
85+
86+
pub async fn download_blob(
87+
&self,
88+
options: Option<BlobBlobClientDownloadOptions<'_>>,
89+
) -> Result<Response> {
90+
let response = self
91+
.client
92+
.get_blob_blob_client(self.container_name.clone(), self.blob_name.clone())
93+
.download(options)
94+
.await?;
95+
Ok(response)
96+
}
97+
98+
// For now, this is single-shot, block blob hot path only.
99+
pub async fn upload_blob(
100+
&self,
101+
data: RequestContent<Bytes>,
102+
overwrite: bool,
103+
content_length: i64,
104+
options: Option<BlobBlockBlobClientUploadOptions<'_>>,
105+
) -> Result<Response<()>> {
106+
let mut options = options.unwrap_or_default();
107+
108+
// Check if they want overwrite, by default overwrite=False
109+
if !overwrite {
110+
options.if_none_match = Some(String::from("*"));
111+
}
112+
113+
let response = self
114+
.client
115+
.get_blob_block_blob_client(self.container_name.clone(), self.blob_name.clone())
116+
.upload(data, content_length, Some(options))
117+
.await?;
118+
Ok(response)
119+
}
69120
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,83 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
3+
4+
use crate::clients::GeneratedBlobClient;
5+
use crate::models::{BlobContainerClientCreateOptions, BlobContainerClientDeleteOptions};
6+
use crate::pipeline::StorageHeadersPolicy;
7+
use crate::BlobClientOptions;
8+
use azure_core::{
9+
credentials::TokenCredential, BearerTokenCredentialPolicy, Policy, Response, Result, Url,
10+
};
11+
use std::sync::Arc;
12+
13+
pub struct BlobContainerClient {
14+
endpoint: Url,
15+
container_name: String,
16+
client: GeneratedBlobClient,
17+
}
18+
19+
impl BlobContainerClient {
20+
pub fn new(
21+
endpoint: &str,
22+
container_name: String,
23+
credential: Arc<dyn TokenCredential>,
24+
options: Option<BlobClientOptions>,
25+
) -> Result<Self> {
26+
let mut options = options.unwrap_or_default();
27+
28+
let storage_headers_policy = Arc::new(StorageHeadersPolicy);
29+
options
30+
.client_options
31+
.per_call_policies
32+
.push(storage_headers_policy);
33+
34+
let oauth_token_policy = BearerTokenCredentialPolicy::new(
35+
credential.clone(),
36+
["https://storage.azure.com/.default"],
37+
);
38+
options
39+
.client_options
40+
.per_try_policies
41+
.push(Arc::new(oauth_token_policy) as Arc<dyn Policy>);
42+
43+
let client = GeneratedBlobClient::new(endpoint, credential, Some(options))?;
44+
45+
Ok(Self {
46+
endpoint: endpoint.parse()?,
47+
container_name,
48+
client,
49+
})
50+
}
51+
52+
pub fn endpoint(&self) -> &Url {
53+
&self.endpoint
54+
}
55+
56+
pub fn container_name(&self) -> &str {
57+
&self.container_name
58+
}
59+
60+
pub async fn create_container(
61+
&self,
62+
options: Option<BlobContainerClientCreateOptions<'_>>,
63+
) -> Result<Response<()>> {
64+
let response = self
65+
.client
66+
.get_blob_container_client(self.container_name.clone())
67+
.create(options)
68+
.await?;
69+
Ok(response)
70+
}
71+
72+
pub async fn delete_container(
73+
&self,
74+
options: Option<BlobContainerClientDeleteOptions<'_>>,
75+
) -> Result<Response<()>> {
76+
let response = self
77+
.client
78+
.get_blob_container_client(self.container_name.clone())
79+
.delete(options)
80+
.await?;
81+
Ok(response)
82+
}
83+
}

sdk/storage/azure_storage_blob/src/generated/clients/blob_append_blob_client.rs

Lines changed: 27 additions & 99 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)