pub enum Command<'a> {
Show 24 variants
HeadObject,
CopyObject {
from: &'a str,
},
DeleteObject,
DeleteObjectTagging,
GetObject,
GetObjectTorrent,
GetObjectRange {
start: u64,
end: Option<u64>,
},
GetObjectTagging,
PutObject {
content: &'a [u8],
content_type: &'a str,
multipart: Option<Multipart<'a>>,
},
PutObjectTagging {
tags: &'a str,
},
ListMultipartUploads {
prefix: Option<&'a str>,
delimiter: Option<&'a str>,
key_marker: Option<String>,
max_uploads: Option<usize>,
},
ListObjects {
prefix: String,
delimiter: Option<String>,
marker: Option<String>,
max_keys: Option<usize>,
},
ListObjectsV2 {
prefix: String,
delimiter: Option<String>,
continuation_token: Option<String>,
start_after: Option<String>,
max_keys: Option<usize>,
},
GetBucketLocation,
PresignGet {
expiry_secs: u32,
custom_queries: Option<HashMap<String, String>>,
},
PresignPut {
expiry_secs: u32,
custom_headers: Option<HeaderMap>,
},
PresignPost {
expiry_secs: u32,
post_policy: String,
},
PresignDelete {
expiry_secs: u32,
},
InitiateMultipartUpload {
content_type: &'a str,
},
UploadPart {
part_number: u32,
content: &'a [u8],
upload_id: &'a str,
},
AbortMultipartUpload {
upload_id: &'a str,
},
CompleteMultipartUpload {
upload_id: &'a str,
data: CompleteMultipartUploadData,
},
CreateBucket {
config: BucketConfiguration,
},
DeleteBucket,
}Variants§
HeadObject
CopyObject
DeleteObject
DeleteObjectTagging
GetObject
GetObjectTorrent
GetObjectRange
GetObjectTagging
PutObject
PutObjectTagging
Fields
ListMultipartUploads
Fields
ListObjects
ListObjectsV2
Fields
GetBucketLocation
PresignGet
PresignPut
PresignPost
PresignDelete
InitiateMultipartUpload
UploadPart
AbortMultipartUpload
CompleteMultipartUpload
CreateBucket
Fields
§
config: BucketConfigurationDeleteBucket
Implementations§
source§impl<'a> Command<'a>
impl<'a> Command<'a>
sourcepub fn http_verb(&self) -> HttpMethod
pub fn http_verb(&self) -> HttpMethod
Examples found in repository?
src/request/request_trait.rs (line 246)
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 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 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
fn presigned_canonical_request(&self, headers: &HeaderMap) -> Result<String, S3Error> {
let (expiry, custom_headers, custom_queries) = match self.command() {
Command::PresignGet {
expiry_secs,
custom_queries,
} => (expiry_secs, None, custom_queries),
Command::PresignPut {
expiry_secs,
custom_headers,
} => (expiry_secs, custom_headers, None),
Command::PresignDelete { expiry_secs } => (expiry_secs, None, None),
_ => unreachable!(),
};
signing::canonical_request(
&self.command().http_verb().to_string(),
&self.presigned_url_no_sig(expiry, custom_headers.as_ref(), custom_queries.as_ref())?,
headers,
"UNSIGNED-PAYLOAD",
)
}
fn presigned_url_no_sig(
&self,
expiry: u32,
custom_headers: Option<&HeaderMap>,
custom_queries: Option<&HashMap<String, String>>,
) -> Result<Url, S3Error> {
let bucket = self.bucket();
let token = if let Some(security_token) = bucket.security_token()? {
Some(security_token)
} else {
bucket.session_token()?
};
let url = Url::parse(&format!(
"{}{}{}",
self.url()?,
&signing::authorization_query_params_no_sig(
&self.bucket().access_key()?.unwrap_or_default(),
&self.datetime(),
&self.bucket().region(),
expiry,
custom_headers,
token.as_ref()
)?,
&signing::flatten_queries(custom_queries)?,
))?;
Ok(url)
}
fn url(&self) -> Result<Url, S3Error> {
let mut url_str = self.bucket().url();
if let Command::CreateBucket { .. } = self.command() {
return Ok(Url::parse(&url_str)?);
}
let path = if self.path().starts_with('/') {
self.path()[1..].to_string()
} else {
self.path()[..].to_string()
};
url_str.push('/');
url_str.push_str(&signing::uri_encode(&path, false));
// Append to url_path
#[allow(clippy::collapsible_match)]
match self.command() {
Command::InitiateMultipartUpload { .. } | Command::ListMultipartUploads { .. } => {
url_str.push_str("?uploads")
}
Command::AbortMultipartUpload { upload_id } => {
write!(url_str, "?uploadId={}", upload_id).expect("Could not write to url_str");
}
Command::CompleteMultipartUpload { upload_id, .. } => {
write!(url_str, "?uploadId={}", upload_id).expect("Could not write to url_str");
}
Command::GetObjectTorrent => url_str.push_str("?torrent"),
Command::PutObject { multipart, .. } => {
if let Some(multipart) = multipart {
url_str.push_str(&multipart.query_string())
}
}
_ => {}
}
let mut url = Url::parse(&url_str)?;
for (key, value) in &self.bucket().extra_query {
url.query_pairs_mut().append_pair(key, value);
}
if let Command::ListObjectsV2 {
prefix,
delimiter,
continuation_token,
start_after,
max_keys,
} = self.command().clone()
{
let mut query_pairs = url.query_pairs_mut();
delimiter.map(|d| query_pairs.append_pair("delimiter", &d));
query_pairs.append_pair("prefix", &prefix);
query_pairs.append_pair("list-type", "2");
if let Some(token) = continuation_token {
query_pairs.append_pair("continuation-token", &token);
}
if let Some(start_after) = start_after {
query_pairs.append_pair("start-after", &start_after);
}
if let Some(max_keys) = max_keys {
query_pairs.append_pair("max-keys", &max_keys.to_string());
}
}
if let Command::ListObjects {
prefix,
delimiter,
marker,
max_keys,
} = self.command().clone()
{
let mut query_pairs = url.query_pairs_mut();
delimiter.map(|d| query_pairs.append_pair("delimiter", &d));
query_pairs.append_pair("prefix", &prefix);
if let Some(marker) = marker {
query_pairs.append_pair("marker", &marker);
}
if let Some(max_keys) = max_keys {
query_pairs.append_pair("max-keys", &max_keys.to_string());
}
}
match self.command() {
Command::ListMultipartUploads {
prefix,
delimiter,
key_marker,
max_uploads,
} => {
let mut query_pairs = url.query_pairs_mut();
delimiter.map(|d| query_pairs.append_pair("delimiter", d));
if let Some(prefix) = prefix {
query_pairs.append_pair("prefix", prefix);
}
if let Some(key_marker) = key_marker {
query_pairs.append_pair("key-marker", &key_marker);
}
if let Some(max_uploads) = max_uploads {
query_pairs.append_pair("max-uploads", max_uploads.to_string().as_str());
}
}
Command::PutObjectTagging { .. }
| Command::GetObjectTagging
| Command::DeleteObjectTagging => {
url.query_pairs_mut().append_pair("tagging", "");
}
_ => {}
}
Ok(url)
}
fn canonical_request(&self, headers: &HeaderMap) -> Result<String, S3Error> {
signing::canonical_request(
&self.command().http_verb().to_string(),
&self.url()?,
headers,
&self.command().sha256(),
)
}More examples
src/request/tokio_backend.rs (line 80)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
async fn response(&self) -> Result<Response, S3Error> {
// Build headers
let headers = match self.headers() {
Ok(headers) => headers,
Err(e) => return Err(e),
};
let mut client_builder = Client::builder();
if let Some(timeout) = self.bucket.request_timeout {
client_builder = client_builder.timeout(timeout)
}
if cfg!(feature = "no-verify-ssl") {
cfg_if::cfg_if! {
if #[cfg(feature = "tokio-native-tls")]
{
client_builder = client_builder.danger_accept_invalid_hostnames(true);
}
}
cfg_if::cfg_if! {
if #[cfg(any(feature = "tokio-native-tls", feature = "tokio-rustls-tls"))]
{
client_builder = client_builder.danger_accept_invalid_certs(true);
}
}
}
let client = client_builder.build()?;
let method = match self.command.http_verb() {
HttpMethod::Delete => reqwest::Method::DELETE,
HttpMethod::Get => reqwest::Method::GET,
HttpMethod::Post => reqwest::Method::POST,
HttpMethod::Put => reqwest::Method::PUT,
HttpMethod::Head => reqwest::Method::HEAD,
};
let request = client
.request(method, self.url()?.as_str())
.headers(headers)
.body(self.request_body());
let response = request.send().await?;
if cfg!(feature = "fail-on-err") && !response.status().is_success() {
let status = response.status().as_u16();
let text = response.text().await?;
return Err(S3Error::Http(status, text));
}
Ok(response)
}sourcepub fn content_length(&self) -> usize
pub fn content_length(&self) -> usize
Examples found in repository?
src/request/request_trait.rs (line 452)
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)
}sourcepub fn content_type(&self) -> String
pub fn content_type(&self) -> String
Examples found in repository?
src/request/request_trait.rs (line 454)
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)
}sourcepub fn sha256(&self) -> String
pub fn sha256(&self) -> String
Examples found in repository?
src/request/request_trait.rs (line 403)
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 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 canonical_request(&self, headers: &HeaderMap) -> Result<String, S3Error> {
signing::canonical_request(
&self.command().http_verb().to_string(),
&self.url()?,
headers,
&self.command().sha256(),
)
}
fn authorization(&self, headers: &HeaderMap) -> Result<String, S3Error> {
let canonical_request = self.canonical_request(headers)?;
let string_to_sign = self.string_to_sign(&canonical_request)?;
let mut hmac = signing::HmacSha256::new_from_slice(&self.signing_key()?)?;
hmac.update(string_to_sign.as_bytes());
let signature = hex::encode(hmac.finalize().into_bytes());
let signed_header = signing::signed_header_string(headers);
signing::authorization_header(
&self.bucket().access_key()?.expect("No access_key provided"),
&self.datetime(),
&self.bucket().region(),
&signed_header,
&signature,
)
}
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)
}