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://
api_key: String
WARN: The service role
key has the ability to bypass Row Level Security. Never share it publicly.
Implementations§
Source§impl StorageClient
impl StorageClient
Sourcepub fn new(project_url: String, api_key: String) -> Self
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();
Sourcepub async fn new_from_env() -> Result<StorageClient, Error>
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();
Sourcepub 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>
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();
Sourcepub async fn delete_bucket(&self, id: &str) -> Result<(), Error>
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();
Sourcepub async fn get_bucket(&self, bucket_id: &str) -> Result<Bucket, Error>
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();
Sourcepub async fn list_buckets(&self) -> Result<Buckets, Error>
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();
Sourcepub 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>
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();
Sourcepub async fn empty_bucket(&self, id: &str) -> Result<String, Error>
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();
Sourcepub async fn replace_file(
&self,
bucket_id: &str,
data: Vec<u8>,
path: &str,
options: Option<FileOptions<'_>>,
) -> Result<ObjectResponse, Error>
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();
Sourcepub async fn update_file(
&self,
bucket_id: &str,
data: Vec<u8>,
path: &str,
options: Option<FileOptions<'_>>,
) -> Result<ObjectResponse, Error>
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();
Sourcepub async fn upload_file(
&self,
bucket_id: &str,
data: Vec<u8>,
path: &str,
options: Option<FileOptions<'_>>,
) -> Result<ObjectResponse, Error>
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();
Sourcepub async fn download_file(
&self,
bucket_id: &str,
path: &str,
options: Option<DownloadOptions<'_>>,
) -> Result<Vec<u8>, Error>
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();
Sourcepub async fn delete_file(
&self,
bucket_id: &str,
path: &str,
) -> Result<BucketResponse, Error>
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();
Sourcepub async fn list_files(
&self,
bucket_id: &str,
path: Option<&str>,
options: Option<FileSearchOptions<'_>>,
) -> Result<Vec<FileObject>, Error>
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();
Sourcepub 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>
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();
Sourcepub async fn create_signed_url(
&self,
bucket_id: &str,
path: &str,
expires_in: u64,
) -> Result<String, Error>
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();
Sourcepub async fn create_multiple_signed_urls(
&self,
bucket_id: &str,
paths: Vec<&str>,
expires_in: u64,
) -> Result<Vec<String>, Error>
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();
Sourcepub async fn create_signed_upload_url(
&self,
bucket_id: &str,
path: &str,
) -> Result<SignedUploadUrlResponse, Error>
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();
Sourcepub async fn upload_to_signed_url(
&self,
bucket_id: &str,
token: &str,
data: Vec<u8>,
path: &str,
options: Option<FileOptions<'_>>,
) -> Result<UploadToSignedUrlResponse, Error>
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();
Sourcepub async fn get_public_url(
&self,
bucket_id: &str,
path: &str,
options: Option<DownloadOptions<'_>>,
) -> Result<String, Error>
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 bucketpath
- 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
- Image transformations (uses
§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}
Sourcepub async fn move_file(
&self,
from_bucket: &str,
to_bucket: Option<&str>,
from_path: &str,
to_path: &str,
) -> Result<String, Error>
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
impl Clone for StorageClient
Source§fn clone(&self) -> StorageClient
fn clone(&self) -> StorageClient
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more