Skip to content

Commit 2f1ca2e

Browse files
authored
use Url instead of String for Blob URLs (#422)
1 parent 9f673e0 commit 2f1ca2e

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

sdk/storage/src/blob/blob/requests/copy_blob_builder.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ use azure_core::headers::COPY_SOURCE;
55
use azure_core::headers::{add_mandatory_header, add_optional_header, add_optional_header_ref};
66
use azure_core::prelude::*;
77
use std::convert::TryInto;
8+
use url::Url;
89

910
#[derive(Debug, Clone)]
1011
pub struct CopyBlobBuilder<'a> {
1112
blob_client: &'a BlobClient,
12-
source_url: &'a str,
13+
source_url: &'a Url,
1314
metadata: Option<&'a Metadata>,
1415
sequence_number_condition: Option<SequenceNumberCondition>,
1516
if_modified_since_condition: Option<IfModifiedSinceCondition>,
@@ -25,7 +26,7 @@ pub struct CopyBlobBuilder<'a> {
2526
}
2627

2728
impl<'a> CopyBlobBuilder<'a> {
28-
pub(crate) fn new(blob_client: &'a BlobClient, source_url: &'a str) -> Self {
29+
pub(crate) fn new(blob_client: &'a BlobClient, source_url: &'a Url) -> Self {
2930
Self {
3031
blob_client,
3132
source_url,
@@ -72,7 +73,7 @@ impl<'a> CopyBlobBuilder<'a> {
7273
url.as_str(),
7374
&http::Method::PUT,
7475
&|mut request| {
75-
request = request.header(COPY_SOURCE, self.source_url);
76+
request = request.header(COPY_SOURCE, self.source_url.as_str());
7677
request = add_optional_header(&self.metadata, request);
7778
request = add_optional_header(&self.sequence_number_condition, request);
7879
request = add_optional_header(&self.if_modified_since_condition, request);

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use bytes::Bytes;
88
use http::method::Method;
99
use http::request::{Builder, Request};
1010
use std::sync::Arc;
11+
use url::Url;
1112

1213
pub trait AsBlobClient<BN: Into<String>> {
1314
fn as_blob_client(&self, blob_name: BN) -> Arc<BlobClient>;
@@ -103,7 +104,7 @@ impl BlobClient {
103104
DeleteBlobVersionBuilder::new(self, version_id)
104105
}
105106

106-
pub fn copy<'a>(&'a self, copy_source: &'a str) -> CopyBlobBuilder<'a> {
107+
pub fn copy<'a>(&'a self, copy_source: &'a Url) -> CopyBlobBuilder<'a> {
107108
CopyBlobBuilder::new(self, copy_source)
108109
}
109110

@@ -161,9 +162,10 @@ impl BlobClient {
161162
pub fn generate_signed_blob_url(
162163
&self,
163164
signature: &SharedAccessSignature,
164-
) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
165-
let url = self.url_with_segments(None)?;
166-
Ok(format!("{}?{}", url.as_str(), signature.token()))
165+
) -> Result<url::Url, Box<dyn std::error::Error + Send + Sync>> {
166+
let mut url = self.url_with_segments(None)?;
167+
url.set_query(Some(&signature.token()));
168+
Ok(url)
167169
}
168170

169171
pub(crate) fn prepare_request(

sdk/storage/tests/blob.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::ops::Add;
1515
use std::ops::Deref;
1616
use std::sync::Arc;
1717
use std::time::Duration;
18+
use url::Url;
1819
use uuid::Uuid;
1920

2021
#[tokio::test]
@@ -309,16 +310,15 @@ async fn copy_blob() {
309310

310311
let cloned_blob = container.as_blob_client("cloned_blob");
311312

312-
cloned_blob
313-
.copy(&format!(
314-
"https://{}.blob.core.windows.net/{}/{}",
315-
&std::env::var("STORAGE_ACCOUNT").unwrap(),
316-
&container_name,
317-
&blob_name
318-
))
319-
.execute()
320-
.await
321-
.unwrap();
313+
let url = Url::parse(&format!(
314+
"https://{}.blob.core.windows.net/{}/{}",
315+
&std::env::var("STORAGE_ACCOUNT").unwrap(),
316+
&container_name,
317+
&blob_name
318+
))
319+
.unwrap();
320+
321+
cloned_blob.copy(&url).execute().await.unwrap();
322322
}
323323

324324
async fn requires_send_future<F, O>(fut: F) -> O

0 commit comments

Comments
 (0)