Skip to content

Commit

Permalink
feat: Add create and upload a signed url
Browse files Browse the repository at this point in the history
  • Loading branch information
tranhoangvuit committed Aug 7, 2023
1 parent 9d94951 commit dfa4583
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 12 deletions.
4 changes: 1 addition & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (
"net/url"
)

var (
version = "v0.5.6"
)
var version = "v0.5.6"

type Client struct {
clientError error
Expand Down
67 changes: 64 additions & 3 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ func (c *Client) UploadOrUpdateFile(bucketId string, relativePath string, data i
_path := removeEmptyFolderName(bucketId + "/" + relativePath)

var (
res *http.Response
err error
res *http.Response
err error
request *http.Request
method = http.MethodPost
method = http.MethodPost
)

if update {
Expand Down Expand Up @@ -110,6 +110,59 @@ func (c *Client) CreateSignedUrl(bucketId string, filePath string, expiresIn int
return response
}

func (c *Client) CreateSignedUploadUrl(bucketId string, filePath string) (SignedUploadUrlResponse, error) {
emptyBody, _ := json.Marshal(struct{}{})
request, err := http.NewRequest(
http.MethodPost,
c.clientTransport.baseUrl.String()+"/object/upload/sign/"+bucketId+"/"+filePath, bytes.NewBuffer(emptyBody))
if err != nil {
return SignedUploadUrlResponse{}, err
}
res, err := c.session.Do(request)
if err != nil {
return SignedUploadUrlResponse{}, err
}

body, err := ioutil.ReadAll(res.Body)
if err != nil {
return SignedUploadUrlResponse{}, err
}
var response SignedUploadUrlResponse
err = json.Unmarshal(body, &response)

return response, err
}

func (c *Client) UploadToSignedUrl(filePath string, fileBody io.Reader) (*UploadToSignedUrlResponse, error) {
c.clientTransport.header.Set("cache-control", defaultFileCacheControl)
c.clientTransport.header.Set("content-type", defaultFileContentType)
c.clientTransport.header.Set("x-upsert", strconv.FormatBool(defaultFileUpsert))

bodyRequest := bufio.NewReader(fileBody)
path := removeEmptyFolderName(filePath)

request, err := http.NewRequest(http.MethodPut, c.clientTransport.baseUrl.String()+path, bodyRequest)
if err != nil {
return nil, err
}
resp, err := c.session.Do(request)
if err != nil {
return nil, err
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var response UploadToSignedUrlResponse
err = json.Unmarshal(body, &response)
if err != nil {
return nil, err
}

return &response, err
}

func (c *Client) GetPublicUrl(bucketId string, filePath string, urlOptions ...UrlOptions) SignedUrlResponse {
var response SignedUrlResponse

Expand Down Expand Up @@ -284,3 +337,11 @@ type UrlOptions struct {
Transform TransformOptions `json:"transform"`
Download bool `json:"download"`
}

type SignedUploadUrlResponse struct {
Url string `json:"url"`
}

type UploadToSignedUrlResponse struct {
Key string `json:"key"`
}
31 changes: 26 additions & 5 deletions test/fileupload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package test

import (
"fmt"
"github.com/supabase-community/storage-go"
"os"
"testing"

storage_go "github.com/supabase-community/storage-go"
)

var rawUrl = "https://abc.supabase.co/storage/v1"
var token = ""
var (
rawUrl = "https://abc.supabase.co/storage/v1"
token = ""
)

func TestUpload(t *testing.T) {
file, err := os.Open("dummy.txt")
Expand All @@ -19,8 +22,8 @@ func TestUpload(t *testing.T) {
resp := c.UploadFile("test1", "test.txt", file)
fmt.Println(resp)

//resp = c.UploadFile("test1", "hola.txt", []byte("hello world"))
//fmt.Println(resp)
// resp = c.UploadFile("test1", "hola.txt", []byte("hello world"))
// fmt.Println(resp)
}

func TestUpdate(t *testing.T) {
Expand Down Expand Up @@ -75,3 +78,21 @@ func TestListFile(t *testing.T) {

fmt.Println(resp)
}

func TestCreateUploadSignedUrl(t *testing.T) {
c := storage_go.NewClient(rawUrl, token, map[string]string{"apiKey": token})
resp, err := c.CreateSignedUploadUrl("your-bucket-id", "book.pdf")

fmt.Println(resp, err)
}

func TestUploadToSignedUrl(t *testing.T) {
c := storage_go.NewClient(rawUrl, token, map[string]string{"apiKey": token})
file, err := os.Open("dummy.txt")
if err != nil {
panic(err)
}
resp, err := c.UploadToSignedUrl("signed-url-response", file)

fmt.Println(resp, err)
}
3 changes: 2 additions & 1 deletion test/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package test

import (
"fmt"
"github.com/supabase-community/storage-go"
"testing"

"github.com/supabase-community/storage-go"
)

func TestBucketListAll(t *testing.T) {
Expand Down

0 comments on commit dfa4583

Please sign in to comment.