Skip to content

Commit

Permalink
Merge branch 'supabase-community:dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
tranhoangvuit authored Oct 9, 2023
2 parents 5b5eeb2 + b84c70d commit 3967037
Showing 1 changed file with 112 additions and 34 deletions.
146 changes: 112 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,49 +24,127 @@ import (
)

func main() {
client := storage_go.NewClient("https://<project-reference-id>.supabase.co/storage/v1", "<project-secret-api-key>", nil)
storageClient := storage_go.NewClient("https://<project-reference-id>.supabase.co/storage/v1", "<project-secret-api-key>", nil)
}
```

// Create a new bucket
bucket, err := client.CreateBucket("bucket-id", storage_go.BucketOptions{Public: true})
### Handling resources

if err.Error != "" {
log.Fatal("error creating bucket, ", err)
}
#### Handling Storage Buckets

// Upload a file
file, err := os.Open("dummy.txt")
if err != nil {
panic(err)
}
- Create a new Storage bucket:

resp := client.UploadFile("bucket-name", "file.txt", file)
fmt.Println(resp)
```go
result, err := storageClient.CreateBucket("bucket-id", storage_go.BucketOptions{
Public: true,
})
```

// Update Bucket
response, err := client.UpdateBucket(bucket.Id, storage_go.BucketOptions{Public: true})
fmt.Println(response)
- Retrieve the details of an existing Storage bucket:

// Empty Bucket
response, err = client.EmptyBucket(bucket.Id)
fmt.Println(response)
```go
result, err := storageClient.GetBucket("bucket-id")
```

// Delete Bucket
response, err = client.DeleteBucket(bucket.Id)
fmt.Println(response)
- Update a new Storage bucket:

// Get a bucket by its id
bucket = GetBucket("bucket-id")
fmt.Println(bucket)
```go
result, err := storageClient.UpdateBucket("bucket-id", storage_go.BucketOptions{
Public: true,
})
```

// Get all buckets
fmt.Println(client.ListBuckets())
- Remove all objects inside a single bucket:

}
```go
result, err := storageClient.EmptyBucket("bucket-id")
```

- Delete an existing bucket (a bucket can't be deleted with existing objects inside it):

```go
result, err := storageClient.DeleteBucket("bucket-id")
```

- 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 to self:
> Update after tagging:
> GOPROXY=proxy.golang.org go list -m github.com/supabase-community/[email protected]
> 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

Expand Down

0 comments on commit 3967037

Please sign in to comment.