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.

68 lines
1.4 KiB

  1. package main
  2. import (
  3. "context"
  4. "encoding/xml"
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "os"
  9. "github.com/tencentyun/cos-go-sdk-v5"
  10. "github.com/tencentyun/cos-go-sdk-v5/debug"
  11. )
  12. func log_status(err error) {
  13. if err == nil {
  14. return
  15. }
  16. if cos.IsNotFoundError(err) {
  17. // WARN
  18. fmt.Println("Resource is not existed")
  19. } else if e, ok := cos.IsCOSError(err); ok {
  20. fmt.Printf("Code: %v\n", e.Code)
  21. fmt.Printf("Message: %v\n", e.Message)
  22. fmt.Printf("Resource: %v\n", e.Resource)
  23. fmt.Printf("RequestId: %v\n", e.RequestID)
  24. // ERROR
  25. } else {
  26. fmt.Println(err)
  27. // ERROR
  28. }
  29. }
  30. func main() {
  31. u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
  32. b := &cos.BaseURL{
  33. BucketURL: u,
  34. }
  35. c := cos.NewClient(b, &http.Client{
  36. Transport: &cos.AuthorizationTransport{
  37. SecretID: os.Getenv("COS_SECRETID"),
  38. SecretKey: os.Getenv("COS_SECRETKEY"),
  39. Transport: &debug.DebugRequestTransport{
  40. RequestHeader: true,
  41. RequestBody: true,
  42. ResponseHeader: true,
  43. ResponseBody: true,
  44. },
  45. },
  46. })
  47. opt := &cos.BucketPutEncryptionOptions{
  48. XMLName: xml.Name{Local: "ServerSideEncryptionConfiguration"},
  49. Rule: &cos.BucketEncryptionConfiguration{
  50. SSEAlgorithm: "AES256",
  51. },
  52. }
  53. _, err := c.Bucket.PutEncryption(context.Background(), opt)
  54. log_status(err)
  55. res, _, err := c.Bucket.GetEncryption(context.Background())
  56. log_status(err)
  57. fmt.Printf("%+v\n", res)
  58. _, err = c.Bucket.DeleteEncryption(context.Background())
  59. log_status(err)
  60. }