pub struct Object { /* private fields */ }
Expand description

Handler for all object related operations.

Implementations

Creates a new Object with normalized path.

  • All path will be converted into relative path (without any leading /)
  • Path endswith / means it’s a dir path.
  • Otherwise, it’s a file path.

Create an empty object, like using the following linux commands:

  • touch path/to/file
  • mkdir path/to/dir/
Behavior
  • Create on existing dir will succeed.
  • Create on existing file will overwrite and truncate it.
Examples
Create an empty file
let o = op.object("path/to/file");
let _ = o.create().await?;
Create a dir
let o = op.object("path/to/dir/");
let _ = o.create().await?;

Read the whole object into a bytes.

This function will allocate a new bytes internally. For more precise memory control or reading data lazily, please use Object::reader

Examples
let bs = o.read().await?;

Read the specified range of object into a bytes.

This function will allocate a new bytes internally. For more precise memory control or reading data lazily, please use Object::range_reader

Examples
let bs = o.range_read(1024..2048).await?;

Create a new reader which can read the whole object.

Examples
let r = o.reader().await?;

Create a new reader which can read the specified range.

Examples
let r = o.range_reader(1024..2048).await?;

Create a reader which implements AsyncRead and AsyncSeek inside specified range.

Notes

It’s not a zero-cost operations. In order to support seeking, we have extra internal state which maintains the reader contents:

  • Seeking is pure in memory operation.
  • Every first read after seeking will start a new read operation on backend.

This operation is neither async nor returning result, because real IO happens while users call read or seek.

Examples
let r = o.seekable_reader(1024..2048);

Write bytes into object.

Notes
  • Write will make sure all bytes has been written, or an error will be returned.
Examples
use bytes::Bytes;

let o = op.object("path/to/file");
let _ = o.write(vec![0; 4096]).await?;

Create a new writer which can write data into the object.

Examples
use bytes::Bytes;

let op = Operator::new(memory::Backend::build().finish().await?);
let o = op.object("path/to/file");
let mut w = o.writer(4096).await?;
w.write(&[1; 4096]);
w.close();

Delete object.

Notes
  • Delete not existing error won’t return errors.
Examples
op.object("test").delete().await?;

List current dir object.

This function will create a new ObjectStreamer handle to list objects.

An error will be returned if object path doesn’t end with /.

Examples
let o = op.object("path/to/dir/");
let mut obs = o.list().await?;
// ObjectStream implements `futures::Stream`
while let Some(o) = obs.next().await {
    let mut o = o?;
    // It's highly possible that OpenDAL already did metadata during list.
    // Use `Object::metadata_cached()` to get cached metadata at first.
    let meta = o.metadata_cached().await?;
    match meta.mode() {
        ObjectMode::FILE => {
            println!("Handling file")
        }
        ObjectMode::DIR => {
            println!("Handling dir like start a new list via meta.path()")
        }
        ObjectMode::Unknown => continue,
    }
}

Get current object’s metadata.

Examples
use std::io::ErrorKind;
if let Err(e) = op.object("test").metadata().await {
    if e.kind() == ErrorKind::NotFound {
        println!("object not exist")
    }
}

Use local cached metadata if possible.

Example
use opendal::services::memory;
use anyhow::Result;
use futures::io;
use opendal::Operator;


#[tokio::main]
async fn main() -> Result<()> {
    let op = Operator::new(memory::Backend::build().finish().await?);
    let mut o = op.object("test");

    o.metadata_cached().await;
    // The second call to metadata_cached will have no cost.
    o.metadata_cached().await;

    Ok(())
}

Check if this object exist or not.

Example
use opendal::services::memory;
use anyhow::Result;
use futures::io;
use opendal::Operator;


#[tokio::main]
async fn main() -> Result<()> {
    let op = Operator::new(memory::Backend::build().finish().await?);
    let _ = op.object("test").is_exist().await?;

    Ok(())
}

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Applies the Compat adapter by value. Read more

Applies the Compat adapter by shared reference. Read more

Applies the Compat adapter by mutable reference. Read more

Returns the argument unchanged.

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

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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

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