Skip to content

[Storage] [WIP] start_copy_from_url for BlobClient #2541

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
27 changes: 23 additions & 4 deletions sdk/storage/azure_storage_blob/src/clients/blob_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
use crate::{
generated::clients::BlobClient as GeneratedBlobClient,
generated::models::{
BlobClientDownloadResult, BlobClientGetPropertiesResult,
BlobClientDownloadResult, BlobClientGetPropertiesResult, BlobClientStartCopyFromUrlResult,
BlockBlobClientCommitBlockListResult, BlockBlobClientStageBlockResult,
BlockBlobClientUploadResult,
},
models::{AccessTier, BlockList, BlockListType, BlockLookupList},
pipeline::StorageHeadersPolicy,
BlobClientDeleteOptions, BlobClientDownloadOptions, BlobClientGetPropertiesOptions,
BlobClientOptions, BlobClientSetMetadataOptions, BlobClientSetPropertiesOptions,
BlobClientSetTierOptions, BlockBlobClientCommitBlockListOptions,
BlockBlobClientGetBlockListOptions, BlockBlobClientStageBlockOptions,
BlockBlobClientUploadOptions,
BlobClientSetTierOptions, BlobClientStartCopyFromUrlOptions,
BlockBlobClientCommitBlockListOptions, BlockBlobClientGetBlockListOptions,
BlockBlobClientStageBlockOptions, BlockBlobClientUploadOptions,
};
use azure_core::{
credentials::TokenCredential,
Expand Down Expand Up @@ -250,4 +250,23 @@ impl BlobClient {
) -> Result<Response<()>> {
self.client.set_tier(tier, options).await
}

/// Copies a blob or an internet resource to a new blob.
///
/// # Arguments
///
/// * `copy_source` - A URL of up to 2 KB in length that specifies a file or blob.
/// The value should be URL-encoded as it would appear in a request URI.
/// If the source is in another account, the source must either be public
/// or must be authenticated via a shared access signature. If the source
/// is public, no authentication is required.
/// Example: https://myaccount.blob.core.windows.net/mycontainer/myblob
/// * `options` - Optional configuration for the request.
pub async fn start_copy_from_url(
&self,
copy_source: &str,
options: Option<BlobClientStartCopyFromUrlOptions<'_>>,
) -> Result<Response<BlobClientStartCopyFromUrlResult>> {
self.client.start_copy_from_url(copy_source, options).await
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions sdk/storage/azure_storage_blob/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@ pub use crate::generated::clients::{
pub use crate::generated::models::{
BlobClientDeleteOptions, BlobClientDownloadOptions, BlobClientGetPropertiesOptions,
BlobClientSetMetadataOptions, BlobClientSetPropertiesOptions, BlobClientSetTierOptions,
BlobContainerClientCreateOptions, BlobContainerClientDeleteOptions,
BlobContainerClientGetPropertiesOptions, BlobContainerClientSetMetadataOptions,
BlobServiceClientGetPropertiesOptions, BlockBlobClientCommitBlockListOptions,
BlockBlobClientGetBlockListOptions, BlockBlobClientStageBlockOptions,
BlockBlobClientUploadOptions,
BlobClientStartCopyFromUrlOptions, BlobContainerClientCreateOptions,
BlobContainerClientDeleteOptions, BlobContainerClientGetPropertiesOptions,
BlobContainerClientSetMetadataOptions, BlobServiceClientGetPropertiesOptions,
BlockBlobClientCommitBlockListOptions, BlockBlobClientGetBlockListOptions,
BlockBlobClientStageBlockOptions, BlockBlobClientUploadOptions,
};

pub mod models {
pub use crate::generated::models::{
AccessTier, ArchiveStatus, BlobClientDownloadResult, BlobClientDownloadResultHeaders,
BlobClientGetPropertiesResult, BlobClientGetPropertiesResultHeaders,
BlobClientStartCopyFromUrlResult, BlobClientStartCopyFromUrlResultHeaders,
BlobContainerClientGetPropertiesResult, BlobContainerClientGetPropertiesResultHeaders,
BlobImmutabilityPolicyMode, BlobType, BlockBlobClientCommitBlockListResult,
BlockBlobClientStageBlockResult, BlockBlobClientUploadResult, BlockList, BlockListType,
Expand Down
36 changes: 35 additions & 1 deletion sdk/storage/azure_storage_blob/tests/blob_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use azure_core_test::{recorded, TestContext};
use azure_storage_blob::{
models::{
AccessTier, BlobClientDownloadResultHeaders, BlobClientGetPropertiesResultHeaders,
BlockListType, BlockLookupList, LeaseState,
BlobClientStartCopyFromUrlResultHeaders, BlockListType, BlockLookupList, CopyStatus,
LeaseState,
},
BlobClientSetMetadataOptions, BlobClientSetPropertiesOptions, BlockBlobClientUploadOptions,
};
Expand Down Expand Up @@ -426,3 +427,36 @@ async fn test_set_access_tier(ctx: TestContext) -> Result<(), Box<dyn Error>> {
container_client.delete_container(None).await?;
Ok(())
}

#[recorded::test]
async fn test_start_copy_from_url(ctx: TestContext) -> Result<(), Box<dyn Error>> {
// Recording Setup
let recording = ctx.recording();
let container_client = get_container_client(recording, true).await?;
let source_blob_client = container_client.blob_client(get_blob_name(recording));
create_test_blob(&source_blob_client).await?;

let blob_client = container_client.blob_client("destination_blob".to_string());
let source_url = format!(
"{}{}/{}",
source_blob_client.endpoint().as_str(),
source_blob_client.container_name(),
source_blob_client.blob_name()
);
let response = blob_client.start_copy_from_url(&source_url, None).await?;
let (_, _, source_content) = source_blob_client.download(None).await?.deconstruct();
let (_, _, copied_content) = blob_client.download(None).await?.deconstruct();

// Assert
let copy_status = response.copy_status()?;
let copy_id = response.copy_id()?;
assert_eq!(CopyStatus::Success, copy_status.unwrap());
assert!(copy_id.is_some());
assert_eq!(
source_content.collect().await?,
copied_content.collect().await?
);

container_client.delete_container(None).await?;
Ok(())
}
Loading