Struct StorageClient

Source
pub struct StorageClient {
    pub client: Client,
    pub project_url: String,
    pub api_key: String,
}
Expand description

Supabase Storage Client

Fields§

§client: Client§project_url: String

REST endpoint for querying and managing your database Example: https://.supabase.co

§api_key: String

WARN: The service role key has the ability to bypass Row Level Security. Never share it publicly.

Implementations§

Source§

impl StorageClient

Source

pub fn new(project_url: String, api_key: String) -> Self

Create a new StorageClient from a project_url and api_key You can find your project url and keys at https://supabase.com/dashboard/project/YOUR_PROJECT_ID/settings/api

§Example
let client = StorageClient::new(project_url, api_key, jwt_secret).unwrap();
Source

pub async fn new_from_env() -> Result<StorageClient, Error>

Create a new StorageClient from the “SUPABASE_URL” and “SUPABASE_API_KEY” environment variables.

Create a new StorageClient from a project_url and api_key You can find your project url and keys at https://supabase.com/dashboard/project/YOUR_PROJECT_ID/settings/api

§Example
let client = StorageClient::new_from_env().unwrap();
Source

pub async fn create_bucket<'a>( &self, name: &str, id: Option<&str>, public: bool, allowed_mime_types: Option<Vec<MimeType<'a>>>, file_size_limit: Option<u64>, ) -> Result<String, Error>

Create a new storage bucket, returning the name (not the id) of the bucket on success.

Requires your StorageClient to have the following RLS permissions: buckets table permissions: insert

WARNING: Do not use underscores in bucket names or ids

§Example
 let name = client
     .create_bucket("a-cool-name-for-a-bucket", None, false, None, None)
     .await
     .unwrap();
Source

pub async fn delete_bucket(&self, id: &str) -> Result<(), Error>

Delete the bucket with the given id

§Example
client.delete_bucket("a-cool-name-for-a-bucket").await.unwrap();
Source

pub async fn get_bucket(&self, bucket_id: &str) -> Result<Bucket, Error>

Get the bucket with the given id

§Example
let bucket = client
    .get_bucket("a-cool-name-for-a-bucket-with-options")
    .await
    .unwrap();
Source

pub async fn list_buckets(&self) -> Result<Buckets, Error>

Retrieves the details of all Storage buckets within an existing project

§Example
let buckets = client.list_buckets().await.unwrap();
Source

pub async fn update_bucket<'a>( &self, id: &str, public: bool, allowed_mime_types: Option<Vec<MimeType<'a>>>, file_size_limit: Option<u64>, ) -> Result<String, Error>

Updates a Storage bucket

Requires the following RLS permissions: buckets table: select and update

§Example
client.update_bucket("bucket_id", true, None, Some(100_000_000)).await.unwrap();
Source

pub async fn empty_bucket(&self, id: &str) -> Result<String, Error>

Empty a bucket with a given id

§Example
let empty = client.empty_bucket("empty_bucket_test").await.unwrap();
Source

