|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "net/url" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "net/http" |
| 12 | + |
| 13 | + "github.com/tencentyun/cos-go-sdk-v5" |
| 14 | + "github.com/tencentyun/cos-go-sdk-v5/debug" |
| 15 | +) |
| 16 | + |
| 17 | +func log_status(err error) { |
| 18 | + if err == nil { |
| 19 | + return |
| 20 | + } |
| 21 | + if cos.IsNotFoundError(err) { |
| 22 | + // WARN |
| 23 | + fmt.Println("WARN: Resource is not existed") |
| 24 | + } else if e, ok := cos.IsCOSError(err); ok { |
| 25 | + fmt.Printf("ERROR: Code: %v\n", e.Code) |
| 26 | + fmt.Printf("ERROR: Message: %v\n", e.Message) |
| 27 | + fmt.Printf("ERROR: Resource: %v\n", e.Resource) |
| 28 | + fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) |
| 29 | + // ERROR |
| 30 | + } else { |
| 31 | + fmt.Printf("ERROR: %v\n", err) |
| 32 | + // ERROR |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func main() { |
| 37 | + // 存储桶名称,由bucketname-appid 组成,appid必须填入,可以在COS控制台查看存储桶名称。 https://console.cloud.tencent.com/cos5/bucket |
| 38 | + // 替换为用户的 region,存储桶region可以在COS控制台“存储桶概览”查看 https://console.cloud.tencent.com/ ,关于地域的详情见 https://cloud.tencent.com/document/product/436/6224 。 |
| 39 | + u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com") |
| 40 | + b := &cos.BaseURL{BucketURL: u} |
| 41 | + c := cos.NewClient(b, &http.Client{ |
| 42 | + Transport: &cos.AuthorizationTransport{ |
| 43 | + // 通过环境变量获取密钥 |
| 44 | + // 环境变量 COS_SECRETID 表示用户的 SecretId,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi |
| 45 | + SecretID: os.Getenv("COS_SECRETID"), |
| 46 | + // 环境变量 COS_SECRETKEY 表示用户的 SecretKey,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi |
| 47 | + SecretKey: os.Getenv("COS_SECRETKEY"), |
| 48 | + // Debug 模式,把对应 请求头部、请求内容、响应头部、响应内容 输出到标准输出 |
| 49 | + Transport: &debug.DebugRequestTransport{ |
| 50 | + RequestHeader: true, |
| 51 | + // Notice when put a large file and set need the request body, might happend out of memory error. |
| 52 | + RequestBody: false, |
| 53 | + ResponseHeader: true, |
| 54 | + ResponseBody: false, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }) |
| 58 | + |
| 59 | + // Case1 上传对象 |
| 60 | + target := "example" |
| 61 | + _, err := c.Object.Put(context.Background(), target, strings.NewReader("test"), nil) |
| 62 | + log_status(err) |
| 63 | + |
| 64 | + key := "sym" |
| 65 | + opt := &cos.ObjectPutSymlinkOptions{ |
| 66 | + SymlinkTarget: target, |
| 67 | + } |
| 68 | + _, err = c.Object.PutSymlink(context.Background(), key, opt) |
| 69 | + log_status(err) |
| 70 | + |
| 71 | + res, _, err := c.Object.GetSymlink(context.Background(), key, nil) |
| 72 | + log_status(err) |
| 73 | + fmt.Printf("res: %v\n", res) |
| 74 | + |
| 75 | + resp, err := c.Object.Get(context.Background(), key, nil) |
| 76 | + log_status(err) |
| 77 | + defer resp.Body.Close() |
| 78 | + |
| 79 | + bs, _ := ioutil.ReadAll(resp.Body) |
| 80 | + fmt.Printf("body: %v\n", string(bs)) |
| 81 | + |
| 82 | + _, err = c.Object.Delete(context.Background(), key, nil) |
| 83 | + log_status(err) |
| 84 | +} |
0 commit comments