go get -u github.com/tencentyun/cos-go-sdk-v5
func NewClient(uri *BaseURL, httpClient *http.Client) *Client
// BaseURL로 각 API에 필요한 기본 URL 액세스type BaseURL struct {// bucket, object 관련 API에 액세스하는 기본 URL(path 부분 미포함): https://examplebucket-1250000000.cos.<Region>.myqcloud.comBucketURL *url.URL// service API에 액세스하는 기본 URL(path 부분 미포함): https://cos.<Region>.myqcloud.comServiceURL *url.URL// Batch API에 액세스하는 기본 URL(path 부분 미포함): https://<UIN>.cos-control.<Region>.myqcloud.comBatchURL *url.URL// CI에 액세스하는 기본 URL(path 부분 미포함): https://examplebucket-1250000000.ci.<Region>.myqcloud.comCIURL *url.URL}
매개변수 이름 | 매개변수 설명 | 유형 | 필수 입력 여부 |
BucketURL | bucket, object 관련 API에 액세스하는 기본 URL(path 부분 미포함) | string | 예 |
ServiceURL | service API에 액세스하는 기본 URL(path 부분 미포함) | string | 아니요 |
BatchURL | Batch API에 액세스하는 기본 URL(path 부분 미포함) | string | 아니요 |
CIURL | CI에 액세스하는 기본 URL(path 부분 미포함) | string | 아니요 |
// examplebucket-1250000000과 COS_REGION을 실제 정보로 수정합니다.u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")// Get Service 쿼리에 사용, 모든 리전 기본값: service.cos.myqcloud.comsu, _ := url.Parse("https://cos.COS_REGION.myqcloud.com")b := &cos.BaseURL{BucketURL: u, ServiceURL: su}// 1. 영구 키client := cos.NewClient(b, &http.Client{Transport: &cos.AuthorizationTransport{SecretID: "SECRETID",SecretKey: "SECRETKEY",},})
// examplebucket-1250000000과 COS_REGION을 실제 정보로 수정합니다.u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")b := &cos.BaseURL{BucketURL: u}// 2. 임시 키client := cos.NewClient(b, &http.Client{Transport: &cos.AuthorizationTransport{SecretID: "SECRETID",SecretKey: "SECRETKEY",SessionToken: "SECRETTOKEN",},})if client != nil {// COS 요청 호출}
// 글로벌 가속 도메인으로 COS 액세스u, _ := url.Parse("http://<BucketName-APPID>.cos.accelerate.myqcloud.com")b := &cos.BaseURL{BucketURL: u}// 2. 임시 키client := cos.NewClient(b, &http.Client{Transport: &cos.AuthorizationTransport{SecretID: "SECRETID",SecretKey: "SECRETKEY",SessionToken: "SECRETTOKEN",},})
// examplebucket-1250000000과 COS_REGION을 실제 정보로 수정합니다.u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")b := &cos.BaseURL{BucketURL: u}// 2. 임시 키client := cos.NewClient(b, &http.Client{Transport: &cos.AuthorizationTransport{SecretID: "SECRETID",SecretKey: "SECRETKEY",SessionToken: "SECRETTOKEN",},})// CRC64 인증 비활성화client.Conf.EnableCRC = false
package mainimport ("context""net/http""net/url""os""github.com/tencentyun/cos-go-sdk-v5")func main() {// examplebucket-1250000000과 COS_REGION을 실제 정보로 수정합니다.u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")b := &cos.BaseURL{BucketURL: u}c := cos.NewClient(b, &http.Client{Transport: &cos.AuthorizationTransport{SecretID: "SECRETID",SecretKey: "SECRETKEY",},})_, err := c.Bucket.Put(context.Background(), nil)if err != nil {panic(err)}}
package mainimport ("context""fmt""net/http""os""github.com/tencentyun/cos-go-sdk-v5")func main() {c := cos.NewClient(nil, &http.Client{Transport: &cos.AuthorizationTransport{SecretID: "SECRETID",SecretKey: "SECRETKEY",},})s, _, err := c.Service.Get(context.Background())if err != nil {panic(err)}for _, b := range s.Buckets {fmt.Printf("%#v\\n", b)}}
package mainimport ("context""net/http""net/url""os""strings""github.com/tencentyun/cos-go-sdk-v5")func main() {// examplebucket-1250000000과 COS_REGION을 실제 정보로 수정합니다.u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")b := &cos.BaseURL{BucketURL: u}c := cos.NewClient(b, &http.Client{Transport: &cos.AuthorizationTransport{SecretID: "SECRETID",SecretKey: "SECRETKEY",},})// 객체 키(Key)는 버킷 내 객체의 고유 식별자입니다.// 예를 들어, 객체의 액세스 도메인 `examplebucket-1250000000.cos.COS_REGION.myqcloud.com/test/objectPut.go`에서 객체 키는 test/objectPut.go입니다.name := "test/objectPut.go"// 1. 문자열로 객체 업로드f := strings.NewReader("test")_, err := c.Object.Put(context.Background(), name, f, nil)if err != nil {panic(err)}// 2. 로컬 파일로 객체 업로드_, err = c.Object.PutFromFile(context.Background(), name, "../test", nil)if err != nil {panic(err)}// 3. 파일 스트림으로 객체 업로드fd, err := os.Open("./test")if err != nil {panic(err)}defer fd.Close()_, err = c.Object.Put(context.Background(), name, fd, nil)if err != nil {panic(err)}}
package mainimport ("context""fmt""net/http""net/url""os""github.com/tencentyun/cos-go-sdk-v5")func main() {// examplebucket-1250000000과 COS_REGION을 실제 정보로 수정합니다.u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")b := &cos.BaseURL{BucketURL: u}c := cos.NewClient(b, &http.Client{Transport: &cos.AuthorizationTransport{SecretID: "SECRETID",SecretKey: "SECRETKEY",},})opt := &cos.BucketGetOptions{Prefix: "test",MaxKeys: 3,}v, _, err := c.Bucket.Get(context.Background(), opt)if err != nil {panic(err)}for _, c := range v.Contents {fmt.Printf("%s, %d\\n", c.Key, c.Size)}}
package mainimport ("context""fmt""io/ioutil""net/http""net/url""os""github.com/tencentyun/cos-go-sdk-v5")func main() {// examplebucket-1250000000과 COS_REGION을 실제 정보로 수정합니다.u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")b := &cos.BaseURL{BucketURL: u}c := cos.NewClient(b, &http.Client{Transport: &cos.AuthorizationTransport{SecretID: "SECRETID",SecretKey: "SECRETKEY",},})// 1. 응답 본문으로 객체 가져오기name := "test/objectPut.go"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))// 2. 로컬 파일에 객체 가져오기_, err = c.Object.GetToFile(context.Background(), name, "exampleobject", nil)if err != nil {panic(err)}}
package mainimport ("context""net/http""net/url""os""github.com/tencentyun/cos-go-sdk-v5")func main() {// examplebucket-1250000000과 COS_REGION을 실제 정보로 수정합니다.u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")b := &cos.BaseURL{BucketURL: u}c := cos.NewClient(b, &http.Client{Transport: &cos.AuthorizationTransport{SecretID: "SECRETID",SecretKey: "SECRETKEY",},})name := "test/objectPut.go"_, err := c.Object.Delete(context.Background(), name)if err != nil {panic(err)}}
피드백