Skip to content

Commit c44d38e

Browse files
committed
add client Copy method
1 parent 98f851d commit c44d38e

File tree

2 files changed

+75
-5
lines changed

2 files changed

+75
-5
lines changed

‎client.go‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ func (c *VercelBlobClient) List(options ListCommandOptions) (*ListBlobResult, er
285285
// be used to later download the blob.
286286
func (c *VercelBlobClient) Put(pathname string, body io.Reader, options PutCommandOptions) (*PutBlobPutResult, error) {
287287

288-
if pathname == "" {
288+
if len(pathname) == 0 {
289289
return nil, NewInvalidInputError("pathname")
290290
}
291291

@@ -418,6 +418,59 @@ func (c *VercelBlobClient) Delete(urlPath string) error {
418418
return nil
419419
}
420420

421+
func (c *VercelBlobClient) Copy(fromUrl, toPath string, options PutCommandOptions) (*PutBlobPutResult, error) {
422+
if len(fromUrl) == 0 {
423+
return nil, NewInvalidInputError("fromUrl")
424+
}
425+
426+
if len(toPath) == 0 {
427+
return nil, NewInvalidInputError("toPath")
428+
}
429+
430+
apiUrl := c.getAPIURL(toPath)
431+
432+
req, err := http.NewRequest(http.MethodPut, apiUrl, nil)
433+
if err != nil {
434+
return nil, err
435+
}
436+
437+
req.URL.Query().Add("fromUrl", fromUrl)
438+
439+
c.addAPIVersionHeader(req)
440+
err = c.addAuthorizationHeader(req, "put", toPath)
441+
if err != nil {
442+
return nil, err
443+
}
444+
445+
if !options.AddRandomSuffix {
446+
req.Header.Set("X-Add-Random-Suffix", "0")
447+
}
448+
if options.ContentType != "" {
449+
req.Header.Set("X-Content-Type", options.ContentType)
450+
}
451+
if options.CacheControlMaxAge > 0 {
452+
req.Header.Set("X-Cache-Control-Max-Age", strconv.FormatUint(options.CacheControlMaxAge, 10))
453+
}
454+
455+
client := &http.Client{}
456+
resp, err := client.Do(req)
457+
if err != nil {
458+
return nil, err
459+
}
460+
defer resp.Body.Close()
461+
462+
if resp.StatusCode != http.StatusOK {
463+
return nil, c.handleError(resp)
464+
}
465+
466+
var result PutBlobPutResult
467+
if err = json.NewDecoder(resp.Body).Decode(&result); err != nil {
468+
return nil, err
469+
}
470+
471+
return &result, nil
472+
}
473+
421474
// Download a blob from the blob store
422475
//
423476
// # Arguments

‎client_test.go‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ package vercel_blob
22

33
import (
44
"fmt"
5-
"strings"
5+
"io"
6+
"os"
67
"testing"
78
)
89

@@ -19,12 +20,14 @@ func Test_CountFiles(t *testing.T) {
1920

2021
func Test_PutWithRandomSuffix(t *testing.T) {
2122
client := NewVercelBlobClient()
23+
f, _ := os.Open("a.png")
24+
defer f.Close()
2225
file1, err := client.Put(
23-
"vercel_blob_unittest/a.txt",
24-
strings.NewReader("test body"),
26+
"vercel_blob_unittest/a.png",
27+
io.Reader(f),
2528
PutCommandOptions{
2629
AddRandomSuffix: true,
27-
ContentType: "text/plain",
30+
//ContentType: "multipart/form-data",
2831
})
2932
if err != nil {
3033
t.Error(err)
@@ -34,6 +37,20 @@ func Test_PutWithRandomSuffix(t *testing.T) {
3437
}
3538
}
3639

40+
func Test_Copy(t *testing.T) {
41+
//https://fetegzn4vw3t5yqf.public.blob.vercel-storage.com/vercel_blob_unittest/a.txt
42+
client := NewVercelBlobClient()
43+
res, err := client.Copy("https://fetegzn4vw3t5yqf.public.blob.vercel-storage.com/vercel_blob_unittest/a.txt",
44+
"vercel_blob_unittest/B.txt",
45+
PutCommandOptions{})
46+
if err != nil {
47+
t.Error(err)
48+
return
49+
} else {
50+
fmt.Println(res.URL)
51+
}
52+
}
53+
3754
func Test_Partial_Download(t *testing.T) {
3855
client := NewVercelBlobClient()
3956
bytes, err := client.Download("vercel_blob_unittest/a.txt",

0 commit comments

Comments
 (0)