pub struct Bucket { /* private fields */ }Expand description
An object-storage bucket.
Implementations§
Source§impl Bucket
impl Bucket
Sourcepub const fn new(name: &'static str) -> Self
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();Sourcepub const fn public(self) -> Self
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.
Sourcepub const fn versioned(self) -> Self
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.
Sourcepub const fn named(name: &'static str) -> Self
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");Sourcepub const fn reference<P: BucketPerms>(&'static self) -> BucketRef<P::Id>
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();Sourcepub async fn list(&self, options: ListOptions) -> Result<ListIterator, Error>
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);
}Sourcepub async fn exists(&self, key: &str) -> Result<bool, Error>
pub async fn exists(&self, key: &str) -> Result<bool, Error>
Whether an object with key exists.
Sourcepub async fn exists_with(
&self,
key: &str,
options: ExistsOptions,
) -> Result<bool, Error>
pub async fn exists_with( &self, key: &str, options: ExistsOptions, ) -> Result<bool, Error>
Whether an object exists, with explicit options (a specific version).
Sourcepub async fn attrs(&self, key: &str) -> Result<ObjectAttrs, Error>
pub async fn attrs(&self, key: &str) -> Result<ObjectAttrs, Error>
The attributes of the object key: its size, content type, version
and etag.
Sourcepub async fn attrs_with(
&self,
key: &str,
options: AttrsOptions,
) -> Result<ObjectAttrs, Error>
pub async fn attrs_with( &self, key: &str, options: AttrsOptions, ) -> Result<ObjectAttrs, Error>
An object’s attributes, with explicit options (a specific version).
Sourcepub async fn upload(
&self,
key: &str,
data: Vec<u8>,
) -> Result<ObjectAttrs, Error>
pub async fn upload( &self, key: &str, data: Vec<u8>, ) -> Result<ObjectAttrs, Error>
Upload data to the object key, returning its stored attributes.
Sourcepub async fn upload_with(
&self,
key: &str,
data: Vec<u8>,
options: UploadOptions,
) -> Result<ObjectAttrs, Error>
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”.
Sourcepub async fn upload_stream(
&self,
key: &str,
reader: impl AsyncRead + Unpin + Send + Sync + 'static,
options: UploadOptions,
) -> Result<ObjectAttrs, Error>
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.
Sourcepub async fn upload_chunks<S, B, E>(
&self,
key: &str,
chunks: S,
options: UploadOptions,
) -> Result<ObjectAttrs, Error>
pub async fn upload_chunks<S, B, E>( &self, key: &str, chunks: S, options: UploadOptions, ) -> Result<ObjectAttrs, Error>
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?;Sourcepub async fn download(&self, key: &str) -> Result<Vec<u8>, Error>
pub async fn download(&self, key: &str) -> Result<Vec<u8>, Error>
Download the full contents of the object key.
Sourcepub async fn download_with(
&self,
key: &str,
options: DownloadOptions,
) -> Result<Vec<u8>, Error>
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?;Sourcepub async fn download_stream(
&self,
key: &str,
options: DownloadOptions,
) -> Result<Download, Error>
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.
Sourcepub async fn remove_with(
&self,
key: &str,
options: RemoveOptions,
) -> Result<(), Error>
pub async fn remove_with( &self, key: &str, options: RemoveOptions, ) -> Result<(), Error>
Remove an object, with explicit options (a specific version).
Sourcepub async fn signed_upload_url(
&self,
key: &str,
) -> Result<SignedUploadUrl, Error>
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.
Sourcepub async fn signed_upload_url_with(
&self,
key: &str,
options: SignedUrlOptions,
) -> Result<SignedUploadUrl, Error>
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?;Sourcepub async fn signed_download_url(
&self,
key: &str,
) -> Result<SignedDownloadUrl, Error>
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.
Sourcepub async fn signed_download_url_with(
&self,
key: &str,
options: SignedUrlOptions,
) -> Result<SignedDownloadUrl, Error>
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).
Auto Trait Implementations§
impl Freeze for Bucket
impl RefUnwindSafe for Bucket
impl Send for Bucket
impl Sync for Bucket
impl Unpin for Bucket
impl UnsafeUnpin for Bucket
impl UnwindSafe for Bucket
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request