Skip to content

Commit

Permalink
Bytes for storage
Browse files Browse the repository at this point in the history
  • Loading branch information
ctaggart committed Jan 16, 2021
1 parent ea1959b commit c99bbc0
Show file tree
Hide file tree
Showing 23 changed files with 89 additions and 63 deletions.
4 changes: 2 additions & 2 deletions sdk/storage/examples/blob_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let content = std::str::from_utf8(&buf)?.to_owned();
println!("content == {}", content);

let _response = blob.put_block_blob(&buf).execute().await?;
let _response = blob.put_block_blob(buf.clone()).execute().await?;

This comment has been minimized.

Copy link
@MindFlavor

MindFlavor Jan 17, 2021

Contributor

Small non-blocking nit: maybe here we should show how to use Bytes to avoid cloning the vector. Something like Bytes::from(buf).


let whole = blob.get().execute().await?;

Expand All @@ -70,7 +70,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut stream = Box::pin(blob.get().stream(512));

println!("\nStreaming");
let mut chunk = 0;
let mut chunk: usize = 0;
while let Some(value) = stream.next().await {
let value = value?;
println!("received {:?} bytes", value.len());
Expand Down
4 changes: 2 additions & 2 deletions sdk/storage/examples/connection_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
for i in 0..10u8 {
container
.as_blob_client(format!("blob{}.txt", i))
.put_block_blob("somedata".as_bytes())
.put_block_blob("somedata")
.content_type("text/plain")
.execute()
.await?;
Expand All @@ -68,7 +68,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {

let mut stream = Box::pin(container.list_blobs().max_results(max_results).stream());

let mut cnt = 0;
let mut cnt: i32 = 0;
while let Some(value) = stream.next().await {
let len = value?.incomplete_vector.len();
println!("received {} blobs", len);
Expand Down
7 changes: 4 additions & 3 deletions sdk/storage/examples/container_and_blob.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use azure_core::prelude::*;
use azure_storage::blob::prelude::*;
use azure_storage::core::prelude::*;
use bytes::Bytes;
use std::error::Error;
use std::sync::Arc;

Expand Down Expand Up @@ -31,15 +32,15 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
.await?;
println!("{:?}", res);

let data = b"something";
let data = Bytes::from_static(b"something");

// this is not mandatory but it helps preventing
// spurious data to be uploaded.
let hash = md5::compute(&data[..]).into();

let res = container
.as_blob_client("blob0.txt")
.put_block_blob(data)
.put_block_blob(data.clone())
.content_type("text/plain")
.hash(&hash)
.execute()
Expand All @@ -48,7 +49,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {

let res = container
.as_blob_client("blob1.txt")
.put_block_blob(data)
.put_block_blob(data.clone())
.content_type("text/plain")
.hash(&hash)
.execute()
Expand Down
4 changes: 2 additions & 2 deletions sdk/storage/examples/list_blobs_00.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
for i in 0..10u8 {
container
.as_blob_client(format!("blob{}.txt", i))
.put_block_blob("somedata".as_bytes())
.put_block_blob("somedata")
.content_type("text/plain")
.execute()
.await?;
Expand All @@ -74,7 +74,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
.stream(),
);

let mut cnt = 0;
let mut cnt: i32 = 0;
while let Some(value) = stream.next().await {
let len = value?.incomplete_vector.len();
println!("received {} blobs", len);
Expand Down
7 changes: 4 additions & 3 deletions sdk/storage/examples/put_block_blob_00.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extern crate log;
use azure_core::prelude::*;
use azure_storage::blob::prelude::*;
use azure_storage::core::prelude::*;
use bytes::Bytes;
use std::error::Error;
use std::sync::Arc;
use std::time::Duration;
Expand Down Expand Up @@ -34,7 +35,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
.as_container_client(&container)
.as_blob_client(&blob_name);

let data = b"something";
let data = Bytes::from_static(b"something");

// this is not mandatory but it helps preventing
// spurious data to be uploaded.
Expand All @@ -45,7 +46,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
// parameters (such as LeaseID, or ContentDisposition, MD5 etc...)
// so make sure to check with the documentation.
let res = blob
.put_block_blob(data)
.put_block_blob(data.clone())
.content_type("text/plain")
.hash(&hash)
.execute()
Expand All @@ -61,7 +62,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
.push(BlobBlockType::Uncommitted(b"pollastro" as &[u8]));

let res = blob
.put_block(&("satanasso".into()), data)
.put_block(&("satanasso".into()), data.clone())
.execute()
.await?;
println!("2-put_block {:?}", res);
Expand Down
11 changes: 6 additions & 5 deletions sdk/storage/examples/put_page_blob_00.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
extern crate log;
use azure_core::prelude::*;
use azure_storage::clients::*;
use bytes::Bytes;
use std::error::Error;
use std::sync::Arc;

Expand Down Expand Up @@ -31,17 +32,17 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
.as_container_client(&container_name);
let blob = container.as_blob_client(&blob_name);

let data: [u8; 2000] = [51; 2000];
let data = Bytes::from_static(&[51; 2000]);

let mut metadata = Metadata::new();
metadata.insert("pollo", "arrosto");
metadata.insert("milk", "shake");

let slice = &data[512..1024];
let slice = data.slice(512..1024);

// this is not mandatory but it helps preventing
// spurious data to be uploaded.
let digest = md5::compute(slice);
let digest = md5::compute(slice.clone());

// The required parameters are container_name_name, blob_name.
// The builder supports many more optional
Expand All @@ -60,15 +61,15 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
// the size of tha page or a buffer out
// of bounds error will be thrown.
let res = blob
.update_page(BA512Range::new(0, 511)?, slice)
.update_page(BA512Range::new(0, 511)?, slice.clone())
.hash(&digest.into())
.execute()
.await?;
println!("update first page == {:?}", res);

// update a second page with the same data
let res = blob
.update_page(BA512Range::new(512, 1023)?, slice)
.update_page(BA512Range::new(512, 1023)?, slice.clone())
.hash(&digest.into())
.execute()
.await?;
Expand Down
2 changes: 1 addition & 1 deletion sdk/storage/examples/stream_blob_00.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let string = "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF";

let _response = blob
.put_block_blob(string.as_bytes())
.put_block_blob(string)
.content_type("text/plain")
.execute()
.await?;
Expand Down
9 changes: 5 additions & 4 deletions sdk/storage/src/blob/blob/requests/append_block_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use crate::blob::prelude::*;
use crate::core::prelude::*;
use azure_core::headers::{add_optional_header, add_optional_header_ref};
use azure_core::prelude::*;
use bytes::Bytes;

#[derive(Debug, Clone)]
pub struct AppendBlockBuilder<'a> {
blob_client: &'a BlobClient,
body: &'a [u8],
body: Bytes,
hash: Option<&'a Hash>,
condition_max_size: Option<ConditionMaxSize>,
condition_append_position: Option<ConditionAppendPosition>,
Expand All @@ -17,10 +18,10 @@ pub struct AppendBlockBuilder<'a> {
}

impl<'a> AppendBlockBuilder<'a> {
pub(crate) fn new(blob_client: &'a BlobClient, body: &'a [u8]) -> Self {
pub(crate) fn new(blob_client: &'a BlobClient, body: impl Into<Bytes>) -> Self {
Self {
blob_client,
body,
body: body.into(),
hash: None,
condition_max_size: None,
condition_append_position: None,
Expand Down Expand Up @@ -68,7 +69,7 @@ impl<'a> AppendBlockBuilder<'a> {
request = add_optional_header(&self.client_request_id, request);
request
},
Some(self.body),
Some(self.body.clone()),
)?;

let response = self
Expand Down
9 changes: 5 additions & 4 deletions sdk/storage/src/blob/blob/requests/put_block_blob_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ use crate::core::prelude::*;
use azure_core::headers::BLOB_TYPE;
use azure_core::headers::{add_mandatory_header, add_optional_header, add_optional_header_ref};
use azure_core::prelude::*;
use bytes::Bytes;

#[derive(Debug, Clone)]
pub struct PutBlockBlobBuilder<'a> {
blob_client: &'a BlobClient,
body: &'a [u8],
body: Bytes,
hash: Option<&'a Hash>,
content_type: Option<ContentType<'a>>,
content_encoding: Option<ContentEncoding<'a>>,
Expand All @@ -23,10 +24,10 @@ pub struct PutBlockBlobBuilder<'a> {
}

impl<'a> PutBlockBlobBuilder<'a> {
pub(crate) fn new(blob_client: &'a BlobClient, body: &'a [u8]) -> Self {
pub(crate) fn new(blob_client: &'a BlobClient, body: impl Into<Bytes>) -> Self {
Self {
blob_client,
body,
body: body.into(),
hash: None,
content_type: None,
content_encoding: None,
Expand Down Expand Up @@ -86,7 +87,7 @@ impl<'a> PutBlockBlobBuilder<'a> {
request = add_optional_header(&self.client_request_id, request);
request
},
Some(self.body),
Some(self.body.clone()),
)?;

let response = self
Expand Down
13 changes: 9 additions & 4 deletions sdk/storage/src/blob/blob/requests/put_block_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,29 @@ use crate::blob::prelude::*;
use crate::core::prelude::*;
use azure_core::headers::{add_optional_header, add_optional_header_ref};
use azure_core::prelude::*;
use bytes::Bytes;

#[derive(Debug, Clone)]
pub struct PutBlockBuilder<'a> {
blob_client: &'a BlobClient,
block_id: &'a BlockId,
body: &'a [u8],
body: Bytes,
hash: Option<&'a Hash>,
client_request_id: Option<ClientRequestId<'a>>,
timeout: Option<Timeout>,
lease_id: Option<&'a LeaseId>,
}

impl<'a> PutBlockBuilder<'a> {
pub(crate) fn new(blob_client: &'a BlobClient, block_id: &'a BlockId, body: &'a [u8]) -> Self {
pub(crate) fn new(
blob_client: &'a BlobClient,
block_id: &'a BlockId,
body: impl Into<Bytes>,
) -> Self {
Self {
blob_client,
block_id,
body,
body: body.into(),
hash: None,
client_request_id: None,
timeout: None,
Expand Down Expand Up @@ -60,7 +65,7 @@ impl<'a> PutBlockBuilder<'a> {
request = add_optional_header_ref(&self.lease_id, request);
request
},
Some(self.body),
Some(self.body.clone()),
)?;

trace!("request.headers() == {:#?}", request.headers());
Expand Down
5 changes: 3 additions & 2 deletions sdk/storage/src/blob/blob/requests/put_block_list_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::blob::prelude::*;
use crate::core::prelude::*;
use azure_core::headers::{add_mandatory_header, add_optional_header, add_optional_header_ref};
use azure_core::prelude::*;
use bytes::Bytes;
use std::borrow::Borrow;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -65,12 +66,12 @@ where
trace!("url == {:?}", url);

let body = self.block_list.to_xml();
let body_bytes = body.as_bytes();
let body_bytes = Bytes::from(body);

// calculate the xml MD5. This can be made optional
// if needed, but i think it's best to calculate it.
let md5 = {
let hash = md5::compute(body_bytes);
let hash = md5::compute(body_bytes.clone());
debug!("md5 hash: {:02X}", hash);
base64::encode(hash.0)
};
Expand Down
9 changes: 5 additions & 4 deletions sdk/storage/src/blob/blob/requests/update_page_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ use crate::core::prelude::*;
use azure_core::headers::{add_mandatory_header, add_optional_header, add_optional_header_ref};
use azure_core::headers::{BLOB_TYPE, PAGE_WRITE};
use azure_core::prelude::*;
use bytes::Bytes;

#[derive(Debug, Clone)]
pub struct UpdatePageBuilder<'a> {
blob_client: &'a BlobClient,
ba512_range: BA512Range,
content: &'a [u8],
content: Bytes,
hash: Option<&'a Hash>,
sequence_number_condition: Option<SequenceNumberCondition>,
if_modified_since_condition: Option<IfModifiedSinceCondition>,
Expand All @@ -23,12 +24,12 @@ impl<'a> UpdatePageBuilder<'a> {
pub(crate) fn new(
blob_client: &'a BlobClient,
ba512_range: BA512Range,
content: &'a [u8],
content: impl Into<Bytes>,
) -> Self {
Self {
blob_client,
ba512_range,
content,
content: content.into(),
hash: None,
sequence_number_condition: None,
if_modified_since_condition: None,
Expand Down Expand Up @@ -82,7 +83,7 @@ impl<'a> UpdatePageBuilder<'a> {
request = add_optional_header_ref(&self.lease_id, request);
request
},
Some(self.content),
Some(self.content.clone()),
)?;

trace!("request.headers() == {:#?}", request.headers());
Expand Down
3 changes: 2 additions & 1 deletion sdk/storage/src/blob/container/requests/set_acl_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use azure_core::headers::{add_mandatory_header, add_optional_header, add_optiona
use azure_core::lease::LeaseId;
use azure_core::prelude::*;
use azure_core::StoredAccessPolicyList;
use bytes::Bytes;
use http::method::Method;
use http::status::StatusCode;

Expand Down Expand Up @@ -62,7 +63,7 @@ impl<'a> SetACLBuilder<'a> {
request
},
match xml {
Some(ref x) => Some(x.as_bytes()),
Some(x) => Some(Bytes::from(x)),
None => None,
},
)?;
Expand Down
Loading

0 comments on commit c99bbc0

Please sign in to comment.