|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "net/url" |
| 8 | + "os" |
| 9 | + |
| 10 | + "github.com/tencentyun/cos-go-sdk-v5" |
| 11 | + "github.com/tencentyun/cos-go-sdk-v5/debug" |
| 12 | +) |
| 13 | + |
| 14 | +func log_status(err error) { |
| 15 | + if err == nil { |
| 16 | + return |
| 17 | + } |
| 18 | + if cos.IsNotFoundError(err) { |
| 19 | + // WARN |
| 20 | + fmt.Println("WARN: Resource is not existed") |
| 21 | + } else if e, ok := cos.IsCOSError(err); ok { |
| 22 | + fmt.Printf("ERROR: Code: %v\n", e.Code) |
| 23 | + fmt.Printf("ERROR: Message: %v\n", e.Message) |
| 24 | + fmt.Printf("ERROR: Resource: %v\n", e.Resource) |
| 25 | + fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) |
| 26 | + // ERROR |
| 27 | + } else { |
| 28 | + fmt.Printf("ERROR: %v\n", err) |
| 29 | + // ERROR |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func main() { |
| 34 | + u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com") |
| 35 | + b := &cos.BaseURL{BucketURL: u} |
| 36 | + c := cos.NewClient(b, &http.Client{ |
| 37 | + Transport: &cos.AuthorizationTransport{ |
| 38 | + SecretID: os.Getenv("COS_SECRETID"), |
| 39 | + SecretKey: os.Getenv("COS_SECRETKEY"), |
| 40 | + Transport: &debug.DebugRequestTransport{ |
| 41 | + RequestHeader: true, |
| 42 | + RequestBody: true, |
| 43 | + ResponseHeader: true, |
| 44 | + ResponseBody: true, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }) |
| 48 | + opt := &cos.ObjectGetOptions{ |
| 49 | + CIProcess: "sensitive-content-recognition", |
| 50 | + CIDetectType: "porn,terrorist,politics,ads", |
| 51 | + } |
| 52 | + |
| 53 | + // Case1 Download object into ReadCloser(). the body needs to be closed |
| 54 | + name := "test.jpg" |
| 55 | + resp, err := c.Object.Get(context.Background(), name, opt) |
| 56 | + log_status(err) |
| 57 | + resp.Body.Close() |
| 58 | + res := cos.GetRecognitionResult(resp.Body) |
| 59 | + if res != nil { |
| 60 | + fmt.Printf("%+v\n", res) |
| 61 | + } |
| 62 | +} |
0 commit comments