Skip to main content

Bucket

Struct Bucket 

Source
pub struct Bucket { /* private fields */ }
Available on crate feature s3 only.
Expand description

The client of a bucket repository.

Bucket::new opens it from an S3Endpoint, and Bucket::from_fetcher opens it over a fetcher that the caller built. Bucket::counted gives a client whose fetcher adds byte counters. Each method takes a key or a list prefix relative to the prefix of the repository, and the client adds PREFIX/. A key in a result is relative to the prefix of the repository too.

§Keys

The client refuses a key that is not a key of a bucket repository, before it sends a request:

  • config, summary, summary.sig, extensions/ostrya/lock, and extensions/ostrya/lock-probe
  • objects/XX/<62 hex digits>.<ext>, in lowercase hex, with the extension commit, commitmeta, dirtree, dirmeta, filez, or tombstone-commit
  • refs/heads/<name>, with a ref name that is_ref_name accepts
  • refs/mirrors/<id>/<name>, with an id of at most 255 bytes that is_collection_id accepts, and a ref name that is_ref_name accepts
  • deltas/<2>/<41>/<target> and deltas/<2>/<41>-<43>/<target>, with checksums in modified base64, and the target superblock or a part number with no leading zero
  • delta-indexes/<2>/<41>.index.

A list prefix ends in /, so a list for s3://b/repo never gives the keys of s3://b/repo2. Its segments are not empty and are not . or ...

The request path of a key encodes each byte other than A-Z, a-z, 0-9, -, ., _, ~, and / as %XX. So a + of a delta key goes as %2B.

§Requests

The GET requests (get, get stream, the checks for an object, and the lists) are fetches. The fetcher repeats a fetch after a transport failure or a 408, 429, or 5xx, as Fetcher::fetch states, and fetches a failed body of a whole read again. A check for an object is a GET with Range: bytes=0-0, as exists states. The signature does not cover the Range header.

The client sends a PUT, a DELETE, and the POST that starts a multipart upload again after Error::UploadInterrupted and after a 408, 429, or 5xx, at most max_retries times. The delay starts at 250 ms, doubles each time, and stops at 2 s. put_if_absent and delete_if_match state their own rules, and put_stream states more rules for the POST requests of a multipart upload.

The client follows no redirect. The body of each listing, of each answer to a request of a multipart upload, and of each error response is at most 4 MiB.

§Errors of the server

An unsuccessful status gives Error::S3, with the code and the message of the S3 Error document in the body. A response whose x-amz-bucket-region header or whose Region element names a region other than the region of the location gives Error::S3Region. An absent key is not an error, whatever region its response names.

The body of an error response to a PUT or a DELETE that declares a coding, or a length over 4 MiB, is not read. The status then gives Error::S3 with no code, and the client does not send a definitive status again.

The Debug output shows the location and the mirror of the fetcher.

Implementations§

Source§

impl Bucket

Source

pub async fn new(endpoint: &S3Endpoint) -> Result<Bucket>

Opens the client of the bucket repository of endpoint.

The call builds the fetcher. It sends no request. S3Endpoint states the HTTP options that the client takes.

§Errors
Source

pub fn from_fetcher(endpoint: &S3Endpoint, fetcher: Fetcher) -> Bucket

Opens the client of the bucket repository of endpoint over fetcher.

The call builds no fetcher and sends no request. The client shares the connection pool, the admission limit, and the counters of fetcher. The client names each request by its URL under the bucket_url of the location, so it does not use the mirrors of fetcher. An example of fetcher is the fetcher of a pull, from the options that pull_options gives, whose mirror is the repository_url.

The client takes the location and the retry count of endpoint. fetcher must sign each request with the credentials and the region of endpoint, and follow no redirect, as the fetcher of Bucket::new does. The call cannot check this.

§Examples
use ostrya_fetch::Fetcher;
use ostrya_remote::s3::{Bucket, Environment, S3Endpoint, Settings};
let endpoint =
    S3Endpoint::resolve("s3://builds/exampleos", &Settings::default(), &Environment::Process)
        .await?;
