Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use Url instead of String for Blob URLs #422

Merged
merged 3 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions sdk/storage/src/blob/blob/requests/copy_blob_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ use azure_core::headers::COPY_SOURCE;
use azure_core::headers::{add_mandatory_header, add_optional_header, add_optional_header_ref};
use azure_core::prelude::*;
use std::convert::TryInto;
use url::Url;

#[derive(Debug, Clone)]
pub struct CopyBlobBuilder<'a> {
blob_client: &'a BlobClient,
source_url: &'a str,
source_url: &'a Url,
metadata: Option<&'a Metadata>,
sequence_number_condition: Option<SequenceNumberCondition>,
if_modified_since_condition: Option<IfModifiedSinceCondition>,
Expand All @@ -25,7 +26,7 @@ pub struct CopyBlobBuilder<'a> {
}

impl<'a> CopyBlobBuilder<'a> {
pub(crate) fn new(blob_client: &'a BlobClient, source_url: &'a str) -> Self {
pub(crate) fn new(blob_client: &'a BlobClient, source_url: &'a Url) -> Self {
Self {
blob_client,
source_url,
Expand Down Expand Up @@ -72,7 +73,7 @@ impl<'a> CopyBlobBuilder<'a> {
url.as_str(),
&http::Method::PUT,
&|mut request| {
request = request.header(COPY_SOURCE, self.source_url);
request = request.header(COPY_SOURCE, self.source_url.as_str());
request = add_optional_header(&self.metadata, request);
request = add_optional_header(&self.sequence_number_condition, request);
request = add_optional_header(&self.if_modified_since_condition, request);
Expand Down
10 changes: 6 additions & 4 deletions sdk/storage/src/blob/clients/blob_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use bytes::Bytes;
use http::method::Method;
use http::request::{Builder, Request};
use std::sync::Arc;
use url::Url;

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

pub fn copy<'a>(&'a self, copy_source: &'a str) -> CopyBlobBuilder<'a> {
pub fn copy<'a>(&'a self, copy_source: &'a Url) -> CopyBlobBuilder<'a> {
CopyBlobBuilder::new(self, copy_source)
}

Expand Down Expand Up @@ -161,9 +162,10 @@ impl BlobClient {
pub fn generate_signed_blob_url(
&self,
signature: &SharedAccessSignature,
) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
let url = self.url_with_segments(None)?;
Ok(format!("{}?{}", url.as_str(), signature.token()))
) -> Result<url::Url, Box<dyn std::error::Error + Send + Sync>> {
let mut url = self.url_with_segments(None)?;
url.set_query(Some(&signature.token()));
Ok(url)
}

pub(crate) fn prepare_request(
Expand Down
20 changes: 10 additions & 10 deletions sdk/storage/tests/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::ops::Add;
use std::ops::Deref;
use std::sync::Arc;
use std::time::Duration;
use url::Url;
use uuid::Uuid;

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

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

cloned_blob
.copy(&format!(
"https://{}.blob.core.windows.net/{}/{}",
&std::env::var("STORAGE_ACCOUNT").unwrap(),
&container_name,
&blob_name
))
.execute()
.await
.unwrap();
let url = Url::parse(&format!(
"https://{}.blob.core.windows.net/{}/{}",
&std::env::var("STORAGE_ACCOUNT").unwrap(),
&container_name,
&blob_name
))
.unwrap();

cloned_blob.copy(&url).execute().await.unwrap();
}

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