Skip to content

Commit

Permalink
presigned url, and demo
Browse files Browse the repository at this point in the history
  • Loading branch information
alantong committed Mar 14, 2019
1 parent 4ff4f2d commit 0c2d28c
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
65 changes: 65 additions & 0 deletions example/object/getByPresignedURL.go
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)

}
42 changes: 42 additions & 0 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"time"
)

// ObjectService 相关 API
Expand All @@ -26,6 +28,11 @@ type ObjectGetOptions struct {
IfModifiedSince string `url:"-" header:"If-Modified-Since,omitempty"`
}

// presignedURLTestingOptions is the opt of presigned url
type presignedURLTestingOptions struct {
authTime *AuthTime
}

// Get Object 请求可以将一个文件(Object)下载至本地。
// 该操作需要对目标 Object 具有读权限或目标 Object 对所有人都开放了读权限(公有读)。
//
Expand Down Expand Up @@ -66,6 +73,41 @@ func (s *ObjectService) GetToFile(ctx context.Context, name, localpath string, o
return resp, nil
}

// GetPresignedURL get the object presigned to down or upload file by url
func (s *ObjectService) GetPresignedURL(ctx context.Context, httpMethod, name, ak, sk string, expired time.Duration, opt interface{}) (*url.URL, error) {
sendOpt := sendOptions{
baseURL: s.client.BaseURL.BucketURL,
uri: "/" + encodeURIComponent(name),
method: httpMethod,
optQuery: opt,
optHeader: opt,
}
req, err := s.client.newRequest(ctx, sendOpt.baseURL, sendOpt.uri, sendOpt.method, sendOpt.body, sendOpt.optQuery, sendOpt.optHeader)
if err != nil {
return nil, err
}

var authTime *AuthTime
if opt != nil {
if opt, ok := opt.(*presignedURLTestingOptions); ok {
authTime = opt.authTime
}
}
if authTime == nil {
authTime = NewAuthTime(expired)
}
authorization := newAuthorization(ak, sk, req, authTime)
sign := encodeURIComponent(authorization)

if req.URL.RawQuery == "" {
req.URL.RawQuery = fmt.Sprintf("sign=%s", sign)
} else {
req.URL.RawQuery = fmt.Sprintf("%s&sign=%s", req.URL.RawQuery, sign)
}
return req.URL, nil

}

// ObjectPutHeaderOptions the options of header of the put object
type ObjectPutHeaderOptions struct {
CacheControl string `header:"Cache-Control,omitempty" url:"-"`
Expand Down

0 comments on commit 0c2d28c

Please sign in to comment.