Skip to content

Commit

Permalink
fix update
Browse files Browse the repository at this point in the history
  • Loading branch information
RWDai committed Jun 19, 2024
1 parent 3ee40ba commit 55f2947
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 53 deletions.
6 changes: 3 additions & 3 deletions s3/src/bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ impl Bucket {

#[maybe_async::maybe_async]
pub async fn get_bucket_lifecycle(&self) -> Result<BucketLifecycleConfiguration, S3Error> {
let request = RequestImpl::new(self, "?lifecycle", Command::GetBucketLifecycle).await?;
let request = RequestImpl::new(self, "", Command::GetBucketLifecycle).await?;
let response = request.response_data(false).await?;
Ok(quick_xml::de::from_str::<BucketLifecycleConfiguration>(
response.as_str()?,
Expand All @@ -798,13 +798,13 @@ impl Bucket {
let command = Command::PutBucketLifecycle {
configuration: lifecycle_config,
};
let request = RequestImpl::new(self, "?lifecycle", command).await?;
let request = RequestImpl::new(self, "", command).await?;
request.response_data(false).await
}

#[maybe_async::maybe_async]
pub async fn delete_bucket_lifecycle(&self) -> Result<ResponseData, S3Error> {
let request = RequestImpl::new(self, "?lifecycle", Command::DeleteBucket).await?;
let request = RequestImpl::new(self, "", Command::DeleteBucket).await?;
request.response_data(false).await
}

Expand Down
12 changes: 3 additions & 9 deletions s3/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ impl<'a> Command<'a> {
match self {
Command::InitiateMultipartUpload { content_type } => content_type.to_string(),
Command::PutObject { content_type, .. } => content_type.to_string(),
Command::CompleteMultipartUpload { .. } => "application/xml".into(),
Command::CompleteMultipartUpload { .. } | Command::PutBucketLifecycle { .. } => {
"application/xml".into()
}
_ => "text/plain".into(),
}
}
Expand Down Expand Up @@ -231,14 +233,6 @@ impl<'a> Command<'a> {
EMPTY_PAYLOAD_SHA.into()
}
}
Command::PutBucketLifecycle {
configuration: config,
..
} => {
let mut sha = Sha256::default();
sha.update(quick_xml::se::to_string(config)?.as_bytes());
hex::encode(sha.finalize().as_slice())
}
_ => EMPTY_PAYLOAD_SHA.into(),
};
return Ok(result);
Expand Down
11 changes: 11 additions & 0 deletions s3/src/request/request_trait.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use base64::engine::general_purpose;
use base64::Engine;
use hmac::Mac;
use quick_xml::se::to_string;
use std::collections::HashMap;
#[cfg(any(feature = "with-tokio", feature = "with-async-std"))]
use std::pin::Pin;
Expand Down Expand Up @@ -380,6 +381,11 @@ pub trait Request {
url_str.push_str(&multipart.query_string())
}
}
Command::GetBucketLifecycle
| Command::PutBucketLifecycle { .. }
| Command::DeleteBucketLifecycle => {
url_str.push_str("?lifecycle");
}
_ => {}
}

Expand Down Expand Up @@ -587,6 +593,11 @@ pub trait Request {
headers.insert(RANGE, range.parse()?);
} else if let Command::CreateBucket { ref config } = self.command() {
config.add_headers(&mut headers)?;
} else if let Command::PutBucketLifecycle { ref configuration } = self.command() {
let digest = md5::compute(to_string(configuration)?.as_bytes());
let hash = general_purpose::STANDARD.encode(digest.as_ref());
headers.insert(HeaderName::from_static("content-md5"), hash.parse()?);
headers.remove("x-amz-content-sha256");
}

