#header #s3 #service #region #communicating #necessary #access-key

common-s3-headers

Only the headers necessary for communicating with S3-like services. It's all you need.

3 releases (1 stable)

1.0.0 Jan 11, 2024
0.1.0 Jan 10, 2024
0.0.1 Jan 10, 2024

#219 in HTTP client

Download history 11/week @ 2024-07-22 8/week @ 2024-09-23 5/week @ 2024-09-30 727/week @ 2024-10-07 933/week @ 2024-10-14 23/week @ 2024-10-21 25/week @ 2024-11-04

1,133 downloads per month

Apache-2.0

46KB
844 lines

Common S3 Headers

Only the headers necessary for communicating with S3-like services. It's all you need.

Get

// Get from S3 from some location.
let url = Url::from_str("https://example.s3.amazonaws.com/test/test.json").unwrap();

let headers = S3HeadersBuilder::new(&url)
  .set_access_key(&access_key)
  .set_secret_key(&secret_key)
  .set_region(&region)
  .set_method("GET")
  .set_service("s3")
  .build();

// Use Reqwest or any other library!
let response = reqwest::blocking::Client::new()
  .get(url)
  .headers(reqwest::header::HeaderMap::from_iter(headers.into_iter().map(
    |(k, v)| {
      (
        reqwest::header::HeaderName::from_str(k).unwrap(),
        reqwest::header::HeaderValue::from_str(&v).unwrap(),
      )
    },
  )))
  .send()
  .unwrap();

Put

// We want to put content into S3.
let url = Url::from_str("https://example.s3.amazonaws.com/test/test.json").unwrap();
let content = "{\"c\":\"d\"}\n".as_bytes().to_vec();

// Build the S3 headers.
let (access_key, secret_key, region) = setup::get_config_from_env("TEST_S3");
let headers = S3HeadersBuilder::new(&url)
  .set_access_key(&access_key)
  .set_secret_key(&secret_key)
  .set_region(&region)
  .set_method(PUT_METHOD)
  .set_service("s3")
  .set_payload_hash_with_content(&content)
  .build();

// Use Reqwest or any other library!
let response = reqwest::blocking::Client::new()
  .put(url)
  .headers(reqwest::header::HeaderMap::from_iter(headers.into_iter().map(
    |(k, v)| {
      (
        reqwest::header::HeaderName::from_str(k).unwrap(),
        reqwest::header::HeaderValue::from_str(&v).unwrap(),
      )
    },
  )))
  .send()
  .unwrap();

List

let url = Url::from_str("https://example.s3.amazonaws.com/").unwrap();

let headers = S3HeadersBuilder::new(&url)
  .set_access_key(&access_key)
  .set_secret_key(&secret_key)
  .set_region(&region)
  .set_method("GET")
  .set_service("s3")
  .build();

// Use Reqwest or any other library!
let response = reqwest::blocking::Client::new()
  .get(url)
  .headers(reqwest::header::HeaderMap::from_iter(headers.into_iter().map(
    |(k, v)| {
      (
        reqwest::header::HeaderName::from_str(k).unwrap(),
        reqwest::header::HeaderValue::from_str(&v).unwrap(),
      )
    },
  )))
  .send()
  .unwrap();

Dependencies

~3–4.5MB
~81K SLoC