Struct s3::bucket_ops::BucketConfiguration
source · pub struct BucketConfiguration { /* private fields */ }Implementations§
source§impl BucketConfiguration
impl BucketConfiguration
pub fn public() -> Self
sourcepub fn set_region(&mut self, region: Region)
pub fn set_region(&mut self, region: Region)
Examples found in repository?
src/bucket.rs (line 310)
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
pub async fn create(
name: &str,
region: Region,
credentials: Credentials,
config: BucketConfiguration,
) -> Result<CreateBucketResponse, S3Error> {
let mut config = config;
config.set_region(region.clone());
let command = Command::CreateBucket { config };
let bucket = Bucket::new(name, region, credentials)?;
let request = RequestImpl::new(&bucket, "", command)?;
let response_data = request.response_data(false).await?;
let response_text = response_data.as_str()?;
Ok(CreateBucketResponse {
bucket,
response_text: response_text.to_string(),
response_code: response_data.status_code(),
})
}
/// Create a new `Bucket` with path style and instantiate it
///
/// ```no_run
/// use s3::{Bucket, BucketConfiguration};
/// use s3::creds::Credentials;
/// # use s3::region::Region;
/// use anyhow::Result;
///
/// # #[tokio::main]
/// # async fn main() -> Result<()> {
/// let bucket_name = "rust-s3-test";
/// let region = "us-east-1".parse()?;
/// let credentials = Credentials::default()?;
/// let config = BucketConfiguration::default();
///
/// // Async variant with `tokio` or `async-std` features
/// let create_bucket_response = Bucket::create_with_path_style(bucket_name, region, credentials, config).await?;
///
/// // `sync` fature will produce an identical method
/// #[cfg(feature = "sync")]
/// let create_bucket_response = Bucket::create_with_path_style(bucket_name, region, credentials, config)?;
///
/// # let region: Region = "us-east-1".parse()?;
/// # let credentials = Credentials::default()?;
/// # let config = BucketConfiguration::default();
/// // Blocking variant, generated with `blocking` feature in combination
/// // with `tokio` or `async-std` features.
/// #[cfg(feature = "blocking")]
/// let create_bucket_response = Bucket::create_with_path_style_blocking(bucket_name, region, credentials, config)?;
/// # Ok(())
/// # }
/// ```
#[maybe_async::maybe_async]
pub async fn create_with_path_style(
name: &str,
region: Region,
credentials: Credentials,
config: BucketConfiguration,
) -> Result<CreateBucketResponse, S3Error> {
let mut config = config;
config.set_region(region.clone());
let command = Command::CreateBucket { config };
let bucket = Bucket::new(name, region, credentials)?.with_path_style();
let request = RequestImpl::new(&bucket, "", command)?;
let response_data = request.response_data(false).await?;
let response_text = response_data.to_string()?;
Ok(CreateBucketResponse {
bucket,
response_text,
response_code: response_data.status_code(),
})
}sourcepub fn set_location_constraint(&mut self, region: Region)
pub fn set_location_constraint(&mut self, region: Region)
sourcepub fn location_constraint_payload(&self) -> Option<String>
pub fn location_constraint_payload(&self) -> Option<String>
Examples found in repository?
src/command.rs (line 172)
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
pub fn content_length(&self) -> usize {
match &self {
Command::CopyObject { from: _ } => 0,
Command::PutObject { content, .. } => content.len(),
Command::PutObjectTagging { tags } => tags.len(),
Command::UploadPart { content, .. } => content.len(),
Command::CompleteMultipartUpload { data, .. } => data.len(),
Command::CreateBucket { config } => {
if let Some(payload) = config.location_constraint_payload() {
Vec::from(payload).len()
} else {
0
}
}
_ => 0,
}
}
pub fn content_type(&self) -> String {
match self {
Command::InitiateMultipartUpload { content_type } => content_type.to_string(),
Command::PutObject { content_type, .. } => content_type.to_string(),
Command::CompleteMultipartUpload { .. } => "application/xml".into(),
_ => "text/plain".into(),
}
}
pub fn sha256(&self) -> String {
match &self {
Command::PutObject { content, .. } => {
let mut sha = Sha256::default();
sha.update(content);
hex::encode(sha.finalize().as_slice())
}
Command::PutObjectTagging { tags } => {
let mut sha = Sha256::default();
sha.update(tags.as_bytes());
hex::encode(sha.finalize().as_slice())
}
Command::CompleteMultipartUpload { data, .. } => {
let mut sha = Sha256::default();
sha.update(data.to_string().as_bytes());
hex::encode(sha.finalize().as_slice())
}
Command::CreateBucket { config } => {
if let Some(payload) = config.location_constraint_payload() {
let mut sha = Sha256::default();
sha.update(payload.as_bytes());
hex::encode(sha.finalize().as_slice())
} else {
EMPTY_PAYLOAD_SHA.into()
}
}
_ => EMPTY_PAYLOAD_SHA.into(),
}
}More examples
src/request/request_trait.rs (line 160)
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
fn request_body(&self) -> Vec<u8> {
if let Command::PutObject { content, .. } = self.command() {
Vec::from(content)
} else if let Command::PutObjectTagging { tags } = self.command() {
Vec::from(tags)
} else if let Command::UploadPart { content, .. } = self.command() {
Vec::from(content)
} else if let Command::CompleteMultipartUpload { data, .. } = &self.command() {
let body = data.to_string();
println!("CompleteMultipartUpload: {}", body);
body.as_bytes().to_vec()
} else if let Command::CreateBucket { config } = &self.command() {
if let Some(payload) = config.location_constraint_payload() {
Vec::from(payload)
} else {
Vec::new()
}
} else {
Vec::new()
}
}sourcepub fn add_headers(&self, headers: &mut HeaderMap) -> Result<(), S3Error>
pub fn add_headers(&self, headers: &mut HeaderMap) -> Result<(), S3Error>
Examples found in repository?
src/request/request_trait.rs (line 504)
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
fn headers(&self) -> Result<HeaderMap, S3Error> {
// Generate this once, but it's used in more than one place.
let sha256 = self.command().sha256();
// Start with extra_headers, that way our headers replace anything with
// the same name.
let mut headers = HeaderMap::new();
for (k, v) in self.bucket().extra_headers.iter() {
headers.insert(k.clone(), v.clone());
}
let host_header = self.host_header();
headers.insert(HOST, host_header.parse()?);
match self.command() {
Command::CopyObject { from } => {
headers.insert(HeaderName::from_static("x-amz-copy-source"), from.parse()?);
}
Command::ListObjects { .. } => {}
Command::ListObjectsV2 { .. } => {}
Command::GetObject => {}
Command::GetObjectTagging => {}
Command::GetBucketLocation => {}
_ => {
headers.insert(
CONTENT_LENGTH,
self.command().content_length().to_string().parse()?,
);
headers.insert(CONTENT_TYPE, self.command().content_type().parse()?);
}
}
headers.insert(
HeaderName::from_static("x-amz-content-sha256"),
sha256.parse()?,
);
headers.insert(
HeaderName::from_static("x-amz-date"),
self.long_date()?.parse()?,
);
if let Some(session_token) = self.bucket().session_token()? {
headers.insert(
HeaderName::from_static("x-amz-security-token"),
session_token.parse()?,
);
} else if let Some(security_token) = self.bucket().security_token()? {
headers.insert(
HeaderName::from_static("x-amz-security-token"),
security_token.parse()?,
);
}
if let Command::PutObjectTagging { tags } = self.command() {
let digest = md5::compute(tags);
let hash = base64::encode(digest.as_ref());
headers.insert(HeaderName::from_static("content-md5"), hash.parse()?);
} else if let Command::PutObject { content, .. } = self.command() {
let digest = md5::compute(content);
let hash = base64::encode(digest.as_ref());
headers.insert(HeaderName::from_static("content-md5"), hash.parse()?);
} else if let Command::UploadPart { content, .. } = self.command() {
let digest = md5::compute(content);
let hash = base64::encode(digest.as_ref());
headers.insert(HeaderName::from_static("content-md5"), hash.parse()?);
} else if let Command::GetObject {} = self.command() {
headers.insert(ACCEPT, "application/octet-stream".to_string().parse()?);
// headers.insert(header::ACCEPT_CHARSET, HeaderValue::from_str("UTF-8")?);
} else if let Command::GetObjectRange { start, end } = self.command() {
headers.insert(ACCEPT, "application/octet-stream".to_string().parse()?);
let mut range = format!("bytes={}-", start);
if let Some(end) = end {
range.push_str(&end.to_string());
}
headers.insert(RANGE, range.parse()?);
} else if let Command::CreateBucket { ref config } = self.command() {
config.add_headers(&mut headers)?;
}
// This must be last, as it signs the other headers, omitted if no secret key is provided
if self.bucket().secret_key()?.is_some() {
let authorization = self.authorization(&headers)?;
headers.insert(AUTHORIZATION, authorization.parse()?);
}
// The format of RFC2822 is somewhat malleable, so including it in
// signed headers can cause signature mismatches. We do include the
// X-Amz-Date header, so requests are still properly limited to a date
// range and can't be used again e.g. reply attacks. Adding this header
// after the generation of the Authorization header leaves it out of
// the signed headers.
headers.insert(DATE, self.datetime().format(&Rfc2822)?.parse()?);
Ok(headers)
}Trait Implementations§
source§impl Clone for BucketConfiguration
impl Clone for BucketConfiguration
source§fn clone(&self) -> BucketConfiguration
fn clone(&self) -> BucketConfiguration
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read more