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.

74 lines
1.8 KiB

3 years ago
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "os"
  7. "strings"
  8. "time"
  9. "net/http"
  10. "github.com/tencentyun/cos-go-sdk-v5"
  11. "github.com/tencentyun/cos-go-sdk-v5/debug"
  12. )
  13. func log_status(err error) {
  14. if err == nil {
  15. return
  16. }
  17. if cos.IsNotFoundError(err) {
  18. // WARN
  19. fmt.Println("WARN: Resource is not existed")
  20. } else if e, ok := cos.IsCOSError(err); ok {
  21. fmt.Printf("ERROR: Code: %v\n", e.Code)
  22. fmt.Printf("ERROR: Message: %v\n", e.Message)
  23. fmt.Printf("ERROR: Resource: %v\n", e.Resource)
  24. fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
  25. // ERROR
  26. } else {
  27. fmt.Printf("ERROR: %v\n", err)
  28. // ERROR
  29. }
  30. }
  31. func main() {
  32. u, _ := url.Parse("http://test-1259654469.cos.ap-guangzhou.myqcloud.com")
  33. b := &cos.BaseURL{BucketURL: u}
  34. c := cos.NewClient(b, &http.Client{
  35. Transport: &cos.AuthorizationTransport{
  36. SecretID: os.Getenv("COS_SECRETID"),
  37. SecretKey: os.Getenv("COS_SECRETKEY"),
  38. Transport: &debug.DebugRequestTransport{
  39. RequestHeader: true,
  40. // Notice when put a large file and set need the request body, might happend out of memory error.
  41. RequestBody: false,
  42. ResponseHeader: true,
  43. ResponseBody: false,
  44. },
  45. },
  46. })
  47. opt := &cos.ObjectPutOptions{
  48. ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
  49. ContentType: "text/html",
  50. Listener: &cos.DefaultProgressListener{},
  51. },
  52. ACLHeaderOptions: &cos.ACLHeaderOptions{
  53. XCosACL: "private",
  54. },
  55. }
  56. name := "append" + time.Now().Format(time.RFC3339)
  57. str1 := "test append object 1"
  58. pos, _, err := c.Object.Append(context.Background(), name, 0, strings.NewReader(str1), opt)
  59. log_status(err)
  60. fmt.Printf("pos: %d\n", pos)
  61. str2 := "test append object 2"
  62. pos, _, err = c.Object.Append(context.Background(), name, pos, strings.NewReader(str2), opt)
  63. log_status(err)
  64. fmt.Printf("pos: %d\n", pos)
  65. }