Skip to main content

S3Endpoint

Struct S3Endpoint 

Source
pub struct S3Endpoint {
    pub host: String,
    pub region: String,
    pub bucket: String,
    pub access_key_id: String,
    pub secret_access_key: String,
}
Expand description

An S3-compatible bucket and the credentials to presign URLs for it.

S3Endpoint::r2 builds the one of an R2 bucket; the runtime helpers (presign_get, direct_upload…) read it from the R2_* variables and secrets. Fill the fields yourself for another S3-compatible store (AWS S3, MinIO…). Debug hides the secret.

§Examples

use ocre::storage::S3Endpoint;

let r2 = S3Endpoint::r2("0123abcd", "blog-storage", "AKID", "s3cr3t");
assert_eq!(r2.host, "0123abcd.r2.cloudflarestorage.com");
assert_eq!(r2.region, "auto");
assert!(!format!("{r2:?}").contains("s3cr3t"));

Fields§

§host: String

Host name of the S3 API, without scheme (<account_id>.r2.cloudflarestorage.com).

§region: String

Signing region: auto for R2.

§bucket: String

Bucket name, put first in the path (/<bucket>/<key>, path style); empty when the host already names the bucket.

§access_key_id: String

Access key ID of the credentials.

§secret_access_key: String

Secret access key of the credentials.

Implementations§

Source§

impl S3Endpoint

Source

pub fn r2( account_id: &str, bucket: &str, access_key_id: &str, secret_access_key: &str, ) -> Self

The S3 endpoint of an R2 bucket: host <account_id>.r2.cloudflarestorage.com, region auto, path-style URLs.

§Examples
let r2 = ocre::storage::S3Endpoint::r2("acc", "blog-storage", "AKID", "secret");
assert_eq!((r2.host.as_str(), r2.bucket.as_str()), ("acc.r2.cloudflarestorage.com", "blog-storage"));
Source

pub fn presign( &self, method: &str, key: &str, headers: &[(&str, &str)], query: &[(&str, &str)], now: i64, expires_in: u64, ) -> Result<String>

Presigns a request on key (AWS SigV4, query-string form) and returns its https:// URL.

headers are signed too (host always is): the client must send them with exactly these values, or the store refuses the request. query adds parameters such as response-content-disposition. The payload is not signed (UNSIGNED-PAYLOAD). now is the Unix time of signing (ocre::now()); the URL works for expires_in seconds. Pure: no binding, a few microseconds of CPU.

§Errors

Error::Internal when expires_in is 0 or over MAX_EXPIRES_IN (7 days).

§Examples
use ocre::storage::S3Endpoint;

let r2 = S3Endpoint::r2("acc", "blog-storage", "AKID", "secret");
let url = r2.presign("GET", "exports/report 1.csv", &[], &[], 1_790_000_000, 300).unwrap();
assert!(url.starts_with("https://acc.r2.cloudflarestorage.com/blog-storage/exports/report%201.csv?X-Amz-Algorithm="));
assert!(url.contains("&X-Amz-Expires=300&"));
assert!(r2.presign("GET", "k", &[], &[], 1_790_000_000, 8 * 24 * 3600).is_err());

Trait Implementations§

Source§

impl Clone for S3Endpoint

Source§

fn clone(&self) -> S3Endpoint

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for S3Endpoint

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for S3Endpoint

Source§

impl PartialEq for S3Endpoint

Source§

fn eq(&self, other: &S3Endpoint) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for S3Endpoint

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromRef<T> for T
where T: Clone,

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V