You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
1.7 KiB

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "os"
  7. "net/http"
  8. "github.com/tencentyun/cos-go-sdk-v5"
  9. "github.com/tencentyun/cos-go-sdk-v5/debug"
  10. )
  11. func log_status(err error) {
  12. if err == nil {
  13. return
  14. }
  15. if cos.IsNotFoundError(err) {
  16. // WARN
  17. fmt.Println("WARN: Resource is not existed")
  18. } else if e, ok := cos.IsCOSError(err); ok {
  19. fmt.Printf("ERROR: Code: %v\n", e.Code)
  20. fmt.Printf("ERROR: Message: %v\n", e.Message)
  21. fmt.Printf("ERROR: Resource: %v\n", e.Resource)
  22. fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
  23. // ERROR
  24. } else {
  25. fmt.Printf("ERROR: %v\n", err)
  26. // ERROR
  27. }
  28. }
  29. func main() {
  30. u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
  31. b := &cos.BaseURL{BucketURL: u}
  32. c := cos.NewClient(b, &http.Client{
  33. Transport: &cos.AuthorizationTransport{
  34. SecretID: os.Getenv("COS_SECRETID"),
  35. SecretKey: os.Getenv("COS_SECRETKEY"),
  36. Transport: &debug.DebugRequestTransport{
  37. RequestHeader: true,
  38. RequestBody: true,
  39. ResponseHeader: true,
  40. ResponseBody: true,
  41. },
  42. },
  43. })
  44. opt := &cos.ObjectPutACLOptions{
  45. Header: &cos.ACLHeaderOptions{
  46. XCosACL: "private",
  47. },
  48. }
  49. name := "test/hello.txt"
  50. _, err := c.Object.PutACL(context.Background(), name, opt)
  51. log_status(err)
  52. // with body
  53. opt = &cos.ObjectPutACLOptions{
  54. Body: &cos.ACLXml{
  55. Owner: &cos.Owner{
  56. ID: "qcs::cam::uin/100000760461:uin/100000760461",
  57. },
  58. AccessControlList: []cos.ACLGrant{
  59. {
  60. Grantee: &cos.ACLGrantee{
  61. Type: "RootAccount",
  62. ID: "qcs::cam::uin/100000760461:uin/100000760461",
  63. },
  64. Permission: "FULL_CONTROL",
  65. },
  66. },
  67. },
  68. }
  69. _, err = c.Object.PutACL(context.Background(), name, opt)
  70. log_status(err)
  71. }