Struct s3reader::S3Reader

source ·
pub struct S3Reader { /* private fields */ }
Expand description

A Reader for S3 objects that implements the Read and Seek traits

This reader allows byte-offset acces to any S3 objects

Example

use std::io::{Read, Seek};
use s3reader::S3Reader;
use s3reader::S3ObjectUri;

let uri = S3ObjectUri::new("s3://my-bucket/path/to/huge/file").unwrap();
let mut reader = S3Reader::open(uri).unwrap();

reader.seek(std::io::SeekFrom::Start(100)).unwrap();

let mut buf: Vec<u8> = [0; 1024].to_vec();
reader.read(&mut buf).expect("Error reading from S3");

Implementations§

source§

impl S3Reader

source

pub fn from_uri(uri: &str) -> Result<S3Reader, S3ReaderError>

Creates a new S3Reader and checks for presence of the S3 object

This is the easiest method to open an S3Reader. Upon creation, it will check if the S3 object is actually present and available and will fetch the header. This prevents possible runtime errors later on.

source

pub fn new(uri: S3ObjectUri) -> S3Reader

Creates a new S3Reader.

This method does not check for presence of an actual object in S3 or for connectivity. Use S3Reader::open instead to ensure that the S3 object actually exists.

source

pub fn open(uri: S3ObjectUri) -> Result<S3Reader, S3ReaderError>

Creates a new S3Reader and checks for presence of the S3 object

This method is the preferred way to create a Reader. It has a minor overhead because it fetches the object’s header from S3, but this ensures that the object is actually available and thus prevents possible runtime errors.

source

pub fn from_config(config: &SdkConfig, uri: S3ObjectUri) -> S3Reader

Creates a new S3Reader with a custom AWS SdkConfig

This method is useful if you don’t want to use the default configbuilder using the environment. It does not check for correctness, connectivity to the S3 bucket or presence of the S3 object.

source

pub async fn read_range( &mut self, from: u64, to: u64 ) -> Result<AggregatedBytes, S3ReaderError>

Returns A Future for the bytes read from the S3 object for the specified byte-range

This method does not update the internal cursor position. To maintain an internal state, use S3Reader::seek and S3Reader::read instead.

The byte ranges from and to are both inclusive, see https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35

Example
use tokio::runtime::Runtime;

use s3reader::S3Reader;
use s3reader::S3ObjectUri;

let uri = S3ObjectUri::new("s3://my-bucket/path/to/huge/file").unwrap();
let mut reader = S3Reader::open(uri).unwrap();

// `read_range` is an async function, we must wrap it in a runtime in the doctest
let bytes = Runtime::new().unwrap().block_on(
    reader.read_range(100, 249)
).unwrap().into_bytes();
assert_eq!(bytes.len(), 150);
source

pub fn read_range_sync( &mut self, from: u64, to: u64 ) -> Result<AggregatedBytes, S3ReaderError>

Returns the bytes read from the S3 object for the specified byte-range

This method does not update the internal cursor position. To maintain an internal state, use S3Reader::seek and S3Reader::read instead.

The byte ranges from and to are both inclusive, see https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35

This method also exists as an async method: S3Reader::read_range

Example
use s3reader::S3Reader;
use s3reader::S3ObjectUri;

let uri = S3ObjectUri::new("s3://my-bucket/path/to/huge/file").unwrap();
let mut reader = S3Reader::open(uri).unwrap();

let bytes = reader.read_range_sync(100, 249).unwrap().into_bytes();
assert_eq!(bytes.len(), 150);
source

pub async fn fetch_header(&mut self) -> Result<(), SdkError<HeadObjectError>>

Fetches the object’s header from S3

Example
use tokio::runtime::Runtime;

use s3reader::S3Reader;
use s3reader::S3ObjectUri;

let uri = S3ObjectUri::new("s3://my-bucket/path/to/huge/file").unwrap();
let mut reader = S3Reader::open(uri).unwrap();

// `fetch_header` is an async function, we must wrap it in a runtime in the doctest
Runtime::new().unwrap().block_on(
    reader.fetch_header()
).unwrap();
assert_eq!(reader.len(), 150);
source

pub fn len(&mut self) -> u64

Returns the content_length of the S3 object

Panics

This method can panic if the header cannot be fetched (e.g. due to network issues, wrong URI etc). This can be prevented by using S3Reader::open which guarantees that the header is present.

source

pub fn pos(&self) -> u64

Trait Implementations§

source§

impl Read for S3Reader

source§

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>

Custom implementation to avoid too many read calls. The default trait reads in 32 bytes blocks that grow over time. However, the IO for S3 has way more latency so S3Reader tries to fetch all data in a single call

source§

fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>

Custom implementation to avoid too many read calls. The default trait reads in 32 bytes blocks that grow over time. However, the IO for S3 has way more latency so S3Reader tries to fetch all data in a single call

source§

fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error>

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
1.36.0 · source§

fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>

Like read, except that it reads into a slice of buffers. Read more
source§

fn is_read_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
1.6.0 · source§

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>

Read the exact number of bytes required to fill buf. Read more
source§

fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
source§

fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Read the exact number of bytes required to fill cursor. Read more
1.0.0 · source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adaptor for this instance of Read. Read more
1.0.0 · source§

fn bytes(self) -> Bytes<Self>
where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 · source§

fn chain<R>(self, next: R) -> Chain<Self, R>
where R: Read, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
1.0.0 · source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more
source§

impl Seek for S3Reader

source§

fn seek(&mut self, pos: SeekFrom) -> Result<u64, Error>

Seek to an offset, in bytes, in a stream. Read more
1.55.0 · source§

fn rewind(&mut self) -> Result<(), Error>

Rewind to the beginning of a stream. Read more
source§

fn stream_len(&mut self) -> Result<u64, Error>

🔬This is a nightly-only experimental API. (seek_stream_len)
Returns the length of this stream (in bytes). Read more
1.51.0 · source§

fn stream_position(&mut self) -> Result<u64, Error>

Returns the current seek position from the start of the stream. Read more
source§

fn seek_relative(&mut self, offset: i64) -> Result<(), Error>

🔬This is a nightly-only experimental API. (seek_seek_relative)
Seeks relative to the current position. Read more

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

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

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

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

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.

§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

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

§

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>,

§

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

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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