let fetcher = Fetcher::new(endpoint.pull_options()?).await?;
let bucket = Bucket::from_fetcher(&endpoint, fetcher.clone());
// The fetcher reads the objects under the repository URL. The client
// reads `summary` with the S3 rules of an absent key and of an error.
let summary = bucket.get("summary", 1 << 20).await?;
Source

pub fn counted(&self, received: Vec<Arc<AtomicU64>>) -> Bucket

Returns a client of the same bucket repository whose fetcher adds counters of received body bytes.

The fetcher of the new client is Fetcher::counted of the fetcher of this client with received, so it has the counters of this client and the counters of received. Fetcher::counted states which bytes each counter gets. The new client has the location, the retry count, and the request URLs of this client. The counters of this client do not change.

Source

pub fn location(&self) -> &Location

Returns the location of the bucket repository.

Source

pub async fn get(&self, key: &str, max_size: u64) -> Result<Option<Object>>

Reads the object of key whole, or returns None if the key holds no object.

A 404 is an absent key if its body arrived whole and holds the code NoSuchKey, or holds no S3 code: an empty body, or a body that is not an S3 Error document. A 404 with another code, for example NoSuchBucket, is an error, so a missing bucket never reads as an empty repository. A 404 whose body did not arrive is an error too, with no code. An absent key is not an error, whatever region its response names. get_strict reads a key by a strict rule, and get_for_pull by the rule of a pull.

The bytes have the capacity of the object and one byte, if the response declares the length of the object.

§Errors
Source

pub async fn get_strict( &self, key: &str, max_size: u64, ) -> Result<Option<Object>>

Reads the object of key whole, or returns None if the key holds no object, by a strict rule of an absent key.

A 404 is an absent key only if its body arrived whole and holds the code NoSuchKey. Each other 404 is an error, also a 404 with an empty body or with a body that is not an S3 Error document, for example a page of a proxy. Use this call where a false absent key causes a loss of data. Else the call is get.

§Errors

The errors of get. A 404 that is not an absent key gives Error::S3 with the status 404, and with no code if the body holds no S3 code.

Source

pub async fn get_for_pull( &self, key: &str, max_size: u64, ) -> Result<Option<Object>>

Reads the object of key whole, or returns None if the key holds no object, by the rule of an absent key of a pull.

An answer that get reads as an absent key is an absent key here too. A 403 is also an absent key if its body arrived whole and holds the code AccessDenied. S3 answers so for an absent key to a caller that has no s3:ListBucket on the bucket, so a pull needs s3:GetObject alone. A 403 with another code, for example SignatureDoesNotMatch or InvalidAccessKeyId, is an error, so a wrong key fails at the first read. A key that holds an object that the credentials cannot read is also an absent key here.

A pull reads summary and summary.sig with this call. It reads config with get.

§Errors

The errors of get. A 403 that is not an absent key gives Error::S3 with the status 403, or Error::S3Region.

Source

pub async fn get_stream(&self, key: &str) -> Result<Option<ObjectStream>>

Opens the body of the object of key as a stream, or returns None if the key holds no object.

The stream holds no more than one chunk of the object in memory. get states the rule of an absent key.

§Errors

The errors of get, other than the size limit: also Error::Transport for a 304. A read of the stream fails with an I/O error if the body fails in transit.

Source

pub async fn get_stream_strict(&self, key: &str) -> Result<Option<ObjectStream>>

Opens the body of the object of key as a stream, or returns None if the key holds no object, by the strict rule of an absent key of get_strict.

The stream holds no more than one chunk of the object in memory.

§Errors

The errors of get_stream. A 404 that is not an absent key gives Error::S3 with the status 404, and with no code if the body holds no S3 code.

Source

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

Returns true if the key key holds an object.

The request is GET KEY with Range: bytes=0-0, and the client reads at most one byte of the object. A 206 and a 200 are an object. A 416 is an object too, because a server gives it for an object of zero bytes. A server that ignores the range answers 200 with the whole object. The client then refuses a declared length over one byte, and reads no byte of it. The fetcher reads a declared body of at most 64 KiB to its end, so the connection goes back to the pool. A larger body, or a body with no declared length, closes the connection.

