Skip to content

Commit

Permalink
update presignedurl demo
Browse files Browse the repository at this point in the history
  • Loading branch information
jojoliang committed Jul 22, 2024
1 parent 8d0d05e commit 4257d33
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 44 deletions.
19 changes: 1 addition & 18 deletions example/object/getByPresignedURL.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package main

import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -55,23 +53,8 @@ func main() {
name := "test"
ctx := context.Background()

// Normal header way to get object
resp, err := c.Object.Get(ctx, name, nil)
log_status(err)
bs, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()

// Get presigned
presignedURL, err := c.Object.GetPresignedURL(ctx, http.MethodGet, name, ak, sk, time.Hour, nil)
log_status(err)

// Get object by presinged url
resp2, err := http.Get(presignedURL.String())
log_status(err)
bs2, _ := ioutil.ReadAll(resp2.Body)
resp2.Body.Close()
fmt.Printf("result2 is : %s\n", string(bs2))

fmt.Printf("%v\n\n", bytes.Compare(bs2, bs) == 0)

fmt.Println(presignedURL.String())
}
12 changes: 0 additions & 12 deletions example/object/getByPresignedURL2.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/http"
"net/url"
"os"
"strings"
"time"
)

Expand Down Expand Up @@ -35,15 +34,4 @@ func main() {
return
}
fmt.Printf("url: %v\n", presignedURL.String())
// 通过预签名方式上传对象
data := "test upload with presignedURL"
f := strings.NewReader(data)
req, err := http.NewRequest(http.MethodPut, presignedURL.String(), f)
if err != nil {
fmt.Printf("Error: %v\n", err)
}
_, err = http.DefaultClient.Do(req)
if err != nil {
fmt.Printf("Error: %v\n", err)
}
}
14 changes: 0 additions & 14 deletions example/object/presigned_url_with_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,7 @@ func main() {
fmt.Printf("Error: %v\n", err)
return
}
// Get object by presinged url
resp, err := http.Get(presignedURL.String())
if err != nil {
fmt.Printf("Error: %v\n", err)
}
defer resp.Body.Close()
fmt.Println(presignedURL.String())
fmt.Printf("resp:%v\n", resp)

// 方法2 通过 PresignedURLOptions 设置 x-cos-security-token
opt := &cos.PresignedURLOptions{
Expand All @@ -57,12 +50,5 @@ func main() {
fmt.Printf("Error: %v\n", err)
return
}
// Get object by presinged url
resp, err = http.Get(presignedURL.String())
if err != nil {
fmt.Printf("Error: %v\n", err)
}
defer resp.Body.Close()
fmt.Println(presignedURL.String())
fmt.Printf("resp:%v\n", resp)
}

0 comments on commit 4257d33

Please sign in to comment.