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.

63 lines
1.5 KiB

5 years ago
  1. package main
  2. import (
  3. "context"
  4. "net/http"
  5. "net/url"
  6. "os"
  7. "github.com/tencentyun/cos-go-sdk-v5"
  8. "github.com/tencentyun/cos-go-sdk-v5/debug"
  9. )
  10. func main() {
  11. u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
  12. b := &cos.BaseURL{
  13. BucketURL: u,
  14. }
  15. c := cos.NewClient(b, &http.Client{
  16. Transport: &cos.AuthorizationTransport{
  17. SecretID: os.Getenv("COS_SECRETID"),
  18. SecretKey: os.Getenv("COS_SECRETKEY"),
  19. Transport: &debug.DebugRequestTransport{
  20. RequestHeader: true,
  21. RequestBody: true,
  22. ResponseHeader: true,
  23. ResponseBody: true,
  24. },
  25. },
  26. })
  27. opt := &cos.BucketPutPolicyOptions{
  28. Version: "2.0",
  29. Statement: []cos.BucketStatement{
  30. {
  31. Principal: map[string][]string{
  32. "qcs": []string{
  33. "qcs::cam::uin/100000000001:uin/100000000011", //替换成您想授予权限的账户uin
  34. },
  35. },
  36. Action: []string{
  37. "name/cos:GetObject",
  38. },
  39. Effect: "allow",
  40. Resource: []string{
  41. //这里改成允许的路径前缀,可以根据自己网站的用户登录态判断允许上传的具体路径,例子: a.jpg 或者 a/* 或者 * (使用通配符*存在重大安全风险, 请谨慎评估使用)
  42. "qcs::cos:ap-guangzhou:uid/1259654469:test-1259654469/exampleobject",
  43. },
  44. Condition: map[string]map[string]interface{}{
  45. "ip_not_equal": map[string]interface{}{
  46. "qcs:ip": []string{
  47. "192.168.1.1",
  48. },
  49. },
  50. },
  51. },
  52. },
  53. }
  54. _, err := c.Bucket.PutPolicy(context.Background(), opt)
  55. if err != nil {
  56. panic(err)
  57. }
  58. }