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
an operation’s version option can name.
Part of the declaration, like Bucket::public.
Sourcepub const fn storage_class(self, class: StorageClass) -> Self
pub const fn storage_class(self, class: StorageClass) -> Self
The storage class an upload gets when it names none, and so how the
provider prices the bucket’s objects. StorageClass::Auto lets the
provider move each object by how often it is read; it is a bucket’s
class only, and allows no lifecycle moves. Standard if not given.
Part of the declaration, like Bucket::public.
Sourcepub const fn lifecycle(self, rules: &'static [LifecycleRule]) -> Self
pub const fn lifecycle(self, rules: &'static [LifecycleRule]) -> Self
Rules the provider applies to the bucket’s objects: moving them to a colder class, or deleting them, some days after they were written. A rule applies to the objects already in the bucket too. At most 100.
Part of the declaration, like Bucket::public.
use encore::objects::{Bucket, LifecycleRule};
static UPLOADS: Bucket = Bucket::new("uploads").versioned().lifecycle(&[
LifecycleRule::new().prefix("tmp/").delete_after_days(1),
LifecycleRule::new().delete_noncurrent_after_days(30).keep_noncurrent_versions(5),
]);Sourcepub const fn retention(self, retention: Retention) -> Self
pub const fn retention(self, retention: Retention) -> Self
Keeps every object at least this long after it was written: until
then it can’t be deleted or overwritten, which fails with
Error::Retained. On AWS such a delete or overwrite succeeds, but
the version written is kept underneath until its retention ends.
Part of the declaration, like Bucket::public.
use encore::objects::{Bucket, Retention};
static INVOICES: Bucket = Bucket::new("invoices").retention(Retention::days(2555));Sourcepub const fn object_retention(self) -> Self
pub const fn object_retention(self) -> Self
Lets objects carry their own retain-until time: set on upload with
UploadRequest::retain_until, and later with
Bucket::extend_retention. Not supported on Cloudflare R2.
Part of the declaration, like Bucket::public.
Sourcepub const fn tag(self, _key: &'static str, _value: &'static str) -> Self
pub const fn tag(self, _key: &'static str, _value: &'static str) -> Self
Tags the bucket with key and value: key/value classification, such
as .tag("data", "pci"), for policies to read. One call per tag, each
with its own key. Encore reads them from the source, so both are string
literals. A key is lowercase letters, digits, _ and -, starting with
a letter and not with encore, which is kept for Encore’s own tags; a
value is the same characters, or empty. Each is at most 63 characters,
and a bucket has at most 32 tags.
Part of the declaration: it takes the bucket by value, so it cannot be
called on the static.
use encore::objects::Bucket;
static RECEIPTS: Bucket = Bucket::new("receipts").tag("data", "pci");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 fn list(&self) -> ListRequest<'_>
pub fn list(&self) -> ListRequest<'_>
List the objects in the bucket, as an iterator of entries: all of them, or those the request’s options select.
With a delimiter, names are grouped by the
part after the prefix up to the delimiter, as a directory listing does:
each group is one entry with ListEntry::is_prefix set, whose name
ends in the delimiter. Groups and objects are listed in order, and the
limit counts both.
let mut entries = UPLOADS.list().prefix("users/42/").delimiter("/").await?;
while let Some(entry) = entries.next().await {
let entry = entry?;
if entry.is_prefix {
println!("{} (folder)", entry.name); // users/42/photos/
} else {
println!("{}", entry.name);
}
}Sourcepub fn list_versions(&self) -> ListVersionsRequest<'_>
pub fn list_versions(&self) -> ListVersionsRequest<'_>
List every stored version of the bucket’s objects: by name, then
newest first. A removed object has no
latest version. An unversioned bucket lists
each object’s one version.
let mut versions = UPLOADS.list_versions().prefix("invoices/").await?;
while let Some(v) = versions.next().await {
let v = v?;
println!("{} {:?} latest={}", v.name, v.version, v.latest);
}Sourcepub fn exists(&self, key: &str) -> ExistsRequest<'_>
pub fn exists(&self, key: &str) -> ExistsRequest<'_>
Whether an object with key exists.
Sourcepub fn attrs(&self, key: &str) -> AttrsRequest<'_>
pub fn attrs(&self, key: &str) -> AttrsRequest<'_>
The attributes of the object key: its size, content type, version,
etag, metadata, …
Sourcepub fn upload(
&self,
key: &str,
data: impl Into<UploadBody>,
) -> UploadRequest<'_>
pub fn upload( &self, key: &str, data: impl Into<UploadBody>, ) -> UploadRequest<'_>
Upload data to the object key, returning its stored attributes.
data is bytes, or UploadBody::reader or UploadBody::chunks
to stream them.
UPLOADS
.upload("a.pdf", data)
.content_type("application/pdf")
.cache_control("public, max-age=3600")
.metadata([("owner", "42")])
.storage_class(StorageClass::Cold)
.await?;
// Streamed, and only if there is no such object yet.
UPLOADS
.upload("b.pdf", UploadBody::reader(file))
.not_exists()
.await?;Sourcepub fn upload_if_match(
&self,
key: &str,
data: impl Into<UploadBody>,
revision: Revision,
) -> UploadIfMatchRequest<'_>
pub fn upload_if_match( &self, key: &str, data: impl Into<UploadBody>, revision: Revision, ) -> UploadIfMatchRequest<'_>
Upload data to the object key only if it is still the revision
revision names, from ObjectAttrs::revision or, for what was read
of it, Download::revision: if another write
came first, or the object is gone, it is
Error::PreconditionFailed. Takes the options of
upload but not_exists.
The match compares the object’s contents, not its attributes:
update_attrs doesn’t break it. On AWS it needs
read access to the bucket too, which Encore grants for it.
let seen = UPLOADS.attrs("state.json").await?;
UPLOADS
.upload_if_match("state.json", next, seen.revision())
.content_type("application/json")
.await?;Sourcepub fn update_attrs(&self, key: &str) -> UpdateAttrsRequest<'_>
pub fn update_attrs(&self, key: &str) -> UpdateAttrsRequest<'_>
Change the attributes of the object key: its content type, cache
control, content disposition or metadata, each a method of the
request. One not set stays as it is. Returns the object’s attributes
after.
On S3 this writes the object anew, so in a versioned bucket it makes a new version.
let attrs = UPLOADS.attrs("a.pdf").await?;
UPLOADS
.update_attrs("a.pdf")
.cache_control("no-store")
.if_match(attrs.revision())
.await?;Sourcepub fn extend_retention(
&self,
key: &str,
until: SystemTime,
) -> ExtendRetentionRequest<'_>
pub fn extend_retention( &self, key: &str, until: SystemTime, ) -> ExtendRetentionRequest<'_>
Keep the object key until until at least, in a bucket declared
with Bucket::object_retention. Retention can only be extended: a
time earlier than the object’s is Error::InvalidArgument. Returns
the object’s attributes after.
Sourcepub fn download(&self, key: &str) -> DownloadRequest<'_>
pub fn download(&self, key: &str) -> DownloadRequest<'_>
Download the object key, or a version of it, or a byte range.
Awaited, it is a Download as soon as the provider has answered:
an object that isn’t there is Error::NotFound here, and what the
provider says of the object is known, before any of its contents.
They are read from the Download, whole or as they arrive.
let whole = UPLOADS.download("a.pdf").await?.bytes().await?;
// The 256 bytes starting at offset 1024.
let part = UPLOADS.download("big.bin").range(1024..1280).await?;
let (size, bytes) = (part.size(), part.bytes().await?);
// The last 64 bytes, however large the object is.
let tail = UPLOADS.download("big.bin").last(64).await?.bytes().await?;Sourcepub fn remove(&self, key: &str) -> RemoveRequest<'_>
pub fn remove(&self, key: &str) -> RemoveRequest<'_>
Remove the object key, or a version of it.
Sourcepub async fn remove_if_match(
&self,
key: &str,
revision: Revision,
) -> Result<(), Error>
pub async fn remove_if_match( &self, key: &str, revision: Revision, ) -> Result<(), Error>
Remove the object key only if it is still the revision revision
names, from ObjectAttrs::revision or Download::revision: if
another write came first,
or it is gone already, it is Error::PreconditionFailed.
The match compares the object’s contents, not its attributes. On AWS
it needs read access to the bucket too, which Encore grants for it.
Cloudflare R2 can’t remove conditionally: there it is
Error::InvalidArgument.
let attrs = UPLOADS.attrs("lock.json").await?;
UPLOADS.remove_if_match("lock.json", attrs.revision()).await?;Sourcepub fn signed_upload_url(&self, key: &str) -> SignedUploadUrlRequest<'_>
pub fn signed_upload_url(&self, key: &str) -> SignedUploadUrlRequest<'_>
A URL through which anyone holding it can upload the object key:
valid for an hour, or the request’s ttl.
Sourcepub fn signed_download_url(&self, key: &str) -> SignedDownloadUrlRequest<'_>
pub fn signed_download_url(&self, key: &str) -> SignedDownloadUrlRequest<'_>
A URL through which anyone holding it can download the object key:
valid for an hour, or the request’s
ttl.
Sourcepub fn public_url(&self, key: &str) -> Result<String, Error>
pub fn public_url(&self, key: &str) -> Result<String, Error>
The public URL of the object key. Only a bucket declared public
has one: for another it is Error::InvalidArgument.
Auto Trait Implementations§
impl Forget for Bucket
impl Freeze for Bucket
impl Move 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