pub async fn replace_file( &self, bucket_id: &str, data: Vec<u8>, path: &str, options: Option<FileOptions<'_>>, ) -> Result<ObjectResponse, Error>

Replaces the file at the designated bucket and path with the given Vec<u8>

§Example
let object = client.replace_file("bucket_id", file, "path/to/file.txt", Some(options)).await.unwrap();
Source

pub async fn update_file( &self, bucket_id: &str, data: Vec<u8>, path: &str, options: Option<FileOptions<'_>>, ) -> Result<ObjectResponse, Error>

Updates the file at the designated bucket and path with the given Vec<u8>

This is identical to replace_file

§Example
let object = client.update_file("bucket_id", file, "path/to/file.txt", Some(options)).await.unwrap();
Source

pub async fn upload_file( &self, bucket_id: &str, data: Vec<u8>, path: &str, options: Option<FileOptions<'_>>, ) -> Result<ObjectResponse, Error>

Uploads a file at the designated bucket and path with the given Vec<u8>

§Example
let object = client.upload_file("bucket_id", file, "path/to/file.txt", Some(options)).await.unwrap();
Source

pub async fn download_file( &self, bucket_id: &str, path: &str, options: Option<DownloadOptions<'_>>, ) -> Result<Vec<u8>, Error>

Download the designated file

§Example
let file = client.download_file("bucket_id", file, "path/to/file.txt", Some(options)).await.unwrap();
Source

pub async fn delete_file( &self, bucket_id: &str, path: &str, ) -> Result<BucketResponse, Error>

Delete the designated file, returning a confirmation message on success

 let message = client
     .delete_file("upload_tests", "tests/signed_upload")
     .await
     .unwrap();
Source

pub async fn list_files( &self, bucket_id: &str, path: Option<&str>, options: Option<FileSearchOptions<'_>>, ) -> Result<Vec<FileObject>, Error>

List all files that match your search criteria

The returned Vec<FileObject> will contain both files and folders. Folders can be identified as having a populated name field, without any other fields.

§Example

let options = FileSearchOptions {
    limit: Some(5), // List up to five files
    offset: Some(1), // Skip the first file
    sort_by: Some(SortBy {
        column: Column::Name, // Sort by name
        order: Order::Asc, // In Ascending order
    }),
    search: None, // With no specific search string
};

client
    .list_files("bucket_id", "folder/", Some(options))
    .await
    .unwrap();
Source

pub async fn copy_file( &self, from_bucket: &str, to_bucket: Option<&str>, from_path: &str, to_path: Option<&str>, copy_metadata: bool, ) -> Result<String, Error>

Copy a file from one path to another

§Example
// Copies `3.txt` into `folder/4.txt` within the same bucket, including metadata
let key = client
    .copy_file("from_bucket", None, "3.txt", Some("folder/4.txt"), true)
    .await
    .unwrap();

// Copies `a.txt` into `folder/b.txt` in a different bucket, including metadata
let key = client
    .copy_file("from_bucket", "to_bucket", "a.txt", Some("folder/b.txt"), true)
    .await
    .unwrap();
Source

pub async fn create_signed_url( &self, bucket_id: &str, path: &str, expires_in: u64, ) -> Result<String, Error>

Create a signed download url, returns a signed_url on success

§Example

client
   .create_signed_url("list_files", "3.txt", 12431234)
   .await
   .unwrap();
Source

pub async fn create_multiple_signed_urls( &self, bucket_id: &str, paths: Vec<&str>, expires_in: u64, ) -> Result<Vec<String>, Error>

Create multiple signed download urls, returns a Vec of signed_urls on success

§Example

let urls = client
   .create_multiple_signed_urls("bucket_id", vec!["1.txt", "2.txt", "3.txt"], 100_000)
   .await
   .unwrap();
Source

pub async fn create_signed_upload_url( &self, bucket_id: &str, path: &str, ) -> Result<SignedUploadUrlResponse, Error>

Create a signed upload url,

Returns the url (without hostname) and authorization token on success

§Example
let signed = client.create_signed_upload_url("list_files", "42.txt").await.unwrap();
Source

pub async fn upload_to_signed_url( &self, bucket_id: &str, token: &str, data: Vec<u8>, path: &str, options: Option<FileOptions<'_>>, ) -> Result<UploadToSignedUrlResponse, Error>

Upload a file to a signed url

Returns the url (without hostname) and authorization token upon success

§Example
let object = client
    .upload_to_signed_url("bucket_id", "upload_token", file, "path/to/file.txt", None).await.unwrap();
Source

pub async fn get_public_url( &self, bucket_id: &str, path: &str, options: Option<DownloadOptions<'_>>, ) -> Result<String, Error>

Returns a public URL for accessing an asset in a storage bucket

§Arguments
  • bucket_id - Unique identifier for the storage bucket
  • path - Path to the file within the bucket, formatted as ‘folder/subfolder/filename.ext’
  • options - Optional parameters for customizing the download URL:
    • Image transformations (uses /render/image endpoint)
    • Download behavior configurations
§Examples
// Basic usage
let url = client.get_public_url("photos", "vacations/beach.jpg", None).await?;

// With image transformation
let options = DownloadOptions {
    transform: Some(Transform { width: 300, ..Default::default() }),
    ..Default::default()
};
let url = client.get_public_url("photos", "vacations/beach.jpg", Some(options)).await?;
§Note

The URL can also be manually constructed by combining: {project_url}/storage/v1/object/public/{bucket_id}/{path}

Source

pub async fn move_file( &self, from_bucket: &str, to_bucket: Option<&str>, from_path: &str, to_path: &str, ) -> Result<String, Error>

Move a file from one path to another

§Example
// Copies `3.txt` into `folder/4.txt` within the same bucket
let key = client
    .move_file("from_bucket", None, "3.txt", Some("folder/4.txt"))
    .await
    .unwrap();

// Copies `a.txt` into `folder/b.txt` in a different bucket
let key = client
    .move_file("from_bucket", "to_bucket", "a.txt", Some("folder/b.txt"))
    .await
    .unwrap();

Trait Implementations§

Source§

impl Clone for StorageClient

Source§

fn clone(&self) -> StorageClient

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

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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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,