-
Notifications
You must be signed in to change notification settings - Fork 288
[Feature] Add support for setting blob properties #869
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f01bc36
Add initial support for set_blob_properties
aaron-hardin 72964a6
Merge branch 'Azure:main' into set-blob-properties
aaron-hardin cc254ea
Code review: add setter
aaron-hardin dcd05ce
Code review: put constructor first
aaron-hardin f88d2b9
Code review re-word doc comment
aaron-hardin a87b97e
Clippy: remove redundant into call
aaron-hardin a910aa5
Merge branch 'Azure:main' into set-blob-properties
aaron-hardin 79789ca
Update sdk/storage_blobs/src/blob/operations/set_properties.rs
bmc-msft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#[macro_use] | ||
extern crate log; | ||
use azure_storage::core::prelude::*; | ||
use azure_storage_blobs::prelude::*; | ||
|
||
#[tokio::main] | ||
async fn main() -> azure_core::Result<()> { | ||
// First we retrieve the account name and master key from environment variables. | ||
let account = | ||
std::env::var("STORAGE_ACCOUNT").expect("Set env variable STORAGE_ACCOUNT first!"); | ||
let master_key = | ||
std::env::var("STORAGE_MASTER_KEY").expect("Set env variable STORAGE_MASTER_KEY first!"); | ||
|
||
let container = std::env::args() | ||
.nth(1) | ||
.expect("please specify container name as command line parameter"); | ||
let blob = std::env::args() | ||
.nth(2) | ||
.expect("please specify blob name as command line parameter"); | ||
|
||
let http_client = azure_core::new_http_client(); | ||
let storage_account_client = | ||
StorageAccountClient::new_access_key(http_client.clone(), &account, &master_key); | ||
|
||
// this is how you would use the SAS token: | ||
// let storage_account_client = StorageAccountClient::new_sas_token(http_client.clone(), &account, | ||
// "sv=2018-11-09&ss=b&srt=o&se=2021-01-15T12%3A09%3A01Z&sp=r&st=2021-01-15T11%3A09%3A01Z&spr=http,https&sig=some_signature")?; | ||
|
||
let storage_client = storage_account_client.storage_client(); | ||
let blob_client = storage_client | ||
.container_client(&container) | ||
.blob_client(&blob); | ||
|
||
trace!("Requesting blob properties"); | ||
|
||
let properties = blob_client | ||
.get_properties() | ||
.into_future() | ||
.await? | ||
.blob | ||
.properties; | ||
|
||
blob_client | ||
.set_properties() | ||
.set_from_blob_properties(properties) | ||
.content_md5(md5::compute("howdy")) | ||
.into_future() | ||
.await?; | ||
|
||
Ok(()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
sdk/storage_blobs/src/blob/operations/set_properties.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
use crate::{blob::BlobProperties, prelude::*}; | ||
use azure_core::prelude::*; | ||
use azure_core::{ | ||
headers::{ | ||
date_from_headers, etag_from_headers, request_id_from_headers, server_from_headers, Headers, | ||
}, | ||
Method, RequestId, | ||
}; | ||
use chrono::{DateTime, Utc}; | ||
use std::convert::{TryFrom, TryInto}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct SetPropertiesBuilder { | ||
blob_client: BlobClient, | ||
lease_id: Option<LeaseId>, | ||
timeout: Option<Timeout>, | ||
cache_control: Option<BlobCacheControl>, | ||
content_type: Option<BlobContentType>, | ||
content_encoding: Option<BlobContentEncoding>, | ||
content_language: Option<BlobContentLanguage>, | ||
content_disposition: Option<BlobContentDisposition>, | ||
content_md5: Option<BlobContentMD5>, | ||
context: Context, | ||
} | ||
|
||
impl SetPropertiesBuilder { | ||
pub(crate) fn new(blob_client: BlobClient) -> Self { | ||
Self { | ||
blob_client, | ||
lease_id: None, | ||
timeout: None, | ||
cache_control: None, | ||
content_type: None, | ||
content_encoding: None, | ||
content_language: None, | ||
content_disposition: None, | ||
content_md5: None, | ||
context: Context::new(), | ||
} | ||
} | ||
|
||
pub fn set_from_blob_properties(self, blob_properties: BlobProperties) -> Self { | ||
let mut s = self; | ||
|
||
if let Some(cc) = blob_properties.cache_control { | ||
s = s.cache_control(cc); | ||
} | ||
if !blob_properties.content_type.is_empty() { | ||
s = s.content_type(blob_properties.content_type); | ||
} | ||
if let Some(ce) = blob_properties.content_encoding { | ||
s = s.content_encoding(ce); | ||
} | ||
if let Some(cl) = blob_properties.content_language { | ||
s = s.content_language(cl); | ||
} | ||
if let Some(cd) = blob_properties.content_disposition { | ||
s = s.content_disposition(cd); | ||
} | ||
if let Some(cmd5) = blob_properties.content_md5 { | ||
s = s.content_md5(cmd5); | ||
} | ||
s | ||
} | ||
|
||
setters! { | ||
lease_id: LeaseId => Some(lease_id), | ||
timeout: Timeout => Some(timeout), | ||
cache_control: BlobCacheControl => Some(cache_control), | ||
content_type: BlobContentType => Some(content_type), | ||
content_encoding: BlobContentEncoding => Some(content_encoding), | ||
content_language: BlobContentLanguage => Some(content_language), | ||
content_disposition: BlobContentDisposition => Some(content_disposition), | ||
content_md5: BlobContentMD5 => Some(content_md5), | ||
context: Context => context, | ||
} | ||
|
||
pub fn into_future(mut self) -> Response { | ||
Box::pin(async move { | ||
let mut url = self.blob_client.url_with_segments(None)?; | ||
|
||
url.query_pairs_mut().append_pair("comp", "properties"); | ||
self.timeout.append_to_url_query(&mut url); | ||
|
||
let mut request = self.blob_client.prepare_request(url, Method::PUT, None)?; | ||
bmc-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
request.add_optional_header(&self.lease_id); | ||
request.add_optional_header(&self.cache_control); | ||
request.add_optional_header(&self.content_type); | ||
request.add_optional_header(&self.content_encoding); | ||
request.add_optional_header(&self.content_language); | ||
request.add_optional_header(&self.content_disposition); | ||
request.add_optional_header(&self.content_md5); | ||
|
||
let response = self | ||
.blob_client | ||
.send(&mut self.context, &mut request) | ||
.await?; | ||
response.headers().try_into() | ||
}) | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct SetPropertiesResponse { | ||
pub request_id: RequestId, | ||
pub etag: String, | ||
pub server: String, | ||
pub date: DateTime<Utc>, | ||
} | ||
|
||
impl TryFrom<&Headers> for SetPropertiesResponse { | ||
type Error = crate::Error; | ||
|
||
fn try_from(headers: &Headers) -> Result<Self, Self::Error> { | ||
Ok(SetPropertiesResponse { | ||
request_id: request_id_from_headers(headers)?, | ||
etag: etag_from_headers(headers)?, | ||
server: server_from_headers(headers)?.to_owned(), | ||
date: date_from_headers(headers)?, | ||
}) | ||
} | ||
} | ||
pub type Response = futures::future::BoxFuture<'static, azure_core::Result<SetPropertiesResponse>>; | ||
|
||
#[cfg(feature = "into_future")] | ||
impl std::future::IntoFuture for SetPropertiesBuilder { | ||
type IntoFuture = Response; | ||
type Output = <Response as std::future::Future>::Output; | ||
fn into_future(self) -> Self::IntoFuture { | ||
Self::into_future(self) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use azure_core::headers::{self, Header}; | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
pub struct BlobCacheControl(std::borrow::Cow<'static, str>); | ||
|
||
impl BlobCacheControl { | ||
pub const fn from_static(s: &'static str) -> Self { | ||
rylev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Self(std::borrow::Cow::Borrowed(s)) | ||
} | ||
|
||
pub fn as_str(&self) -> &str { | ||
self.0.as_ref() | ||
} | ||
} | ||
|
||
impl From<&'static str> for BlobCacheControl { | ||
fn from(s: &'static str) -> Self { | ||
Self::from_static(s) | ||
} | ||
} | ||
|
||
impl From<String> for BlobCacheControl { | ||
fn from(s: String) -> Self { | ||
Self(std::borrow::Cow::Owned(s)) | ||
} | ||
} | ||
|
||
impl From<&String> for BlobCacheControl { | ||
fn from(s: &String) -> Self { | ||
Self(std::borrow::Cow::Owned(s.clone())) | ||
} | ||
} | ||
|
||
impl Header for BlobCacheControl { | ||
fn name(&self) -> headers::HeaderName { | ||
azure_core::headers::BLOB_CACHE_CONTROL | ||
} | ||
|
||
fn value(&self) -> headers::HeaderValue { | ||
self.0.to_string().into() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use azure_core::headers::{self, Header}; | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
pub struct BlobContentDisposition(std::borrow::Cow<'static, str>); | ||
|
||
impl BlobContentDisposition { | ||
pub const fn from_static(s: &'static str) -> Self { | ||
Self(std::borrow::Cow::Borrowed(s)) | ||
} | ||
|
||
pub fn as_str(&self) -> &str { | ||
self.0.as_ref() | ||
} | ||
} | ||
|
||
impl From<&'static str> for BlobContentDisposition { | ||
fn from(s: &'static str) -> Self { | ||
Self::from_static(s) | ||
} | ||
} | ||
|
||
impl From<String> for BlobContentDisposition { | ||
fn from(s: String) -> Self { | ||
Self(std::borrow::Cow::Owned(s)) | ||
} | ||
} | ||
|
||
impl From<&String> for BlobContentDisposition { | ||
fn from(s: &String) -> Self { | ||
Self(std::borrow::Cow::Owned(s.clone())) | ||
} | ||
} | ||
|
||
impl Header for BlobContentDisposition { | ||
fn name(&self) -> headers::HeaderName { | ||
"x-ms-blob-content-disposition".into() | ||
} | ||
|
||
fn value(&self) -> headers::HeaderValue { | ||
self.0.to_string().into() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use azure_core::headers::{self, Header}; | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
pub struct BlobContentEncoding(std::borrow::Cow<'static, str>); | ||
|
||
impl BlobContentEncoding { | ||
pub const fn from_static(s: &'static str) -> Self { | ||
Self(std::borrow::Cow::Borrowed(s)) | ||
} | ||
|
||
pub fn as_str(&self) -> &str { | ||
self.0.as_ref() | ||
} | ||
} | ||
|
||
impl From<&'static str> for BlobContentEncoding { | ||
fn from(s: &'static str) -> Self { | ||
Self::from_static(s) | ||
} | ||
} | ||
|
||
impl From<String> for BlobContentEncoding { | ||
fn from(s: String) -> Self { | ||
Self(std::borrow::Cow::Owned(s)) | ||
} | ||
} | ||
|
||
impl From<&String> for BlobContentEncoding { | ||
fn from(s: &String) -> Self { | ||
Self(std::borrow::Cow::Owned(s.clone())) | ||
} | ||
} | ||
|
||
impl Header for BlobContentEncoding { | ||
fn name(&self) -> headers::HeaderName { | ||
"x-ms-blob-content-encoding".into() | ||
} | ||
|
||
fn value(&self) -> headers::HeaderValue { | ||
self.0.to_string().into() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use azure_core::headers::{self, Header}; | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
pub struct BlobContentLanguage(std::borrow::Cow<'static, str>); | ||
|
||
impl BlobContentLanguage { | ||
pub const fn from_static(s: &'static str) -> Self { | ||
Self(std::borrow::Cow::Borrowed(s)) | ||
} | ||
|
||
pub fn as_str(&self) -> &str { | ||
self.0.as_ref() | ||
} | ||
} | ||
|
||
impl From<&'static str> for BlobContentLanguage { | ||
fn from(s: &'static str) -> Self { | ||
Self::from_static(s) | ||
} | ||
} | ||
|
||
impl From<String> for BlobContentLanguage { | ||
fn from(s: String) -> Self { | ||
Self(std::borrow::Cow::Owned(s)) | ||
} | ||
} | ||
|
||
impl From<&String> for BlobContentLanguage { | ||
fn from(s: &String) -> Self { | ||
Self(std::borrow::Cow::Owned(s.clone())) | ||
} | ||
} | ||
|
||
impl Header for BlobContentLanguage { | ||
fn name(&self) -> headers::HeaderName { | ||
"x-ms-blob-content-language".into() | ||
} | ||
|
||
fn value(&self) -> headers::HeaderValue { | ||
self.0.to_string().into() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.