azure_storage_blobs/blob/operations/
copy_blob_from_url.rs

1use crate::{
2    blob::{copy_status_from_headers, CopyStatus, SourceContentMD5},
3    prelude::*,
4};
5use azure_core::{headers::*, prelude::*, RequestId, Url};
6use azure_storage::{
7    copy_id_from_headers, headers::content_md5_from_headers_optional, ConsistencyMD5, CopyId,
8};
9use time::OffsetDateTime;
10
11operation! {
12    CopyBlobFromUrl,
13    client: BlobClient,
14    source_url: Url,
15    ?is_synchronous: bool,
16    ?metadata: Metadata,
17    ?if_modified_since: IfModifiedSinceCondition,
18    ?if_match: IfMatchCondition,
19    ?if_source_since: IfSourceModifiedSinceCondition,
20    ?if_source_match: IfSourceMatchCondition,
21    ?lease_id: LeaseId,
22    ?source_content_md5: SourceContentMD5
23}
24
25impl CopyBlobFromUrlBuilder {
26    pub fn into_future(mut self) -> CopyBlobFromUrl {
27        Box::pin(async move {
28            let url = self.client.url()?;
29
30            let mut headers = Headers::new();
31            headers.insert(COPY_SOURCE, self.source_url.to_string());
32            headers.insert(
33                REQUIRES_SYNC,
34                format!("{}", self.is_synchronous.unwrap_or(false)),
35            );
36            if let Some(metadata) = &self.metadata {
37                for m in metadata.iter() {
38                    headers.add(m);
39                }
40            }
41            headers.add(self.if_modified_since);
42            headers.add(self.if_match);
43            headers.add(self.lease_id);
44            headers.add(self.if_source_since);
45            headers.add(self.if_source_match);
46            headers.add(self.source_content_md5);
47
48            let mut request =
49                BlobClient::finalize_request(url, azure_core::Method::Put, headers, None)?;
50
51            let response = self.client.send(&mut self.context, &mut request).await?;
52
53            (response.headers()).try_into()
54        })
55    }
56}
57
58#[derive(Debug, Clone, PartialEq, Eq)]
59pub struct CopyBlobFromUrlResponse {
60    pub content_md5: Option<ConsistencyMD5>,
61    pub last_modified: OffsetDateTime,
62    pub etag: String,
63    pub server: String,
64    pub request_id: RequestId,
65    pub version: String,
66    pub copy_id: CopyId,
67    pub copy_status: CopyStatus,
68    pub date: OffsetDateTime,
69}
70
71impl TryFrom<&Headers> for CopyBlobFromUrlResponse {
72    type Error = azure_core::Error;
73    fn try_from(headers: &Headers) -> azure_core::Result<Self> {
74        Ok(Self {
75            content_md5: content_md5_from_headers_optional(headers)?,
76            last_modified: last_modified_from_headers(headers)?,
77            etag: etag_from_headers(headers)?,
78            server: server_from_headers(headers)?,
79            request_id: request_id_from_headers(headers)?,
80            version: version_from_headers(headers)?,
81            copy_id: copy_id_from_headers(headers)?,
82            copy_status: copy_status_from_headers(headers)?,
83            date: date_from_headers(headers)?,
84        })
85    }
86}