Android Example [B4X] Supabase - Storage Files


This is a very simple tutorial on how to use the storage file options.

Upload File
Uploads a file to an existing bucket.
B4X:
    Dim UploadFile As Supabase_StorageFile = xSupabase.Storage.UploadFile("Avatar","test.png")
    UploadFile.FileBody(xSupabase.Storage.ConvertFile2Binary(File.DirAssets,"test.jpg"))
    Wait For (UploadFile.Execute) Complete (StorageFile As SupabaseStorageFile)
    If StorageFile.Error.Success Then
        Log($"File ${"test.jpg"} successfully uploaded "$)
    Else
        Log("Error: " & StorageFile.Error.ErrorMessage)
    End If
Download File
Downloads a file.
B4X:
    Dim DownloadFile As Supabase_StorageFile = xSupabase.Storage.DownloadFile("Avatar","test.png")
    Wait For (DownloadFile.Execute) Complete (StorageFile As SupabaseStorageFile)
    If StorageFile.Error.Success Then
        Log($"File ${"test.jpg"} successfully downloaded "$)
        ImageView1.SetBitmap(xSupabase.Storage.BytesToImage(StorageFile.FileBody))
    Else
        Log("Error: " & StorageFile.Error.ErrorMessage)
    End If
Update File
Replaces an existing file at the specified path with a new one.
B4X:
    Dim UpdateFile As Supabase_StorageFile = xSupabase.Storage.UpdateFile("Avatar","test.png")
    UpdateFile.FileBody(xSupabase.Storage.ConvertFile2Binary(File.DirAssets,"test2.jpg"))
    Wait For (UpdateFile.Execute) Complete (StorageFile As SupabaseStorageFile)
    If StorageFile.Error.Success Then
        Log($"File ${"test.jpg"} successfully updated "$)
    Else
        Log("Error: " & StorageFile.Error.ErrorMessage)
    End If
Delete File
comming soon
Move File
Moves an existing file to a new path in the same bucket.
FromPath - The original file path, including the current file name. For example `folder/image.png`
ToPath - The new file path, including the new file name. For example `folder/image-copy.png`
B4X:
    Wait For (xSupabase.Storage.MoveFile("Avatar","public/avatar1.png", "private/avatar2.png").Execute) Complete (StorageFile As SupabaseStorageFile)
    If StorageFile.Error.Success Then
        Log($"Files successfully moved "$)
    Else
        Log("Error: " & StorageFile.Error.ErrorMessage)
    End If
Copy File
Copies an existing file to a new path in the same bucket.
FromPath - The original file path, including the current file name. For example `folder/image.png`
ToPath - The new file path, including the new file name. For example `folder/image-copy.png`
B4X:
    Wait For (xSupabase.Storage.CopyFile("Avatar","public/avatar1.png", "private/avatar2.png").Execute) Complete (StorageFile As SupabaseStorageFile)
    If StorageFile.Error.Success Then
        Log($"Files successfully copied "$)
    Else
        Log("Error: " & StorageFile.Error.ErrorMessage)
    End If
Get Public Url
Retrieve public URL
A simple convenience function to get the URL for an asset in a public bucket. If you do not want to use this function, you can construct the public URL by concatenating the bucket URL with the path to the asset.
This function does not verify if the bucket is public. If a public URL is created for a bucket which is not public, you will not be able to download the asset.
B4X:
Log(xSupabase.Storage.GetPublicUrl("Avatar","test.png"))
Create signed Url
Create signed url to download file without requiring permissions. This URL can be valid for a set number of seconds.
B4X:
    Wait For (xSupabase.Storage.CreateSignedUrl("Avatar","test.png",60).Execute) Complete (StorageFile As SupabaseStorageFile)
    If StorageFile.Error.Success Then
        Log(StorageFile.SignedURL)

        Dim DownloadFile As Supabase_StorageFile = xSupabase.Storage.DownloadFile("Avatar")
        DownloadFile.Path("test.png")
        DownloadFile.SignedURL(StorageFile.SignedURL)
        Wait For (DownloadFile.Execute) Complete (StorageFile As SupabaseStorageFile)
        If StorageFile.Error.Success Then
            Log($"File from signed URL successfully downloaded "$)
            ImageView1.SetBitmap(xSupabase.Storage.BytesToImage(StorageFile.FileBody))
        Else
            Log("Error: " & StorageFile.Error.ErrorMessage)
        End If

    Else
        Log("Error: " & StorageFile.Error.ErrorMessage)
    End If
 

josejad

Expert
Licensed User
Longtime User
Hi Alexander, you're doing a great job with this feature.

I'm not familiar with Supabase, so I'm not sure what a bucket is, but just two questions regarding this:

- Can you create a shared bucket to share files from a project, for example?
- Searching for buckets, I've seen this: "Supabase Storage includes a built-in image optimizer, so you can resize and compress your media files on the fly." Is this feature implemented?

Thanks again.
 

Alexander Stolte

Expert
Licensed User
Longtime User
- Can you create a shared bucket to share files from a project, for example?
Buckets can be private or public.
Searching for buckets, I've seen this: "Supabase Storage includes a built-in image optimizer, so you can resize and compress your media files on the fly." Is this feature implemented?
I know, but it is currently not yet in. It is also currently not yet inside that you can upload and download files part way.
I'm not familiar with Supabase, so I'm not sure what a bucket is, but just two questions regarding this:
Everything is documented at supabase.
 

Alexander Stolte

Expert
Licensed User
Longtime User
- Searching for buckets, I've seen this: "Supabase Storage includes a built-in image optimizer, so you can resize and compress your media files on the fly." Is this feature implemented?
While implementing it, I came across the following:
1694597473970.png
 
Top