Struct Operator

Source
pub struct Operator { /* private fields */ }
Expand description

Operator for uploading, downloading, and deleting files to a R2 bucket.

§Example

use cf_r2_sdk::builder::Builder;
use cf_r2_sdk::error::Error;
use dotenvy::dotenv;
use std::env;

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Error> {
   // load .env file
   dotenv().expect(".env file not found.");
   // insert a environment variable
   let bucket_name = env::var("BUCKET_NAME").expect("BUCKET_NAME not found in .env file.");
   let endpoint_url: String =
       env::var("ENDPOINT_URL").expect("ENDPOINT_URL not found in .env file.");
   let access_key_id: String =
       env::var("ACCESS_KEY_ID").expect("ACCESS_KEY_ID not found in .env file.");
   let secret_access_key: String =
      env::var("SECRET_ACCESS_KEY").expect("SECRET_ACCESS_KEY not found in .env file.");
   let region: String = env::var("REGION").expect("REGION not found in .env file.");

   let object: cf_r2_sdk::operator::Operator = Builder::new()
       .set_bucket_name(bucket_name)
       .set_access_key_id(access_key_id)
       .set_secret_access_key(secret_access_key)
       .set_endpoint(endpoint_url)
       .set_region(region)
       .create_client_result()?;

   let _ = object
       .upload_binary("sample.txt", "test/plain", b"Hello, World!", None)
       .await?;
   Ok(())
}

Implementations§

Source§

impl Operator

Source

pub fn new(bucket_name: String, client: Client) -> Self

Create a new Operator instance.

Source

pub async fn upload_file( &self, file_name: &str, mime_type: &str, file_path: &str, cache_control: Option<&str>, ) -> Result<(), OperationError>

Upload a file to the R2 bucket.

§Example
use cf_r2_sdk::builder::Builder;
use cf_r2_sdk::error::Error;
use dotenvy::dotenv;
use std::env;

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Error> {
   // load .env file
   dotenv().expect(".env file not found.");
   // insert a environment variable
   let bucket_name = env::var("BUCKET_NAME").expect("BUCKET_NAME not found in .env file.");
   let endpoint_url: String =
       env::var("ENDPOINT_URL").expect("ENDPOINT_URL not found in .env file.");
   let access_key_id: String =
       env::var("ACCESS_KEY_ID").expect("ACCESS_KEY_ID not found in .env file.");
   let secret_access_key: String =
      env::var("SECRET_ACCESS_KEY").expect("SECRET_ACCESS_KEY not found in .env file.");
   let region: String = env::var("REGION").expect("REGION not found in .env file.");

   let object: cf_r2_sdk::operator::Operator = Builder::new()
       .set_bucket_name(bucket_name)
       .set_access_key_id(access_key_id)
       .set_secret_access_key(secret_access_key)
       .set_endpoint(endpoint_url)
       .set_region(region)
       .create_client_result()?;

   // upload file
   object
       .upload_file("sample.jpg", "image/jpeg", "./data/sample.jpg", None)
       .await?;
  Ok(())
}
Source

pub async fn upload_binary( &self, file_name: &str, mime_type: &str, binary: &[u8], cache_control: Option<&str>, ) -> Result<(), OperationError>

Upload binary data to the R2 bucket.

§Example
use cf_r2_sdk::builder::Builder;
use cf_r2_sdk::error::Error;
use dotenvy::dotenv;
use std::env;

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Error> {
   // load .env file
   dotenv().expect(".env file not found.");
   // insert a environment variable
   let bucket_name = env::var("BUCKET_NAME").expect("BUCKET_NAME not found in .env file.");
   let endpoint_url: String =
       env::var("ENDPOINT_URL").expect("ENDPOINT_URL not found in .env file.");
   let access_key_id: String =
       env::var("ACCESS_KEY_ID").expect("ACCESS_KEY_ID not found in .env file.");
   let secret_access_key: String =
      env::var("SECRET_ACCESS_KEY").expect("SECRET_ACCESS_KEY not found in .env file.");
   let region: String = env::var("REGION").expect("REGION not found in .env file.");

   let object: cf_r2_sdk::operator::Operator = Builder::new()
       .set_bucket_name(bucket_name)
       .set_access_key_id(access_key_id)
       .set_secret_access_key(secret_access_key)
       .set_endpoint(endpoint_url)
       .set_region(region)
       .create_client_result()?;

   // upload binary data
   object
       .upload_binary("sample.txt", "test/plain", b"Hello, World!", None)
       .await?;

   Ok(())
}
Source

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

