-
Notifications
You must be signed in to change notification settings - Fork 258
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
storage_blobs: Add support for snapshot blob. #966
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
077b20b
storage_blobs: Add support for snapshot blob.
gorzell d1359ed
Merge branch 'main' of github.com:gorzell/azure-sdk-for-rust into gor…
gorzell a9d14f3
Add a version of the e2e test that uses the mock_transport_framework.
gorzell 0668a18
Merge branch 'main' of github.com:gorzell/azure-sdk-for-rust into gor…
gorzell d1e3fb8
Fix some clippy issues and format JSON properly.
gorzell 7123e4a
Clean up doc comment and make function name locally consistent.
gorzell d957709
Add a feature specific `use` for `StorageCredentials`.
gorzell 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 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
This file contains 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
This file contains 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,58 @@ | ||
#[macro_use] | ||
extern crate log; | ||
|
||
use azure_storage::core::prelude::*; | ||
use azure_storage_blobs::prelude::*; | ||
use bytes::Bytes; | ||
|
||
#[tokio::main] | ||
async fn main() -> azure_core::Result<()> { | ||
env_logger::init(); | ||
debug!("log initialized"); | ||
// First we retrieve the account name and access key from environment variables. | ||
let account = | ||
std::env::var("STORAGE_ACCOUNT").expect("Set env variable STORAGE_ACCOUNT first!"); | ||
let access_key = | ||
std::env::var("STORAGE_ACCESS_KEY").expect("Set env variable STORAGE_ACCESS_KEY first!"); | ||
|
||
let container = std::env::args() | ||
.nth(1) | ||
.expect("please specify container name as command line parameter"); | ||
let blob_name = std::env::args() | ||
.nth(2) | ||
.expect("please specify blob name as command line parameter"); | ||
|
||
let blob_client = StorageClient::new_access_key(&account, &access_key) | ||
.container_client(&container) | ||
.blob_client(&blob_name); | ||
|
||
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[..]); | ||
|
||
// The required parameters are container_name, blob_name and body. | ||
// The builder supports many more optional | ||
// parameters (such as LeaseID, or ContentDisposition, MD5 etc...) | ||
// so make sure to check with the documentation. | ||
let res = blob_client | ||
.put_block_blob(data.clone()) | ||
.content_type("text/plain") | ||
.hash(hash) | ||
.into_future() | ||
.await?; | ||
println!("put_blob {:?}", res); | ||
|
||
let res = blob_client.snapshot().into_future().await?; | ||
println!("blob snapshot: {:?}", res.snapshot); | ||
|
||
let res = blob_client | ||
.delete() | ||
.delete_snapshots_method(DeleteSnapshotsMethod::Include) | ||
.into_future() | ||
.await?; | ||
println!("Delete blob == {:?}", res); | ||
|
||
Ok(()) | ||
} |
This file contains 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
This file contains 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
This file contains 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,68 @@ | ||
use crate::prelude::*; | ||
use azure_core::headers::{etag_from_headers, HeaderName}; | ||
use azure_core::{ | ||
headers::{date_from_headers, last_modified_from_headers, request_id_from_headers, Headers}, | ||
prelude::*, | ||
Method::Put, | ||
RequestId, | ||
}; | ||
use time::OffsetDateTime; | ||
|
||
operation! { | ||
SnapshotBlob, | ||
client: BlobClient, | ||
?metadata: Metadata, | ||
?if_modified_since: IfModifiedSinceCondition, | ||
?if_match: IfMatchCondition, | ||
?lease_id: LeaseId | ||
} | ||
|
||
impl SnapshotBlobBuilder { | ||
pub fn into_future(mut self) -> SnapshotBlob { | ||
Box::pin(async move { | ||
let mut url = self.client.url()?; | ||
|
||
url.query_pairs_mut().append_pair("comp", "snapshot"); | ||
|
||
let mut headers = Headers::new(); | ||
headers.add(self.lease_id); | ||
headers.add(self.if_modified_since); | ||
headers.add(self.if_match); | ||
if let Some(metadata) = &self.metadata { | ||
for m in metadata.iter() { | ||
headers.add(m); | ||
} | ||
} | ||
|
||
let mut request = self.client.finalize_request(url, Put, headers, None)?; | ||
|
||
let response = self.client.send(&mut self.context, &mut request).await?; | ||
response.headers().try_into() | ||
}) | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct SnapshotBlobResponse { | ||
pub request_id: RequestId, | ||
pub etag: String, | ||
pub date: OffsetDateTime, | ||
pub snapshot: Snapshot, | ||
pub last_modified: OffsetDateTime, | ||
} | ||
|
||
impl TryFrom<&Headers> for SnapshotBlobResponse { | ||
type Error = crate::Error; | ||
|
||
fn try_from(headers: &Headers) -> Result<Self, Self::Error> { | ||
Ok(SnapshotBlobResponse { | ||
request_id: request_id_from_headers(headers)?, | ||
etag: etag_from_headers(headers)?, | ||
date: date_from_headers(headers)?, | ||
snapshot: Snapshot::new(headers.get_str(&SNAPSHOT)?.to_string()), | ||
last_modified: last_modified_from_headers(headers)?, | ||
}) | ||
} | ||
} | ||
|
||
pub const SNAPSHOT: HeaderName = HeaderName::from_static("x-ms-snapshot"); | ||
This file contains 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
This file contains 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unsure if this is the right place for this. I'll think about it a bit more...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seemed like most of the other
const
of this type were incore
which was a bit extreme for something storage specific. It could go inlib
, or some other central place in thestorage_blobs
crate for sure.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More specifically: https://github.com/gorzell/azure-sdk-for-rust/blob/main/sdk/core/src/headers/mod.rs
I think it would be fine to move this to a
headers.rs
file at the top level ofstorage_blob
.