The client reads a body that declares a length of at most one byte to its end, so the connection goes back to the pool. If the body fails after the status, the status decides, and the key holds an object. The client does not read a body with no declared length, and its connection closes. So a server that stalls such a body does not hold the check until the progress timeout.

get states the rule of an absent key. exists_strict reads a key by the strict rule.

§Errors
Source

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

Returns true if the key key holds an object, by the strict rule of an absent key of get_strict.

exists states the request and the rules of an object.

§Errors

The errors of exists. A 404 that is not an absent key gives Error::S3 with the status 404, and with no code if the body holds no S3 code.

Source

pub async fn put(&self, key: &str, body: Vec<u8>) -> Result<Option<String>>

Writes body to key, and returns the ETag of the response.

The request is idempotent, so the client sends it again by the rule of Bucket. A second request shares the bytes of the first one, so the client holds one copy of the body.

The client hashes a body of more than 1 MiB once, on the blocking pool of the runtime, before the first request. Each request signs that digest. The signer hashes a smaller body on the task of each request.

§Errors
  • Error::InvalidInput if key is not a key of a bucket repository, or if body is larger than 5 GiB, the limit of one S3 PUT.
  • Error::S3 and Error::S3Region for an unsuccessful response.
  • Error::Fetch for a failure of the fetcher, also after the last request that the client sent again.
Source

pub async fn put_stream<R>( &self, key: &str, body: R, part_size: u64, ) -> Result<Option<String>>
where R: AsyncRead + Unpin + Send,

Writes the bytes that body gives to key, and returns the ETag of the answer that wrote the object, as the server wrote it.

Servers give different ETag values for one object of several parts. Do not compare the result with the ETag of a later read of the key.

The call reads body in parts of part_size bytes. part_size gives the part size of an object of a known size. A body of at most one part goes in one PUT. A longer body goes in a multipart upload:

  1. POST KEY?uploads starts the upload and gives the upload id.
  2. PUT KEY?partNumber=N&uploadId=ID sends each part, one part after the other. The part numbers start at 1 and have no gap. Each part other than the last one holds part_size bytes.
  3. POST KEY?uploadId=ID with a CompleteMultipartUpload document completes the upload. The document gives each part number with the ETag of its answer, as the server wrote it.

The call holds one part in memory, with the ETag of each part that it sent and the document that completes the upload. The first part starts at 8 KiB and doubles up to part_size while the bytes arrive, so a small body takes a small buffer. After each part, the call takes the buffer back and reads the next part into it. If the fetcher still holds the bytes of a part, the next part takes a new buffer. After a full part, the call reads one byte more to find if the body goes on. The call hashes each part of more than 1 MiB once, on the blocking pool of the runtime, before the first request of the part. The part moves to the pool and back with no copy, and each request of the part signs that digest. The signer hashes a smaller part on the task of each request.

The client takes an upload id and an ETag of a part of at most 1 KiB each. So the ETag values of an upload and the document that completes it have a limit.

The client sends each part again by the rule of Bucket. A part has the same bytes in each request. The POST that starts the upload is sent again by the same rule, and also after a 2xx whose body did not arrive. The POST that completes the upload is sent again after an interrupted request, after a 408, 429, or 5xx, after a 2xx whose body cannot be read, and after a 2xx whose body is an S3 Error document. S3 can send such a document after the head of a 200.

After an interrupted request, a 408, 429, or 5xx, or an unusable 2xx to the POST that completes the upload, the server can have completed the upload. If a later request then gets a 404 with the code NoSuchUpload, the client reads the key as get_stream does. A Content-Length equal to the number of bytes of the body gives success with the ETag of the read, and the client reads no byte of the object. An absent object and an object of another length give an error.

This check compares the length alone. It is sound for a key whose name sets its content, such as a content-addressed object or a static delta. For a key whose content changes, an older object of the same length passes the check.

After each failure that comes after the start of the upload, the client sends one DELETE KEY?uploadId=ID to abort the upload, and ignores its outcome. Then it returns the error of the failure. If the last answer to a request of the upload is a 404 with the code NoSuchUpload, the server holds no such upload, and the client sends no abort.

