Skip to content

Commit

Permalink
add base64 encode
Browse files Browse the repository at this point in the history
  • Loading branch information
wcy-fdu committed Feb 15, 2024
1 parent b3970cc commit 74cd208
Showing 1 changed file with 44 additions and 14 deletions.
58 changes: 44 additions & 14 deletions core/src/services/azblob/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
// specific language governing permissions and limitations
// under the License.

use base64::prelude::BASE64_STANDARD;
use base64::Engine;
use bytes::Bytes;
use http::header::HeaderName;

use http::header::CONTENT_LENGTH;
use http::header::CONTENT_TYPE;
use http::header::IF_MATCH;
Expand All @@ -27,7 +30,7 @@ use http::Response;
use reqsign::AzureStorageCredential;
use reqsign::AzureStorageLoader;
use reqsign::AzureStorageSigner;
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::fmt::Debug;
use std::fmt::Formatter;
Expand Down Expand Up @@ -449,20 +452,17 @@ impl AzblobCore {
"BlockBlob",
);

// Set body
// refer to https://learn.microsoft.com/en-us/rest/api/storageservices/put-block-list?
let req_body = {
let block_list = block_ids
.iter()
.map(|block_id| format!(" <Uncommitted>{}</Uncommitted>", block_id))
.collect::<Vec<String>>()
.join("\n");

format!("<BlockList>\n{}\n</BlockList>", block_list)
};
let content = quick_xml::se::to_string(&PutBlockListRequest {
uncommitted: block_ids
.into_iter()
.map(|block_id| BASE64_STANDARD.encode(block_id))
.collect(),
})
.map_err(new_xml_deserialize_error)?;
let req = req
.body(AsyncBody::Bytes(Bytes::from(content)))
.map_err(new_request_build_error)?;

let body = AsyncBody::Bytes(Bytes::from(req_body));
let req = req.body(body).map_err(new_request_build_error)?;
Ok(req)
}

Expand Down Expand Up @@ -643,6 +643,13 @@ impl AzblobCore {
}
}

/// Request of CompleteMultipartUploadRequest
#[derive(Default, Debug, Serialize)]
#[serde(default, rename = "<BlockList>", rename_all = "PascalCase")]
pub struct PutBlockListRequest {
pub uncommitted: Vec<String>,
}

#[derive(Default, Debug, Deserialize)]
#[serde(default, rename_all = "PascalCase")]
pub struct ListBlobsOutput {
Expand Down Expand Up @@ -871,4 +878,27 @@ mod tests {

de::from_reader(Bytes::from(bs).reader()).expect("must success")
}

/// This example is from https://learn.microsoft.com/en-us/rest/api/storageservices/put-block-list?tabs=microsoft-entra-id
#[test]
fn test_serialize_put_block_list_request() {
let req = PutBlockListRequest {
uncommitted: vec!["1".to_string(), "2".to_string(), "3".to_string()],
};

let actual = quick_xml::se::to_string(&req).expect("must succeed");

pretty_assertions::assert_eq!(
actual,
r#"<BlockList>
<Uncommitted>1</Uncommitted>
<Uncommitted>2</Uncommitted>
<Uncommitted>3</Uncommitted>
</BlockList>"#
// Cleanup space and new line
.replace([' ', '\n'], "")
// Escape `"` by hand to address <https://github.com/tafia/quick-xml/issues/362>
.replace('"', "&quot;")
)
}
}

0 comments on commit 74cd208

Please sign in to comment.