-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
alantong
committed
Mar 14, 2019
1 parent
4ff4f2d
commit 0c2d28c
Showing
2 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
"os" | ||
"time" | ||
|
||
"io/ioutil" | ||
|
||
"bytes" | ||
"net/http" | ||
|
||
"github.com/tencentyun/cos-go-sdk-v5" | ||
"github.com/tencentyun/cos-go-sdk-v5/debug" | ||
) | ||
|
||
func main() { | ||
ak := os.Getenv("COS_SECRETID") | ||
sk := os.Getenv("COS_SECRETKEY") | ||
u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com") | ||
b := &cos.BaseURL{BucketURL: u} | ||
c := cos.NewClient(b, &http.Client{ | ||
Transport: &cos.AuthorizationTransport{ | ||
SecretID: ak, | ||
SecretKey: sk, | ||
Expire: time.Hour, | ||
Transport: &debug.DebugRequestTransport{ | ||
RequestHeader: true, | ||
RequestBody: true, | ||
ResponseHeader: true, | ||
ResponseBody: true, | ||
}, | ||
}, | ||
}) | ||
|
||
name := "test" | ||
ctx := context.Background() | ||
|
||
// Normal header way to get object | ||
resp, err := c.Object.Get(ctx, name, nil) | ||
if err != nil { | ||
panic(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) | ||
if err != nil { | ||
panic(err) | ||
} | ||
// Get object by presinged url | ||
resp2, err := http.Get(presignedURL.String()) | ||
if err != nil { | ||
panic(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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters