pub struct ObjectClient<'a>(/* private fields */);
Expand description

Operations on Objects.

Implementations§

source§

impl<'a> ObjectClient<'a>

source

pub async fn create( &self, bucket: &str, file: Vec<u8>, filename: &str, mime_type: &str ) -> Result<Object>

Create a new object. Upload a file as that is loaded in memory to google cloud storage, where it will be interpreted according to the mime type you specified.

Example
use cloud_storage::Client;
use cloud_storage::Object;

let file: Vec<u8> = read_cute_cat("cat.png");
let client = Client::default();
client.object().create("cat-photos", file, "recently read cat.png", "image/png").await?;
source

pub async fn create_streamed<S>( &self, bucket: &str, stream: S, length: impl Into<Option<u64>>, filename: &str, mime_type: &str ) -> Result<Object>
where S: TryStream + Send + Sync + 'static, S::Error: Into<Box<dyn Error + Send + Sync>>, Bytes: From<S::Ok>,

Create a new object. This works in the same way as ObjectClient::create, except it does not need to load the entire file in ram.

Example
use cloud_storage::Client;
use cloud_storage::Object;

let client = Client::default();
let file = reqwest::Client::new()
    .get("https://my_domain.rs/nice_cat_photo.png")
    .send()
    .await?
    .bytes_stream();
client.object().create_streamed("cat-photos", file, 10, "recently read cat.png", "image/png").await?;
source

pub async fn list( &self, bucket: &'a str, list_request: ListRequest ) -> Result<impl Stream<Item = Result<ObjectList>> + 'a>

Obtain a list of objects within this Bucket.

Example
use cloud_storage::Client;
use cloud_storage::{Object, ListRequest};

let client = Client::default();
let all_objects = client.object().list("my_bucket", ListRequest::default()).await?;
source

pub async fn read(&self, bucket: &str, file_name: &str) -> Result<Object>

Obtains a single object with the specified name in the specified bucket.

Example
use cloud_storage::Client;
use cloud_storage::Object;

let client = Client::default();
let object = client.object().read("my_bucket", "path/to/my/file.png").await?;
source

pub async fn download(&self, bucket: &str, file_name: &str) -> Result<Vec<u8>>

Download the content of the object with the specified name in the specified bucket.

Example
use cloud_storage::Client;
use cloud_storage::Object;

let client = Client::default();
let bytes = client.object().download("my_bucket", "path/to/my/file.png").await?;
source

pub async fn download_streamed( &self, bucket: &str, file_name: &str ) -> Result<impl Stream<Item = Result<u8>> + Unpin>

Download the content of the object with the specified name in the specified bucket, without allocating the whole file into a vector.

Example
use cloud_storage::Client;
use cloud_storage::Object;
use futures_util::stream::StreamExt;
use tokio::fs::File;
use tokio::io::{AsyncWriteExt, BufWriter};

let client = Client::default();
let mut stream = client.object().download_streamed("my_bucket", "path/to/my/file.png").await?;
let mut file = BufWriter::new(File::create("file.png").await.unwrap());
while let Some(byte) = stream.next().await {
    file.write_all(&[byte.unwrap()]).await.unwrap();
}
file.flush().await?;
source

pub async fn update(&self, object: &Object) -> Result<Object>

Updates a single object with the specified name in the specified bucket with the new information in object.

Note that if the name or bucket fields are changed, the object will not be found. See [rewrite] or [copy] for similar operations.

Example
use cloud_storage::Client;
use cloud_storage::Object;

let client = Client::default();
let mut object = client.object().read("my_bucket", "path/to/my/file.png").await?;
object.content_type = Some("application/xml".to_string());
client.object().update(&object).await?;
source

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

Deletes a single object with the specified name in the specified bucket.

Example
use cloud_storage::Client;
use cloud_storage::Object;

let client = Client::default();
client.object().delete("my_bucket", "path/to/my/file.png").await?;
source

pub async fn compose( &self, bucket: &str, req: &ComposeRequest, destination_object: &str ) -> Result<Object>

Concatenates the contents of multiple objects into one.

Example
use cloud_storage::Client;
use cloud_storage::object::{Object, ComposeRequest, SourceObject};

let client = Client::default();
let obj1 = client.object().read("my_bucket", "file1").await?;
let obj2 = client.object().read("my_bucket", "file2").await?;
let compose_request = ComposeRequest {
    kind: "storage#composeRequest".to_string(),
    source_objects: vec![
        SourceObject {
            name: obj1.name.clone(),
            generation: None,
            object_preconditions: None,
        },
        SourceObject {
            name: obj2.name.clone(),
            generation: None,
            object_preconditions: None,
        },
    ],
    destination: None,
};
let obj3 = client.object().compose("my_bucket", &compose_request, "test-concatted-file").await?;
// obj3 is now a file with the content of obj1 and obj2 concatted together.
source

pub async fn copy( &self, object: &Object, destination_bucket: &str, path: &str ) -> Result<Object>

Copy this object to the target bucket and path.

Example
use cloud_storage::Client;
use cloud_storage::object::{Object, ComposeRequest};

let client = Client::default();
let obj1 = client.object().read("my_bucket", "file1").await?;
let obj2 = client.object().copy(&obj1, "my_other_bucket", "file2").await?;
// obj2 is now a copy of obj1.
source

pub async fn rewrite( &self, object: &Object, destination_bucket: &str, path: &str ) -> Result<Object>

Moves a file from the current location to the target bucket and path.

Limitations

This function does not yet support rewriting objects to another

  • Geographical Location,
  • Encryption,
  • Storage class. These limitations mean that for now, the rewrite and the copy methods do the same thing.
Example
use cloud_storage::Client;
use cloud_storage::object::Object;

let client = Client::default();
let obj1 = client.object().read("my_bucket", "file1").await?;
let obj2 = client.object().rewrite(&obj1, "my_other_bucket", "file2").await?;
// obj2 is now a copy of obj1.

Trait Implementations§

source§

impl<'a> Debug for ObjectClient<'a>

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for ObjectClient<'a>

§

impl<'a> Send for ObjectClient<'a>

§

impl<'a> Sync for ObjectClient<'a>

§

impl<'a> Unpin for ObjectClient<'a>

§

impl<'a> !UnwindSafe for ObjectClient<'a>

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