A dropped future sends no abort, so the upload stays open, and it holds storage in the bucket. list_uploads gives each such upload, and abort_upload aborts it. The prune of a bucket repository of the ostrya crate aborts each such upload. To remove such uploads with no call of the client, set a lifecycle rule on the bucket that aborts incomplete multipart uploads.

§Errors
  • Error::InvalidInput if key is not a key of a bucket repository, or if part_size is less than 5 MiB or more than 5 GiB, the S3 limits of a part. The call then reads no byte of body.
  • Error::Io if a read of body fails.
  • Error::LimitExceeded if the body is longer than 10,000 parts of part_size bytes, the S3 limit of the parts of an upload. The message names the key and the part size.
  • Error::S3 and Error::S3Region for an unsuccessful response, and Error::S3 for an S3 Error document with a 2xx after the last request that the client sent again.
  • Error::Transport if the answer that starts the upload has no usable upload id or an upload id of more than 1 KiB, if its body did not arrive after the last request that the client sent again, if the answer to a part has no ETag or an ETag of more than 1 KiB, or if no answer that completes the upload can be read. Also if the key holds no object or an object of another length after a 404 NoSuchUpload to a completion whose outcome is not known. The message then states what the key holds.
  • The errors of get_stream for the read of the key after such a 404 NoSuchUpload.
  • Error::Fetch for a failure of the fetcher, also after the last request that the client sent again.
Source

pub async fn put_if_absent( &self, key: &str, body: Vec<u8>, ) -> Result<PutIfAbsent>

Writes body to key if the key holds no object.

The request carries If-None-Match: *. A 2xx gives PutIfAbsent::Created. A 412 to the first request gives PutIfAbsent::Present. A 409 with the code ConditionalRequestConflict is sent again, at most max_retries times.

After an interrupted request, the outcome is not known. The client then reads the key and compares the SHA-256 of what it holds with the SHA-256 of body:

After a 408, 429, or 5xx, the client sends the request again within the retry count. A 412 after such a status, or after an interrupted request, can answer a request of this call. So the client then reads the key and compares it in the same way: the same bytes give PutIfAbsent::Refused, and other bytes or an absent key give PutIfAbsent::Present.

The compare reads the object as a stream, in chunks of 64 KiB. A response that declares a length other than the length of body holds other bytes, and the client reads none of it. The client computes the SHA-256 of body on the blocking pool of the runtime: for a body of more than 1 MiB once before the first request, which each request signs, and for a smaller body at the first compare.

A server that does not honor If-None-Match writes the object over the one that the key holds, and gives PutIfAbsent::Created.

§Errors

The errors of put, and the errors of get for the read of the key.

Source

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

Deletes the object of key.

A 2xx and an absent key are success. get states the rule of an absent key. The client sends the request again by the rule of Bucket.

§Errors
Source

pub async fn delete_if_match( &self, key: &str, etag: &str, ) -> Result<DeleteIfMatch>

Deletes the object of key if its ETag is etag.

The request is DELETE KEY with If-Match: ETAG. etag goes as the server wrote it, for example the etag of a read. A 2xx gives DeleteIfMatch::Deleted. A 412 or an absent key to the first request gives DeleteIfMatch::NotMatched. get states the rule of an absent key. SeaweedFS 4.36 answers 412 for an absent key.

The client sends the request again by the rule of Bucket. A 412 or an absent key after a request with an unknown outcome can answer a request of this call. So the client then reads the key as exists does. An absent key gives DeleteIfMatch::Deleted, and an object gives DeleteIfMatch::NotMatched.

A server that does not honor If-Match deletes the object whatever its ETag is, and gives DeleteIfMatch::Deleted. rclone serve s3 1.75.1 is such a server.

§Errors
  • Error::InvalidInput if key is not a key of a bucket repository, or if etag is empty or longer than 1 KiB. The client then sends no request.
  • Error::S3 and Error::S3Region for an unsuccessful response.
  • Error::Fetch for a failure of the fetcher, also after the last request that the client sent again.
  • The errors of exists for the read of the key.
Source

pub async fn abort_upload(&self, key: &str, upload_id: &str) -> Result<()>

Aborts the multipart upload upload_id of key.