// This must be last, as it signs the other headers, omitted if no secret key is provided
Expand Down
85 changes: 44 additions & 41 deletions s3/src/serde_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ impl CorsRule {
#[serde(rename = "LifecycleConfiguration")]
pub struct BucketLifecycleConfiguration {
#[serde(rename = "Rule")]
rules: Vec<LifecycleRule>,
pub rules: Vec<LifecycleRule>,
}

impl BucketLifecycleConfiguration {
Expand All @@ -390,52 +390,52 @@ pub struct LifecycleRule {
rename = "AbortIncompleteMultipartUpload",
skip_serializing_if = "Option::is_none"
)]
abort_incomplete_multipart_upload: Option<AbortIncompleteMultipartUpload>,
pub abort_incomplete_multipart_upload: Option<AbortIncompleteMultipartUpload>,

#[serde(rename = "Expiration", skip_serializing_if = "Option::is_none")]
expiration: Option<Expiration>,
pub expiration: Option<Expiration>,

#[serde(rename = "Filter", skip_serializing_if = "Option::is_none")]
filter: Option<LifecycleFilter>,
pub filter: Option<LifecycleFilter>,

#[serde(rename = "ID", skip_serializing_if = "Option::is_none")]
id: Option<String>,
pub id: Option<String>,

#[serde(
rename = "NoncurrentVersionExpiration",
skip_serializing_if = "Option::is_none"
)]
noncurrent_version_expiration: Option<NoncurrentVersionExpiration>,
pub noncurrent_version_expiration: Option<NoncurrentVersionExpiration>,

#[serde(
rename = "NoncurrentVersionTransition",
skip_serializing_if = "Option::is_none"
)]
noncurrent_version_transition: Option<Vec<NoncurrentVersionTransition>>,
pub noncurrent_version_transition: Option<Vec<NoncurrentVersionTransition>>,

#[serde(rename = "Status")]
/// Valid Values: Enabled | Disabled
status: String,
pub status: String,

#[serde(rename = "Transition", skip_serializing_if = "Option::is_none")]
transition: Option<Vec<Transition>>,
pub transition: Option<Vec<Transition>>,
}

pub struct LifecycleRuleBuilder {
lifecycle_rule: LifecycleRule,
}

