From dfa4583134f1f880fd00f556706b3a871a5e9d97 Mon Sep 17 00:00:00 2001 From: Austin Tran Date: Tue, 1 Aug 2023 00:27:48 +0700 Subject: [PATCH] feat: Add create and upload a signed url --- client.go | 4 +-- storage.go | 67 +++++++++++++++++++++++++++++++++++++++-- test/fileupload_test.go | 31 ++++++++++++++++--- test/storage_test.go | 3 +- 4 files changed, 93 insertions(+), 12 deletions(-) diff --git a/client.go b/client.go index 64d1dc0..18c57b7 100644 --- a/client.go +++ b/client.go @@ -5,9 +5,7 @@ import ( "net/url" ) -var ( - version = "v0.5.6" -) +var version = "v0.5.6" type Client struct { clientError error diff --git a/storage.go b/storage.go index 4d5dfaa..b0c3c2a 100644 --- a/storage.go +++ b/storage.go @@ -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 { @@ -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 @@ -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"` +} diff --git a/test/fileupload_test.go b/test/fileupload_test.go index ccbd7e0..4f94a0e 100644 --- a/test/fileupload_test.go +++ b/test/fileupload_test.go @@ -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") @@ -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) { @@ -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) +} diff --git a/test/storage_test.go b/test/storage_test.go index c5b6152..4fa145e 100644 --- a/test/storage_test.go +++ b/test/storage_test.go @@ -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) {