From fce9db6f338bf349d109506d6ac3560e6fabc557 Mon Sep 17 00:00:00 2001 From: Austin Tran Date: Sun, 8 Oct 2023 14:03:42 +0700 Subject: [PATCH 1/2] chore: update Readme with more examples --- README.md | 150 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 116 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 33a6cb6..e92b14d 100644 --- a/README.md +++ b/README.md @@ -2,15 +2,15 @@ This library is a Golang client for the [Supabase Storage API](https://supabase.com/docs/guides/storage). It's a collection of helper functions that help you manage your buckets through the API. -## Quick start +## Quick start guide -Install +#### Install ```shell go get github.com/supabase-community/storage-go ``` -Usage +### Connecting to the storage backend ```go package main @@ -24,49 +24,131 @@ import ( ) func main() { - client := storage_go.NewClient("https://.supabase.co/storage/v1", "", nil) + storageClient := storage_go.NewClient("https://.supabase.co/storage/v1", "", nil) - // Create a new bucket - bucket, err := client.CreateBucket("bucket-id", storage_go.BucketOptions{Public: true}) + // Another way + storage_go.NewClient("https://.supabase.co/storage/v1", "", ) +} +``` + +### Handling resources + +#### Handling Storage Buckets - if err.Error != "" { - log.Fatal("error creating bucket, ", err) - } +- Create a new Storage bucket: - // Upload a file - file, err := os.Open("dummy.txt") - if err != nil { - panic(err) - } +```go + result, err := storageClient.CreateBucket("bucket-id", storage_go.BucketOptions{ + Public: true, + }) +``` - resp := client.UploadFile("bucket-name", "file.txt", file) - fmt.Println(resp) +- Retrieve the details of an existing Storage bucket: + +```go + result, err := storageClient.GetBucket("bucket-id") +``` - // Update Bucket - response, err := client.UpdateBucket(bucket.Id, storage_go.BucketOptions{Public: true}) - fmt.Println(response) +- Update a new Storage bucket: - // Empty Bucket - response, err = client.EmptyBucket(bucket.Id) - fmt.Println(response) +```go + result, err := storageClient.UpdateBucket("bucket-id", storage_go.BucketOptions{ + Public: true, + }) +``` - // Delete Bucket - response, err = client.DeleteBucket(bucket.Id) - fmt.Println(response) +- Remove all objects inside a single bucket: - // Get a bucket by its id - bucket = GetBucket("bucket-id") - fmt.Println(bucket) +```go + result, err := storageClient.EmptyBucket("bucket-id") +``` - // Get all buckets - fmt.Println(client.ListBuckets()) +- Delete an existing bucket (a bucket can't be deleted with existing objects inside it): -} +```go + result, err := storageClient.DeleteBucket("bucket-id") ``` -> Note to self: -> Update after tagging: -> GOPROXY=proxy.golang.org go list -m github.com/supabase-community/storage-go@v0.6.8 +- Retrieve the details of all Storage buckets within an existing project: + +```go + result, err := storageClient.ListBuckets("bucket-id") +``` + +#### Handling Files + +```go + fileBody := ... // load your file here + + result, err := storageClient.UploadFile("test", "test.txt", fileBody) +``` + +> Note: The `upload` method also accepts a map of optional parameters. + +- Download a file from an exisiting bucket: + +```go + result, err := storageClient.DownloadFile("bucket-id", "test.txt") +``` + +- List all the files within a bucket: + +```go + result, err := storageClient.ListFiles("bucket-id", "", storage_go.FileSearchOptions{ + Limit: 10, + Offset: 0, + SortByOptions: storage_go.SortBy{ + Column: "", + Order: "", + }, + }) +``` + +> Note: The `list` method also accepts a map of optional parameters. + +- Replace an existing file at the specified path with a new one: + +```go + fileBody := ... // load your file here + + result, err := storageClient.UpdateFile("test", "test.txt", file) +``` + +- Move an existing file: + +```go + result, err := storageClient.MoveFile("test", "test.txt", "random/test.txt") +``` + +- Delete files within the same bucket: + +```go + result, err := storageClient.RemoveFile("test", []string{"book.pdf"}) +``` + +- Create signed URL to download file without requiring permissions: + +```go + const expireIn = 60 + + result, err := storageClient.CreateSignedUrl("test", "test.mp4", expireIn) + +``` + +- Retrieve URLs for assets in public buckets: + +```go + result, err := storageClient.GetPublicUrl("test", "book.pdf") +``` + +- Create an signed URL and upload to signed URL: + +```go + fileBody := ... // load your file here + + resp, err := storageClient.CreateSignedUploadUrl("test", "test.txt") + res, err := storageClient.UploadToSignedUrl(resp.Url, file) +``` ## License From ab151740a9270726eb7af0afe6fc969b06d8626a Mon Sep 17 00:00:00 2001 From: Austin Tran Date: Sun, 8 Oct 2023 14:16:57 +0700 Subject: [PATCH 2/2] chore: update format code inside Readme --- README.md | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index e92b14d..fcb5341 100644 --- a/README.md +++ b/README.md @@ -25,9 +25,6 @@ import ( func main() { storageClient := storage_go.NewClient("https://.supabase.co/storage/v1", "", nil) - - // Another way - storage_go.NewClient("https://.supabase.co/storage/v1", "", ) } ``` @@ -39,8 +36,8 @@ func main() { ```go result, err := storageClient.CreateBucket("bucket-id", storage_go.BucketOptions{ - Public: true, - }) + Public: true, + }) ``` - Retrieve the details of an existing Storage bucket: @@ -53,8 +50,8 @@ func main() { ```go result, err := storageClient.UpdateBucket("bucket-id", storage_go.BucketOptions{ - Public: true, - }) + Public: true, + }) ``` - Remove all objects inside a single bucket: @@ -95,13 +92,13 @@ func main() { ```go result, err := storageClient.ListFiles("bucket-id", "", storage_go.FileSearchOptions{ - Limit: 10, - Offset: 0, - SortByOptions: storage_go.SortBy{ - Column: "", - Order: "", - }, - }) + Limit: 10, + Offset: 0, + SortByOptions: storage_go.SortBy{ + Column: "", + Order: "", + }, + }) ``` > Note: The `list` method also accepts a map of optional parameters. @@ -132,7 +129,6 @@ func main() { const expireIn = 60 result, err := storageClient.CreateSignedUrl("test", "test.mp4", expireIn) - ``` - Retrieve URLs for assets in public buckets: @@ -147,7 +143,7 @@ func main() { fileBody := ... // load your file here resp, err := storageClient.CreateSignedUploadUrl("test", "test.txt") - res, err := storageClient.UploadToSignedUrl(resp.Url, file) + res, err := storageClient.UploadToSignedUrl(resp.Url, file) ``` ## License