impl LifecycleRule {
pub fn builder(status: String) -> LifecycleRuleBuilder {
pub fn builder(status: &str) -> LifecycleRuleBuilder {
LifecycleRuleBuilder::new(status)
}
}

impl LifecycleRuleBuilder {
pub fn new(status: String) -> LifecycleRuleBuilder {
pub fn new(status: &str) -> LifecycleRuleBuilder {
LifecycleRuleBuilder {
lifecycle_rule: LifecycleRule {
status,
status: status.to_string(),
..Default::default()
},
}
Expand All @@ -460,8 +460,8 @@ impl LifecycleRuleBuilder {
self
}

pub fn id(mut self, id: String) -> LifecycleRuleBuilder {
self.lifecycle_rule.id = Some(id);
pub fn id(mut self, id: &str) -> LifecycleRuleBuilder {
self.lifecycle_rule.id = Some(id.to_string());
self
}

Expand Down Expand Up @@ -496,7 +496,7 @@ pub struct AbortIncompleteMultipartUpload {
rename = "DaysAfterInitiation",
skip_serializing_if = "Option::is_none"
)]
days_after_initiation: Option<i32>,
pub days_after_initiation: Option<i32>,
}

impl AbortIncompleteMultipartUpload {
Expand All @@ -511,23 +511,23 @@ impl AbortIncompleteMultipartUpload {
pub struct Expiration {
/// Indicates at what date the object is to be moved or deleted. The date value must conform to the ISO 8601 format. The time is always midnight UTC.
#[serde(rename = "Date", skip_serializing_if = "Option::is_none")]
date: Option<String>,
pub date: Option<String>,

#[serde(rename = "Days", skip_serializing_if = "Option::is_none")]
days: Option<i32>,
pub days: Option<u32>,

/// Indicates whether Amazon S3 will remove a delete marker with no noncurrent versions. If set to true, the delete marker will be expired; if set to false the policy takes no action. This cannot be specified with Days or Date in a Lifecycle Expiration Policy.
#[serde(
rename = "ExpiredObjectDeleteMarker",
skip_serializing_if = "Option::is_none"
)]
expired_object_delete_marker: Option<bool>,
pub expired_object_delete_marker: Option<bool>,
}

impl Expiration {
pub fn new(
date: Option<String>,
days: Option<i32>,
days: Option<u32>,
expired_object_delete_marker: Option<bool>,
) -> Self {
Self {
Expand All @@ -541,22 +541,22 @@ impl Expiration {
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct LifecycleFilter {
#[serde(rename = "And", skip_serializing_if = "Option::is_none")]
and: Option<And>,
pub and: Option<And>,

#[serde(
rename = "ObjectSizeGreaterThan",
skip_serializing_if = "Option::is_none"
)]
object_size_greater_than: Option<i64>,
pub object_size_greater_than: Option<i64>,

#[serde(rename = "ObjectSizeLessThan", skip_serializing_if = "Option::is_none")]
object_size_less_than: Option<i64>,
pub object_size_less_than: Option<i64>,

#[serde(rename = "Prefix", skip_serializing_if = "Option::is_none")]
prefix: Option<String>,
pub prefix: Option<String>,

#[serde(rename = "Tag", skip_serializing_if = "Option::is_none")]
tag: Option<Tag>,
pub tag: Option<Tag>,
}
impl LifecycleFilter {
pub fn new(
Expand All @@ -582,16 +582,16 @@ pub struct And {
rename = "ObjectSizeGreaterThan",
skip_serializing_if = "Option::is_none"
)]
object_size_greater_than: Option<i64>,
pub object_size_greater_than: Option<i64>,

#[serde(rename = "ObjectSizeLessThan", skip_serializing_if = "Option::is_none")]
object_size_less_than: Option<i64>,
pub object_size_less_than: Option<i64>,

#[serde(rename = "Prefix", skip_serializing_if = "Option::is_none")]
prefix: Option<String>,
pub prefix: Option<String>,

#[serde(rename = "Tag", skip_serializing_if = "Option::is_none")]
tags: Option<Vec<Tag>>,
pub tags: Option<Vec<Tag>>,
}

impl And {
Expand All @@ -613,15 +613,18 @@ impl And {
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Tag {
#[serde(rename = "Key")]
key: String,
pub key: String,

#[serde(rename = "Value")]
value: String,
pub value: String,
}

impl Tag {
pub fn new(key: String, value: String) -> Self {
Self { key, value }
pub fn new(key: &str, value: &str) -> Self {
Self {
key: key.to_string(),
value: value.to_string(),
}
}
}

Expand All @@ -631,10 +634,10 @@ pub struct NoncurrentVersionExpiration {
rename = "NewerNoncurrentVersions",
skip_serializing_if = "Option::is_none"
)]
newer_noncurrent_versions: Option<i32>,
pub newer_noncurrent_versions: Option<i32>,

#[serde(rename = "NoncurrentDays", skip_serializing_if = "Option::is_none")]
noncurrent_days: Option<i32>,
pub noncurrent_days: Option<i32>,
}

impl NoncurrentVersionExpiration {
Expand All @@ -652,14 +655,14 @@ pub struct NoncurrentVersionTransition {
rename = "NewerNoncurrentVersions",
skip_serializing_if = "Option::is_none"
)]
newer_noncurrent_versions: Option<i32>,
pub newer_noncurrent_versions: Option<i32>,

#[serde(rename = "NoncurrentDays", skip_serializing_if = "Option::is_none")]
noncurrent_days: Option<i32>,
pub noncurrent_days: Option<i32>,

#[serde(rename = "StorageClass", skip_serializing_if = "Option::is_none")]
/// Valid Values: GLACIER | STANDARD_IA | ONEZONE_IA | INTELLIGENT_TIERING | DEEP_ARCHIVE | GLACIER_IR
storage_class: Option<String>,
pub storage_class: Option<String>,
}

impl NoncurrentVersionTransition {
Expand All @@ -679,17 +682,17 @@ impl NoncurrentVersionTransition {
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Transition {
#[serde(rename = "Date", skip_serializing_if = "Option::is_none")]
date: Option<String>,
pub date: Option<String>,

#[serde(rename = "Days", skip_serializing_if = "Option::is_none")]
days: Option<i32>,
pub days: Option<u32>,
/// Valid Values: GLACIER | STANDARD_IA | ONEZONE_IA | INTELLIGENT_TIERING | DEEP_ARCHIVE | GLACIER_IR
#[serde(rename = "StorageClass")]
storage_class: Option<String>,
pub storage_class: Option<String>,
}

impl Transition {
pub fn new(date: Option<String>, days: Option<i32>, storage_class: Option<String>) -> Self {
pub fn new(date: Option<String>, days: Option<u32>, storage_class: Option<String>) -> Self {
Transition {
date,
days,
Expand Down

0 comments on commit 55f2947

Please sign in to comment.