Download a file as binary data from the R2 bucket.

§Example
use cf_r2_sdk::builder::Builder;
use cf_r2_sdk::error::Error;
use dotenvy::dotenv;
use std::env;

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Error> {
   // load .env file
   dotenv().expect(".env file not found.");
   // insert a environment variable
   let bucket_name = env::var("BUCKET_NAME").expect("BUCKET_NAME not found in .env file.");
   let endpoint_url: String =
       env::var("ENDPOINT_URL").expect("ENDPOINT_URL not found in .env file.");
   let access_key_id: String =
       env::var("ACCESS_KEY_ID").expect("ACCESS_KEY_ID not found in .env file.");
   let secret_access_key: String =
      env::var("SECRET_ACCESS_KEY").expect("SECRET_ACCESS_KEY not found in .env file.");
   let region: String = env::var("REGION").expect("REGION not found in .env file.");

   let object: cf_r2_sdk::operator::Operator = Builder::new()
       .set_bucket_name(bucket_name)
       .set_access_key_id(access_key_id)
       .set_secret_access_key(secret_access_key)
       .set_endpoint(endpoint_url)
       .set_region(region)
       .create_client_result()?;

   object
       .upload_binary("sample.txt", "test/plain", b"Hello, World!", None)
       .await?;

   // download binary data
   object
       .download("sample.txt")
       .await?;
  Ok(())
}
Source

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

Delete a file from the R2 bucket.

§Example
use cf_r2_sdk::builder::Builder;
use cf_r2_sdk::error::Error;
use dotenvy::dotenv;
use std::env;

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Error> {
   // load .env file
   dotenv().expect(".env file not found.");
   // insert a environment variable
   let bucket_name = env::var("BUCKET_NAME").expect("BUCKET_NAME not found in .env file.");
   let endpoint_url: String =
       env::var("ENDPOINT_URL").expect("ENDPOINT_URL not found in .env file.");
   let access_key_id: String =
       env::var("ACCESS_KEY_ID").expect("ACCESS_KEY_ID not found in .env file.");
   let secret_access_key: String =
      env::var("SECRET_ACCESS_KEY").expect("SECRET_ACCESS_KEY not found in .env file.");
   let region: String = env::var("REGION").expect("REGION not found in .env file.");

   let object: cf_r2_sdk::operator::Operator = Builder::new()
       .set_bucket_name(bucket_name)
       .set_access_key_id(access_key_id)
       .set_secret_access_key(secret_access_key)
       .set_endpoint(endpoint_url)
       .set_region(region)
       .create_client_result()?;

   object
       .upload_binary("sample.txt", "test/plain", b"Hello, World!", None)
       .await?;

   // delete file
   let bin: Vec<u8> = object.download("sample.txt").await?;

   println!("{:?}", bin);
   Ok(())
}
Source

pub async fn list_objects(&self) -> Result<Vec<String>, OperationError>

Get file names vector from the R2 bucket.

§Example
use cf_r2_sdk::builder::Builder;
use cf_r2_sdk::error::Error;
use dotenvy::dotenv;
use std::env;

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Error> {
   // load .env file
   dotenv().expect(".env file not found.");
   // insert a environment variable
   let bucket_name = env::var("BUCKET_NAME").expect("BUCKET_NAME not found in .env file.");
   let endpoint_url: String =
       env::var("ENDPOINT_URL").expect("ENDPOINT_URL not found in .env file.");
   let access_key_id: String =
       env::var("ACCESS_KEY_ID").expect("ACCESS_KEY_ID not found in .env file.");
   let secret_access_key: String =
      env::var("SECRET_ACCESS_KEY").expect("SECRET_ACCESS_KEY not found in .env file.");
   let region: String = env::var("REGION").expect("REGION not found in .env file.");

   let object: cf_r2_sdk::operator::Operator = Builder::new()
       .set_bucket_name(bucket_name)
       .set_access_key_id(access_key_id)
       .set_secret_access_key(secret_access_key)
       .set_endpoint(endpoint_url)
       .set_region(region)
       .create_client_result()?;

   object
      .upload_binary("sample.txt", "test/plain", b"Hello, World!", None)
      .await?;

   // get file names vector
   let file_names: Vec<String> = object.list_objects().await?;

   for file_name in file_names {
      println!("{}", file_name);
   }

   Ok(())
}

Trait Implementations§

Source§

impl Clone for Operator

Source§

fn clone(&self) -> Operator

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Operator

Source§

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

Formats the value using the given formatter. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

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

Source§

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

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

impl<T> ErasedDestructor for T
where T: 'static,