first to commit project
This commit is contained in:
43
example/object/MutiUpload.go
Normal file
43
example/object/MutiUpload.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"os"
|
||||
"time"
|
||||
"net/http"
|
||||
|
||||
"fmt"
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("http://tencentyun02-1252448703.cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{BucketURL: u}
|
||||
c := cos.NewClient(b, &http.Client{
|
||||
//设置超时时间
|
||||
Timeout: 100 * time.Second,
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
SecretID: os.Getenv("COS_Key"),
|
||||
SecretKey: os.Getenv("COS_Secret"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: false,
|
||||
RequestBody: false,
|
||||
ResponseHeader: false,
|
||||
ResponseBody: false,
|
||||
},
|
||||
},
|
||||
})
|
||||
f,err:=os.Open("E:/cos-php-sdk.zip")
|
||||
if err!=nil {panic(err)}
|
||||
opt := &cos.MultiUploadOptions{
|
||||
OptIni: nil,
|
||||
PartSize:1,
|
||||
}
|
||||
v, _, err := c.Object.MultiUpload(
|
||||
context.Background(), "test", f, opt,
|
||||
)
|
||||
if err!=nil {panic(err)}
|
||||
fmt.Println(v)
|
||||
}
|
||||
43
example/object/abortMultipartUpload.go
Normal file
43
example/object/abortMultipartUpload.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func main() {
|
||||
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: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: false,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
name := "test_multipart.txt"
|
||||
v, _, err := c.Object.InitiateMultipartUpload(context.Background(), name, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("%s\n", v.UploadID)
|
||||
|
||||
resp, err := c.Object.AbortMultipartUpload(context.Background(), name, v.UploadID)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("%s\n", resp.Status)
|
||||
}
|
||||
75
example/object/append.go
Normal file
75
example/object/append.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net/url"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func genBigData(blockSize int) []byte {
|
||||
b := make([]byte, blockSize)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func main() {
|
||||
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: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: false,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
startTime := time.Now()
|
||||
|
||||
name := fmt.Sprintf("test/test_object_append_%s", startTime.Format(time.RFC3339))
|
||||
data := genBigData(1024 * 1024 * 1)
|
||||
length := len(data)
|
||||
r := bytes.NewReader(data)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// 第一次就必须 append
|
||||
resp, err := c.Object.Append(ctx, name, 0, r, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("%s\n", resp.Status)
|
||||
|
||||
// head
|
||||
if _, err = c.Object.Head(ctx, name, nil); err != nil {
|
||||
panic(err)
|
||||
return
|
||||
}
|
||||
|
||||
// 再次 append
|
||||
data = genBigData(1024 * 1024 * 5)
|
||||
r = bytes.NewReader(data)
|
||||
resp, err = c.Object.Append(context.Background(), name, length, r, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("%s\n", resp.Status)
|
||||
}
|
||||
95
example/object/completeMultipartUpload.go
Normal file
95
example/object/completeMultipartUpload.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func initUpload(c *cos.Client, name string) *cos.InitiateMultipartUploadResult {
|
||||
v, _, err := c.Object.InitiateMultipartUpload(context.Background(), name, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("%#v\n", v)
|
||||
return v
|
||||
}
|
||||
|
||||
func uploadPart(c *cos.Client, name string, uploadID string, blockSize, n int) string {
|
||||
|
||||
b := make([]byte, blockSize)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
s := fmt.Sprintf("%X", b)
|
||||
f := strings.NewReader(s)
|
||||
|
||||
resp, err := c.Object.UploadPart(
|
||||
context.Background(), name, uploadID, n, f, nil,
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("%s\n", resp.Status)
|
||||
return resp.Header.Get("Etag")
|
||||
}
|
||||
|
||||
func main() {
|
||||
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: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: false,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
name := "test/test_complete_upload.go"
|
||||
up := initUpload(c, name)
|
||||
uploadID := up.UploadID
|
||||
blockSize := 1024 * 1024 * 3
|
||||
|
||||
opt := &cos.CompleteMultipartUploadOptions{}
|
||||
for i := 1; i < 5; i++ {
|
||||
etag := uploadPart(c, name, uploadID, blockSize, i)
|
||||
opt.Parts = append(opt.Parts, cos.Object{
|
||||
PartNumber: i, ETag: etag},
|
||||
)
|
||||
}
|
||||
|
||||
c = cos.NewClient(b, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
SecretID: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: true,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
v, resp, err := c.Object.CompleteMultipartUpload(
|
||||
context.Background(), name, uploadID, opt,
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("%s\n", resp.Status)
|
||||
fmt.Printf("%#v\n", v)
|
||||
fmt.Printf("%s\n", v.Location)
|
||||
}
|
||||
63
example/object/copy.go
Normal file
63
example/object/copy.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"net/http"
|
||||
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"time"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func main() {
|
||||
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: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: true,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
source := "test/objectMove1.go"
|
||||
expected := "test"
|
||||
f := strings.NewReader(expected)
|
||||
|
||||
_, err := c.Object.Put(context.Background(), source, f, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
soruceURL := fmt.Sprintf("%s/%s", u.Host, source)
|
||||
dest := fmt.Sprintf("test/objectMove_%d.go", time.Now().Nanosecond())
|
||||
//opt := &cos.ObjectCopyOptions{}
|
||||
res, _, err := c.Object.Copy(context.Background(), dest, soruceURL, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("%+v\n\n", res)
|
||||
|
||||
resp, err := c.Object.Get(context.Background(), dest, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
bs, _ := ioutil.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
result := string(bs)
|
||||
if result != expected {
|
||||
panic(fmt.Sprintf("%s != %s", result, expected))
|
||||
}
|
||||
}
|
||||
36
example/object/delete.go
Normal file
36
example/object/delete.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func main() {
|
||||
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: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: true,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
name := "test/objectPut.go"
|
||||
|
||||
_, err := c.Object.Delete(context.Background(), name)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
102
example/object/deleteMultiple.go
Normal file
102
example/object/deleteMultiple.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"bytes"
|
||||
"io"
|
||||
|
||||
"math/rand"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func genBigData(blockSize int) []byte {
|
||||
b := make([]byte, blockSize)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func uploadMulti(c *cos.Client) []string {
|
||||
names := []string{}
|
||||
data := genBigData(1024 * 1024 * 1)
|
||||
ctx := context.Background()
|
||||
var r io.Reader
|
||||
var name string
|
||||
n := 3
|
||||
|
||||
for n > 0 {
|
||||
name = fmt.Sprintf("test/test_multi_delete_%s", time.Now().Format(time.RFC3339))
|
||||
r = bytes.NewReader(data)
|
||||
|
||||
c.Object.Put(ctx, name, r, nil)
|
||||
names = append(names, name)
|
||||
n--
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
func main() {
|
||||
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: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: false,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
ctx := context.Background()
|
||||
|
||||
names := uploadMulti(c)
|
||||
names = append(names, []string{"a", "b", "c", "a+bc/xx&?+# "}...)
|
||||
obs := []cos.Object{}
|
||||
for _, v := range names {
|
||||
obs = append(obs, cos.Object{Key: v})
|
||||
}
|
||||
//sha1 := ""
|
||||
opt := &cos.ObjectDeleteMultiOptions{
|
||||
Objects: obs,
|
||||
//XCosSha1: sha1,
|
||||
//Quiet: true,
|
||||
}
|
||||
|
||||
c = cos.NewClient(b, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
SecretID: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: true,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
v, _, err := c.Object.DeleteMulti(ctx, opt)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for _, x := range v.DeletedObjects {
|
||||
fmt.Printf("deleted %s\n", x.Key)
|
||||
}
|
||||
for _, x := range v.Errors {
|
||||
fmt.Printf("error %s, %s, %s\n", x.Key, x.Code, x.Message)
|
||||
}
|
||||
}
|
||||
54
example/object/get.go
Normal file
54
example/object/get.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"io/ioutil"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func main() {
|
||||
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: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: true,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
name := "test/hello.txt"
|
||||
resp, err := c.Object.Get(context.Background(), name, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
bs, _ := ioutil.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
fmt.Printf("%s\n", string(bs))
|
||||
|
||||
// range
|
||||
opt := &cos.ObjectGetOptions{
|
||||
ResponseContentType: "text/html",
|
||||
Range: "bytes=0-3",
|
||||
}
|
||||
resp, err = c.Object.Get(context.Background(), name, opt)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
bs, _ = ioutil.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
fmt.Printf("%s\n", string(bs))
|
||||
}
|
||||
40
example/object/getACL.go
Normal file
40
example/object/getACL.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func main() {
|
||||
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: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: true,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
name := "test/hello.txt"
|
||||
v, _, err := c.Object.GetACL(context.Background(), name)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for _, a := range v.AccessControlList {
|
||||
fmt.Printf("%s, %s, %s\n", a.Grantee.Type, a.Grantee.ID, a.Permission)
|
||||
}
|
||||
|
||||
}
|
||||
45
example/object/getAnonymous.go
Normal file
45
example/object/getAnonymous.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
)
|
||||
|
||||
func upload(c *cos.Client, name string) {
|
||||
f := strings.NewReader("test")
|
||||
f = strings.NewReader("test xxx")
|
||||
opt := &cos.ObjectPutOptions{
|
||||
ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
|
||||
ContentType: "text/html",
|
||||
},
|
||||
ACLHeaderOptions: &cos.ACLHeaderOptions{
|
||||
XCosACL: "public-read",
|
||||
},
|
||||
}
|
||||
c.Object.Put(context.Background(), name, f, opt)
|
||||
return
|
||||
}
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{BucketURL: u}
|
||||
c := cos.NewClient(b, nil)
|
||||
|
||||
name := "test/anonymous_get.go"
|
||||
upload(c, name)
|
||||
|
||||
resp, err := c.Object.Get(context.Background(), name, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return
|
||||
}
|
||||
bs, _ := ioutil.ReadAll(resp.Body)
|
||||
defer resp.Body.Close()
|
||||
fmt.Printf("%s\n", string(bs))
|
||||
}
|
||||
35
example/object/head.go
Normal file
35
example/object/head.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func main() {
|
||||
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: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: true,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
name := "test/hello.txt"
|
||||
_, err := c.Object.Head(context.Background(), name, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
38
example/object/initiateMultipartUpload.go
Normal file
38
example/object/initiateMultipartUpload.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func main() {
|
||||
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: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: true,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
name := "test_multipart" + time.Now().Format(time.RFC3339)
|
||||
v, _, err := c.Object.InitiateMultipartUpload(context.Background(), name, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("%s\n", v.UploadID)
|
||||
}
|
||||
81
example/object/listParts.go
Normal file
81
example/object/listParts.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func initUpload(c *cos.Client, name string) *cos.InitiateMultipartUploadResult {
|
||||
v, _, err := c.Object.InitiateMultipartUpload(context.Background(), name, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("%#v\n", v)
|
||||
return v
|
||||
}
|
||||
|
||||
func uploadPart(c *cos.Client, name string, uploadID string, blockSize, n int) string {
|
||||
|
||||
b := make([]byte, blockSize)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
s := fmt.Sprintf("%X", b)
|
||||
f := strings.NewReader(s)
|
||||
|
||||
resp, err := c.Object.UploadPart(
|
||||
context.Background(), name, uploadID, n, f, nil,
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("%s\n", resp.Status)
|
||||
return resp.Header.Get("Etag")
|
||||
}
|
||||
|
||||
func main() {
|
||||
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: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: false,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
name := "test/test_list_parts.go"
|
||||
up := initUpload(c, name)
|
||||
uploadID := up.UploadID
|
||||
ctx := context.Background()
|
||||
blockSize := 1024 * 1024 * 3
|
||||
|
||||
for i := 1; i < 5; i++ {
|
||||
uploadPart(c, name, uploadID, blockSize, i)
|
||||
}
|
||||
|
||||
v, _, err := c.Object.ListParts(ctx, name, uploadID)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return
|
||||
}
|
||||
for _, p := range v.Parts {
|
||||
fmt.Printf("%d, %s, %d\n", p.PartNumber, p.ETag, p.Size)
|
||||
}
|
||||
fmt.Printf("%s\n", v.Initiator.ID)
|
||||
fmt.Printf("%s\n", v.Owner.ID)
|
||||
}
|
||||
39
example/object/options.go
Normal file
39
example/object/options.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func main() {
|
||||
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: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: true,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
name := "test/hello.txt"
|
||||
opt := &cos.ObjectOptionsOptions{
|
||||
Origin: "http://www.qq.com",
|
||||
AccessControlRequestMethod: "PUT",
|
||||
}
|
||||
_, err := c.Object.Options(context.Background(), name, opt)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
54
example/object/put.go
Normal file
54
example/object/put.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func main() {
|
||||
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: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: true,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
name := "test/objectPut.go"
|
||||
f := strings.NewReader("test")
|
||||
|
||||
_, err := c.Object.Put(context.Background(), name, f, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
name = "test/put_option.go"
|
||||
f = strings.NewReader("test xxx")
|
||||
opt := &cos.ObjectPutOptions{
|
||||
ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
|
||||
ContentType: "text/html",
|
||||
},
|
||||
ACLHeaderOptions: &cos.ACLHeaderOptions{
|
||||
//XCosACL: "public-read",
|
||||
XCosACL: "private",
|
||||
},
|
||||
}
|
||||
_, err = c.Object.Put(context.Background(), name, f, opt)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
64
example/object/putACL.go
Normal file
64
example/object/putACL.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func main() {
|
||||
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: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: true,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
opt := &cos.ObjectPutACLOptions{
|
||||
Header: &cos.ACLHeaderOptions{
|
||||
XCosACL: "private",
|
||||
},
|
||||
}
|
||||
name := "test/hello.txt"
|
||||
_, err := c.Object.PutACL(context.Background(), name, opt)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// with body
|
||||
opt = &cos.ObjectPutACLOptions{
|
||||
Body: &cos.ACLXml{
|
||||
Owner: &cos.Owner{
|
||||
ID: "qcs::cam::uin/100000760461:uin/100000760461",
|
||||
},
|
||||
AccessControlList: []cos.ACLGrant{
|
||||
{
|
||||
Grantee: &cos.ACLGrantee{
|
||||
Type: "RootAccount",
|
||||
ID: "qcs::cam::uin/100000760461:uin/100000760461",
|
||||
},
|
||||
|
||||
Permission: "FULL_CONTROL",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
_, err = c.Object.PutACL(context.Background(), name, opt)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
53
example/object/uploadFile.go
Normal file
53
example/object/uploadFile.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"net/http"
|
||||
|
||||
"fmt"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func main() {
|
||||
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: os.Getenv("COS_Key"),
|
||||
SecretKey: os.Getenv("COS_Secret"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: false,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
name := "test/uploadFile.go"
|
||||
f, err := os.Open(os.Args[0])
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
s, err := f.Stat()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(s.Size())
|
||||
opt := &cos.ObjectPutOptions{
|
||||
ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
|
||||
ContentLength: int(s.Size()),
|
||||
},
|
||||
}
|
||||
//opt.ContentLength = int(s.Size())
|
||||
|
||||
_, err = c.Object.Put(context.Background(), name, f, opt)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
53
example/object/uploadPart.go
Normal file
53
example/object/uploadPart.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func initUpload(c *cos.Client, name string) *cos.InitiateMultipartUploadResult {
|
||||
v, _, err := c.Object.InitiateMultipartUpload(context.Background(), name, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("%#v\n", v)
|
||||
return v
|
||||
}
|
||||
|
||||
func main() {
|
||||
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: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: true,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
name := "test/test_multi_upload.go"
|
||||
up := initUpload(c, name)
|
||||
uploadID := up.UploadID
|
||||
|
||||
f := strings.NewReader("test heoo")
|
||||
_, err := c.Object.UploadPart(
|
||||
context.Background(), name, uploadID, 1, f, nil,
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user