Struct opendal::Object

source ·
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.

ID of object.

ID is the unique id of object in the underlying backend. In different backend, the id could have different meaning.

For example:

  • In fs: id is the absolute path of file, like /path/to/dir/test_object.
  • In s3: id is the full object key, like path/to/dir/test_object
Example
use anyhow::Result;
use futures::io;
use opendal::services::memory;
use opendal::Operator;
use opendal::Scheme;

#[tokio::main]
async fn main() -> Result<()> {
    let op = Operator::from_env(Scheme::Memory)?;
    let id = op.object("test").id();

    Ok(())
}

Path of object. Path is relative to operator’s root. Only valid in current operator.

The value is the same with Metadata::path().

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

#[tokio::main]
async fn main() -> Result<()> {
    let op = Operator::from_env(Scheme::Memory)?;
    let path = op.object("test").path();

    Ok(())
}

Name of object. Name is the last segment of path.

If this object is a dir, Name MUST endswith / Otherwise, Name MUST NOT endswith /.

The value is the same with Metadata::name().

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

#[tokio::main]
async fn main() -> Result<()> {
    let op = Operator::from_env(Scheme::Memory)?;
    let name = op.object("test").name();

    Ok(())
}

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?;

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.blocking_create()?;
Create a dir
let o = op.object("path/to/dir/");
let _ = o.blocking_create()?;

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 o = op.object("path/to/file");
let bs = o.read().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::blocking_reader

Examples
let o = op.object("path/to/file");
let bs = o.blocking_read()?;

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

Notes
  • The returning contnet’s length may be smaller than the range specifed.
Examples
let o = op.object("path/to/file");
let bs = o.range_read(1024..2048).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::blocking_range_reader

Examples
let o = op.object("path/to/file");
let bs = o.blocking_range_read(1024..2048)?;

Create a new reader which can read the whole object.

Examples
let o = op.object("path/to/file");
let r = o.reader().await?;

Create a new reader which can read the whole object.

Examples
let o = op.object("path/to/file");
let r = o.blocking_reader()?;

Create a new reader which can read the specified range.

Notes
  • The returning contnet’s length may be smaller than the range specifed.
Examples
let o = op.object("path/to/file");
let r = o.range_reader(1024..2048).await?;

Create a new reader which can read the specified range.

Examples
let o = op.object("path/to/file");
let r = o.blocking_range_reader(1024..2048)?;

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);

Read the whole object into a bytes with auto detected compress algorithm.

If we can’t find the correct algorithm, we return Ok(None) instead.

Feature

This function needs to enable feature compress.

Examples
let o = op.object("path/to/file.gz");
let bs = o.decompress_read().await?.expect("must read succeed");

Create a reader with auto-detected compress algorithm.

If we can’t find the correct algorithm, we will return Ok(None).

Feature

This function needs to enable feature compress.

Examples
let o = op.object("path/to/file.gz");
let r = o.decompress_reader().await?;

Read the whole object into a bytes with specific compress algorithm.

Feature

This function needs to enable feature compress.

Examples
let o = op.object("path/to/file.gz");
let bs = o.decompress_read_with(CompressAlgorithm::Gzip).await?;

Create a reader with specific compress algorithm.

Feature

This function needs to enable feature compress.

Examples
let o = op.object("path/to/file.gz");
let r = o.decompress_reader_with(CompressAlgorithm::Gzip).await?;

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?;

Write data with option described in OpenDAL rfc-0661

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

let op = Operator::from_env(Scheme::S3)?;
let o = op.object("path/to/file");
let bs = b"hello, world!".to_vec();
let args = OpWrite::new(bs.len() as u64).with_content_type("text/plain");
let _ = o.write_with(args, bs).await?;

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.blocking_write(vec![0; 4096])?;

Write data with option described in OpenDAL rfc-0661

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

let o = op.object("hello.txt");
let bs = b"hello, world!".to_vec();
let ow = OpWrite::new(bs.len() as u64).with_content_type("text/plain");
let _ = o.blocking_write_with(ow, bs)?;

Write data into object from a BytesRead.

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

let o = op.object("path/to/file");
let r = Cursor::new(vec![0; 4096]);
let _ = o.write_from(4096, r).await?;

Write data into object from a BlockingBytesRead.

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

use bytes::Bytes;

let o = op.object("path/to/file");
let r = Cursor::new(vec![0; 4096]);
let _ = o.blocking_write_from(4096, r)?;

Delete object.

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

Delete object.

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

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 op = Operator::from_env(Scheme::Memory)?;
let o = op.object("path/to/dir/");
let mut ds = o.list().await?;
// ObjectStreamer implements `futures::Stream`
while let Some(de) = ds.try_next().await? {
    match de.mode() {
        ObjectMode::FILE => {
            println!("Handling file")
        }
        ObjectMode::DIR => {
            println!("Handling dir like start a new list via meta.path()")
        }
        ObjectMode::Unknown => continue,
    }
}

List current dir object.

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

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

Examples
use anyhow::anyhow;
let op = Operator::from_env(Scheme::Memory)?;
let o = op.object("path/to/dir/");
let mut ds = o.blocking_list()?;
while let Some(de) = ds.next() {
    let de = de?;
    match de.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")
    }
}

Get current object’s metadata.

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

Check if this object exists or not.

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

#[tokio::main]
async fn main() -> Result<()> {
    let op = Operator::from_env(Scheme::Memory)?;
    let _ = op.object("test").is_exist().await?;

    Ok(())
}

Check if this object exists or not.

Example
use anyhow::Result;
use opendal::services::memory;
use opendal::Operator;
use opendal::Scheme;
fn main() -> Result<()> {
    let op = Operator::from_env(Scheme::Memory)?;
    let _ = op.object("test").blocking_is_exist()?;

    Ok(())
}

Presign an operation for read.

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

#[tokio::main]
async fn main() -> Result<()> {
    let signed_req = op.object("test").presign_read(Duration::hours(1))?;
    let req = http::Request::builder()
        .method(signed_req.method())
        .uri(signed_req.uri())
        .body(())?;

Presign an operation for write.

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

#[tokio::main]
async fn main() -> Result<()> {
    let signed_req = op.object("test").presign_write(Duration::hours(1))?;
    let req = http::Request::builder()
        .method(signed_req.method())
        .uri(signed_req.uri())
        .body(())?;

Construct a multipart with existing upload id.

Create a new multipart for current path.

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

ObjectEntry can convert into object without overhead.

Converts to this type from the input type.

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.

The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
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