Skip to main content

Bucket

Struct Bucket 

Source
pub struct Bucket { /* private fields */ }
Expand description

An object-storage bucket.

Implementations§

Source§

impl Bucket

Source

pub const fn new(name: &'static str) -> Self

Declare a bucket. const so it can initialize a module-scope static.

Options follow the name, each a method of its own:

use encore::objects::Bucket;

static AVATARS: Bucket = Bucket::new("avatars").public().versioned();
Source

pub const fn public(self) -> Self

Makes the bucket public: its objects are readable by anyone, at the URL Bucket::public_url gives.

Part of the declaration: it takes the bucket by value, so it cannot be called on the static.

Source

pub const fn versioned(self) -> Self

Makes the bucket versioned: an object keeps its earlier versions, which the _with operations can name.

Part of the declaration, like Bucket::public.

Source

pub const fn named(name: &'static str) -> Self

Reference an existing bucket by name. To declare a new bucket, use Bucket::new instead.

The name must be a string literal, so Encore can resolve it statically. const so it can initialize a module-scope static. A named bucket takes no options: it is configured where it is declared.

static UPLOADS: encore::objects::Bucket = encore::objects::Bucket::named("uploads");
Source

pub const fn reference<P: BucketPerms>(&'static self) -> BucketRef<P::Id>

A reference to the bucket that grants only the permissions P: one of the perms, or a tuple of them. Unlike the bucket, a reference can be passed to other code; the service is granted the permissions it names.

The permissions must be written as the type argument, since they are what the service is granted: the compiler never infers them. const, so a reference can initialize a module-scope static.

use encore::objects::{perms, Bucket, BucketRef};

static UPLOADS: Bucket = Bucket::named("uploads");
static AVATARS: BucketRef<(perms::Uploader, perms::Lister)> =
    UPLOADS.reference::<(perms::Uploader, perms::Lister)>();
ⓘ
// Not even from the type it is assigned to.
static AVATARS: BucketRef<perms::Uploader> = UPLOADS.reference();
Source

pub async fn list(&self, options: ListOptions) -> Result<ListIterator, Error>

List the objects in the bucket — those under options.prefix, up to options.limit — as an iterator of entries.

ⓘ
let mut entries = UPLOADS.list(ListOptions { prefix: Some("avatars/".into()), ..Default::default() }).await?;
while let Some(entry) = entries.next().await {
    println!("{}", entry?.name);
}
Source

pub async fn exists(&self, key: &str) -> Result<bool, Error>

Whether an object with key exists.

Source

pub async fn exists_with( &self, key: &str, options: ExistsOptions, ) -> Result<bool, Error>

Whether an object exists, with explicit options (a specific version).

Source

pub async fn attrs(&self, key: &str) -> Result<ObjectAttrs, Error>

The attributes of the object key: its size, content type, version and etag.

Source

pub async fn attrs_with( &self, key: &str, options: AttrsOptions, ) -> Result<ObjectAttrs, Error>

An object’s attributes, with explicit options (a specific version).

Source

pub async fn upload( &self, key: &str, data: Vec<u8>, ) -> Result<ObjectAttrs, Error>

Upload data to the object key, returning its stored attributes.

Source

pub async fn upload_with( &self, key: &str, data: Vec<u8>, options: UploadOptions, ) -> Result<ObjectAttrs, Error>

Upload data with explicit options — a content type, or a precondition such as “only if it does not exist yet”.

Source

pub async fn upload_stream( &self, key: &str, reader: impl AsyncRead + Unpin + Send + Sync + 'static, options: UploadOptions, ) -> Result<ObjectAttrs, Error>

Upload the object key from a reader, streaming its contents rather than buffering them.

Source

pub async fn upload_chunks<S, B, E>( &self, key: &str, chunks: S, options: UploadOptions, ) -> Result<ObjectAttrs, Error>
where S: Stream<Item = Result<B, E>> + Send + 'static, B: Into<Bytes> + 'static, E: Into<Box<dyn Error + Send + Sync>> + 'static,

Upload the object key from a stream of chunks, such as the body of a request being forwarded. A chunk is anything that becomes Bytes: Bytes itself, Vec<u8>, String, &'static [u8].

ⓘ
let chunks = futures::stream::iter([Ok::<_, std::io::Error>(Bytes::from("hello"))]);
UPLOADS.upload_chunks("greeting.txt", chunks, UploadOptions::default()).await?;
Source

pub async fn download(&self, key: &str) -> Result<Vec<u8>, Error>

Download the full contents of the object key.

Source

pub async fn download_with( &self, key: &str, options: DownloadOptions, ) -> Result<Vec<u8>, Error>

Download the object key with explicit options — a specific version, or only a byte range of the object.

ⓘ
// The 256 bytes starting at offset 1024.
let head = UPLOADS.download_with("big.bin", DownloadOptions {
    range: Some(ByteRange { offset: 1024, length: Some(256) }),
    ..Default::default()
}).await?;
Source

pub async fn download_stream( &self, key: &str, options: DownloadOptions, ) -> Result<Download, Error>

Download the object key as it arrives, rather than buffering it whole: see Download.

Source

pub async fn remove(&self, key: &str) -> Result<(), Error>

Remove the object key.

Source

pub async fn remove_with( &self, key: &str, options: RemoveOptions, ) -> Result<(), Error>

Remove an object, with explicit options (a specific version).

Source

pub async fn signed_upload_url( &self, key: &str, ) -> Result<SignedUploadUrl, Error>

A URL through which anyone holding it can upload the object key, valid for an hour.

Source

pub async fn signed_upload_url_with( &self, key: &str, options: SignedUrlOptions, ) -> Result<SignedUploadUrl, Error>

A signed upload URL, with explicit options (how long it is valid for).

ⓘ
let upload = UPLOADS.signed_upload_url_with("report.pdf", SignedUrlOptions {
    ttl: Some(Duration::from_secs(5 * 60)),
}).await?;
Source

pub async fn signed_download_url( &self, key: &str, ) -> Result<SignedDownloadUrl, Error>

A URL through which anyone holding it can download the object key, valid for an hour.

Source

pub async fn signed_download_url_with( &self, key: &str, options: SignedUrlOptions, ) -> Result<SignedDownloadUrl, Error>

A signed download URL, with explicit options (how long it is valid for).

Source

pub fn public_url(&self, key: &str) -> Result<String, Error>

The public URL of the object key. Only a bucket declared public has one.

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
Source§

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

Source§

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

Source§

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

Source§

fn exact_from(value: T) -> U

Source§

impl<T, U> ExactInto<U> for T
where U: ExactFrom<T>,

Source§

fn exact_into(self) -> U

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self> ⓘ

Attaches the current Context to this type, returning a WithContext wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<T, U> OverflowingInto<U> for T
where U: OverflowingFrom<T>,

Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

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

Source§

impl<T, U> RoundingInto<U> for T
where U: RoundingFrom<T>,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> SaturatingInto<U> for T
where U: SaturatingFrom<T>,

Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T, U> WrappingInto<U> for T
where U: WrappingFrom<T>,

Source§

fn wrapping_into(self) -> U