The request is DELETE KEY?uploadId=ID. A 2xx and a 404 with the code NoSuchUpload are success, so an upload that a server already removed is not an error. The client sends the request again by the rule of Bucket.

§Errors
Source

pub async fn list(&self, prefix: &str) -> Result<Listing>

Lists each object under prefix, over all pages.

prefix is relative to the prefix of the repository and ends in /, for example objects/ab/ or refs/heads/. The request is ListObjectsV2 with the encoding type url, and the client decodes each key.

§Errors
  • Error::InvalidInput if prefix does not end in /, or holds an empty segment, ., .., or a character outside the alphabet of a key.
  • Error::Transport if a page cannot be used: a body that the XML reader refuses, a truncated page with no continuation token, a key outside the prefix, or a page that does not move forward. Each key and common prefix of a page comes after each one of the earlier pages, in the byte order of UTF-8. A truncated page that holds no key and no common prefix gives a continuation token that no page gave after the last page with a key or a common prefix. More than 1000 truncated pages in a row with no key and no common prefix fail the list. So a server that gives the same page again, or empty pages with no end, fails the list.
  • Error::Transport if the server answers with 304.
  • Error::LimitExceeded if a page is larger than 4 MiB.
  • Error::S3 and Error::S3Region for an unsuccessful response, or for an S3 Error document with a 200.
  • Error::Fetch and Error::Io as for get.
Source

pub async fn list_delimited(&self, prefix: &str) -> Result<Listing>

Lists the objects and the common prefixes under prefix, with the delimiter /, over all pages.

Each key and each common prefix is relative to the prefix of the repository.

§Errors

The errors of list.

Source

pub async fn list_uploads(&self) -> Result<Vec<Upload>>

Lists the incomplete multipart uploads of the keys of the repository, over all pages.

The request is ListMultipartUploads with the prefix PREFIX/. With no prefix, the list covers the whole bucket, and the client keeps an upload only if its key is a key of a bucket repository. A 404 with the code NoSuchUpload is an empty list, because a server can answer a bucket with no upload in this way.

§Errors

The errors of list, with the markers of the next page in place of the continuation token. The rule of progress is different:

  • The NextKeyMarker of each truncated page does not come before the key marker of the page before it, in the byte order of UTF-8.
  • While the key marker stays the same, the NextUploadIdMarker of each truncated page is one that no earlier page gave for that key marker. S3 sorts the uploads of one key by the time of their start, and not by the upload id, so the upload id markers of one key have no order.

So a truncated page that holds no upload must move the markers.

Trait Implementations§

Source§

impl Debug for Bucket

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Bucket

§

impl !UnwindSafe for Bucket

§

impl Forget for Bucket

§

impl Freeze for Bucket
where *const ArcInner<Inner> is !null: Freeze, PhantomData<ArcInner<Inner>>: Freeze, *const ArcInner<[Arc<Atomic<u64>>]> is !null: Freeze, PhantomData<ArcInner<[Arc<Atomic<u64>>]>>: Freeze, *const u8 is !null: Freeze, PhantomData<u8>: Freeze,

§

impl Move for Bucket

§

impl Send for Bucket
where Arc<Inner>: Send, Arc<[Arc<Atomic<u64>>]>: Send, RawVecInner: Send,

§

impl Sync for Bucket
where Arc<Inner>: Sync, Arc<[Arc<Atomic<u64>>]>: Sync, RawVecInner: Sync,

§

impl Unpin for Bucket
where Arc<Inner>: Unpin, Arc<[Arc<Atomic<u64>>]>: Unpin, *const u8 is !null: Unpin,

§

impl UnsafeUnpin for Bucket
where *const ArcInner<Inner> is !null: UnsafeUnpin, PhantomData<ArcInner<Inner>>: UnsafeUnpin, *const ArcInner<[Arc<Atomic<u64>>]> is !null: UnsafeUnpin, PhantomData<ArcInner<[Arc<Atomic<u64>>]>>: UnsafeUnpin, *const u8 is !null: UnsafeUnpin, PhantomData<u8>: UnsafeUnpin,

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Same for T

Source§

type Output = T

Should always be Self
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<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