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.

410 lines
10 KiB

4 years ago
6 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
  1. package cos
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/base64"
  6. "encoding/xml"
  7. "fmt"
  8. "io"
  9. "io/ioutil"
  10. "net/http"
  11. "net/url"
  12. "reflect"
  13. "strings"
  14. "text/template"
  15. "strconv"
  16. "github.com/google/go-querystring/query"
  17. "github.com/mozillazg/go-httpheader"
  18. )
  19. const (
  20. // Version current go sdk version
  21. Version = "0.7.20"
  22. userAgent = "cos-go-sdk-v5/" + Version
  23. contentTypeXML = "application/xml"
  24. defaultServiceBaseURL = "http://service.cos.myqcloud.com"
  25. )
  26. var bucketURLTemplate = template.Must(
  27. template.New("bucketURLFormat").Parse(
  28. "{{.Schema}}://{{.BucketName}}.cos.{{.Region}}.myqcloud.com",
  29. ),
  30. )
  31. // BaseURL 访问各 API 所需的基础 URL
  32. type BaseURL struct {
  33. // 访问 bucket, object 相关 API 的基础 URL(不包含 path 部分): http://example.com
  34. BucketURL *url.URL
  35. // 访问 service API 的基础 URL(不包含 path 部分): http://example.com
  36. ServiceURL *url.URL
  37. // 访问 job API 的基础 URL (不包含 path 部分): http://example.com
  38. BatchURL *url.URL
  39. // 访问 CI 的基础 URL
  40. CIURL *url.URL
  41. }
  42. // NewBucketURL 生成 BaseURL 所需的 BucketURL
  43. //
  44. // bucketName: bucket名称, bucket的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
  45. // Region: 区域代码: ap-beijing-1,ap-beijing,ap-shanghai,ap-guangzhou...
  46. // secure: 是否使用 https
  47. func NewBucketURL(bucketName, region string, secure bool) *url.URL {
  48. schema := "https"
  49. if !secure {
  50. schema = "http"
  51. }
  52. w := bytes.NewBuffer(nil)
  53. bucketURLTemplate.Execute(w, struct {
  54. Schema string
  55. BucketName string
  56. Region string
  57. }{
  58. schema, bucketName, region,
  59. })
  60. u, _ := url.Parse(w.String())
  61. return u
  62. }
  63. // Client is a client manages communication with the COS API.
  64. type Client struct {
  65. client *http.Client
  66. Host string
  67. UserAgent string
  68. BaseURL *BaseURL
  69. common service
  70. Service *ServiceService
  71. Bucket *BucketService
  72. Object *ObjectService
  73. Batch *BatchService
  74. CI *CIService
  75. }
  76. type service struct {
  77. client *Client
  78. }
  79. // NewClient returns a new COS API client.
  80. func NewClient(uri *BaseURL, httpClient *http.Client) *Client {
  81. if httpClient == nil {
  82. httpClient = &http.Client{}
  83. }
  84. baseURL := &BaseURL{}
  85. if uri != nil {
  86. baseURL.BucketURL = uri.BucketURL
  87. baseURL.ServiceURL = uri.ServiceURL
  88. baseURL.BatchURL = uri.BatchURL
  89. baseURL.CIURL = uri.CIURL
  90. }
  91. if baseURL.ServiceURL == nil {
  92. baseURL.ServiceURL, _ = url.Parse(defaultServiceBaseURL)
  93. }
  94. c := &Client{
  95. client: httpClient,
  96. UserAgent: userAgent,
  97. BaseURL: baseURL,
  98. }
  99. c.common.client = c
  100. c.Service = (*ServiceService)(&c.common)
  101. c.Bucket = (*BucketService)(&c.common)
  102. c.Object = (*ObjectService)(&c.common)
  103. c.Batch = (*BatchService)(&c.common)
  104. c.CI = (*CIService)(&c.common)
  105. return c
  106. }
  107. func (c *Client) newRequest(ctx context.Context, baseURL *url.URL, uri, method string, body interface{}, optQuery interface{}, optHeader interface{}) (req *http.Request, err error) {
  108. uri, err = addURLOptions(uri, optQuery)
  109. if err != nil {
  110. return
  111. }
  112. u, _ := url.Parse(uri)
  113. urlStr := baseURL.ResolveReference(u).String()
  114. var reader io.Reader
  115. contentType := ""
  116. contentMD5 := ""
  117. if body != nil {
  118. // 上传文件
  119. if r, ok := body.(io.Reader); ok {
  120. reader = r
  121. } else {
  122. b, err := xml.Marshal(body)
  123. if err != nil {
  124. return nil, err
  125. }
  126. contentType = contentTypeXML
  127. reader = bytes.NewReader(b)
  128. contentMD5 = base64.StdEncoding.EncodeToString(calMD5Digest(b))
  129. }
  130. }
  131. req, err = http.NewRequest(method, urlStr, reader)
  132. if err != nil {
  133. return
  134. }
  135. req.Header, err = addHeaderOptions(req.Header, optHeader)
  136. if err != nil {
  137. return
  138. }
  139. if v := req.Header.Get("Content-Length"); req.ContentLength == 0 && v != "" && v != "0" {
  140. req.ContentLength, _ = strconv.ParseInt(v, 10, 64)
  141. }
  142. if contentMD5 != "" {
  143. req.Header["Content-MD5"] = []string{contentMD5}
  144. }
  145. if c.UserAgent != "" {
  146. req.Header.Set("User-Agent", c.UserAgent)
  147. }
  148. if req.Header.Get("Content-Type") == "" && contentType != "" {
  149. req.Header.Set("Content-Type", contentType)
  150. }
  151. if c.Host != "" {
  152. req.Host = c.Host
  153. }
  154. return
  155. }
  156. func (c *Client) doAPI(ctx context.Context, req *http.Request, result interface{}, closeBody bool) (*Response, error) {
  157. req = req.WithContext(ctx)
  158. resp, err := c.client.Do(req)
  159. if err != nil {
  160. // If we got an error, and the context has been canceled,
  161. // the context's error is probably more useful.
  162. select {
  163. case <-ctx.Done():
  164. return nil, ctx.Err()
  165. default:
  166. }
  167. return nil, err
  168. }
  169. defer func() {
  170. if closeBody {
  171. // Close the body to let the Transport reuse the connection
  172. io.Copy(ioutil.Discard, resp.Body)
  173. resp.Body.Close()
  174. }
  175. }()
  176. response := newResponse(resp)
  177. err = checkResponse(resp)
  178. if err != nil {
  179. // even though there was an error, we still return the response
  180. // in case the caller wants to inspect it further
  181. return response, err
  182. }
  183. if result != nil {
  184. if w, ok := result.(io.Writer); ok {
  185. io.Copy(w, resp.Body)
  186. } else {
  187. err = xml.NewDecoder(resp.Body).Decode(result)
  188. if err == io.EOF {
  189. err = nil // ignore EOF errors caused by empty response body
  190. }
  191. }
  192. }
  193. return response, err
  194. }
  195. type sendOptions struct {
  196. // 基础 URL
  197. baseURL *url.URL
  198. // URL 中除基础 URL 外的剩余部分
  199. uri string
  200. // 请求方法
  201. method string
  202. body interface{}
  203. // url 查询参数
  204. optQuery interface{}
  205. // http header 参数
  206. optHeader interface{}
  207. // 用 result 反序列化 resp.Body
  208. result interface{}
  209. // 是否禁用自动调用 resp.Body.Close()
  210. // 自动调用 Close() 是为了能够重用连接
  211. disableCloseBody bool
  212. }
  213. func (c *Client) send(ctx context.Context, opt *sendOptions) (resp *Response, err error) {
  214. req, err := c.newRequest(ctx, opt.baseURL, opt.uri, opt.method, opt.body, opt.optQuery, opt.optHeader)
  215. if err != nil {
  216. return
  217. }
  218. resp, err = c.doAPI(ctx, req, opt.result, !opt.disableCloseBody)
  219. return
  220. }
  221. // addURLOptions adds the parameters in opt as URL query parameters to s. opt
  222. // must be a struct whose fields may contain "url" tags.
  223. func addURLOptions(s string, opt interface{}) (string, error) {
  224. v := reflect.ValueOf(opt)
  225. if v.Kind() == reflect.Ptr && v.IsNil() {
  226. return s, nil
  227. }
  228. u, err := url.Parse(s)
  229. if err != nil {
  230. return s, err
  231. }
  232. qs, err := query.Values(opt)
  233. if err != nil {
  234. return s, err
  235. }
  236. // 保留原有的参数,并且放在前面。因为 cos 的 url 路由是以第一个参数作为路由的
  237. // e.g. /?uploads
  238. q := u.RawQuery
  239. rq := qs.Encode()
  240. if q != "" {
  241. if rq != "" {
  242. u.RawQuery = fmt.Sprintf("%s&%s", q, qs.Encode())
  243. }
  244. } else {
  245. u.RawQuery = rq
  246. }
  247. return u.String(), nil
  248. }
  249. // addHeaderOptions adds the parameters in opt as Header fields to req. opt
  250. // must be a struct whose fields may contain "header" tags.
  251. func addHeaderOptions(header http.Header, opt interface{}) (http.Header, error) {
  252. v := reflect.ValueOf(opt)
  253. if v.Kind() == reflect.Ptr && v.IsNil() {
  254. return header, nil
  255. }
  256. h, err := httpheader.Header(opt)
  257. if err != nil {
  258. return nil, err
  259. }
  260. for key, values := range h {
  261. for _, value := range values {
  262. header.Add(key, value)
  263. }
  264. }
  265. return header, nil
  266. }
  267. // Owner defines Bucket/Object's owner
  268. type Owner struct {
  269. UIN string `xml:"uin,omitempty"`
  270. ID string `xml:",omitempty"`
  271. DisplayName string `xml:",omitempty"`
  272. }
  273. // Initiator same to the Owner struct
  274. type Initiator Owner
  275. // Response API 响应
  276. type Response struct {
  277. *http.Response
  278. }
  279. func newResponse(resp *http.Response) *Response {
  280. return &Response{
  281. Response: resp,
  282. }
  283. }
  284. // ACLHeaderOptions is the option of ACLHeader
  285. type ACLHeaderOptions struct {
  286. XCosACL string `header:"x-cos-acl,omitempty" url:"-" xml:"-"`
  287. XCosGrantRead string `header:"x-cos-grant-read,omitempty" url:"-" xml:"-"`
  288. XCosGrantWrite string `header:"x-cos-grant-write,omitempty" url:"-" xml:"-"`
  289. XCosGrantFullControl string `header:"x-cos-grant-full-control,omitempty" url:"-" xml:"-"`
  290. XCosGrantReadACP string `header:"x-cos-grant-read-acp,omitempty" url:"-" xml:"-"`
  291. XCosGrantWriteACP string `header:"x-cos-grant-write-acp,omitempty" url:"-" xml:"-"`
  292. }
  293. // ACLGrantee is the param of ACLGrant
  294. type ACLGrantee struct {
  295. Type string `xml:"type,attr"`
  296. UIN string `xml:"uin,omitempty"`
  297. URI string `xml:"URI,omitempty"`
  298. ID string `xml:",omitempty"`
  299. DisplayName string `xml:",omitempty"`
  300. SubAccount string `xml:"Subaccount,omitempty"`
  301. }
  302. // ACLGrant is the param of ACLXml
  303. type ACLGrant struct {
  304. Grantee *ACLGrantee
  305. Permission string
  306. }
  307. // ACLXml is the ACL body struct
  308. type ACLXml struct {
  309. XMLName xml.Name `xml:"AccessControlPolicy"`
  310. Owner *Owner
  311. AccessControlList []ACLGrant `xml:"AccessControlList>Grant,omitempty"`
  312. }
  313. func decodeACL(resp *Response, res *ACLXml) {
  314. ItemMap := map[string]string{
  315. "ACL": "x-cos-acl",
  316. "READ": "x-cos-grant-read",
  317. "WRITE": "x-cos-grant-write",
  318. "READ_ACP": "x-cos-grant-read-acp",
  319. "WRITE_ACP": "x-cos-grant-write-acp",
  320. "FULL_CONTROL": "x-cos-grant-full-control",
  321. }
  322. publicACL := make(map[string]int)
  323. resACL := make(map[string][]string)
  324. for _, item := range res.AccessControlList {
  325. if item.Grantee == nil {
  326. continue
  327. }
  328. if item.Grantee.ID == "qcs::cam::anyone:anyone" || item.Grantee.URI == "http://cam.qcloud.com/groups/global/AllUsers" {
  329. publicACL[item.Permission] = 1
  330. } else if item.Grantee.ID != res.Owner.ID {
  331. resACL[item.Permission] = append(resACL[item.Permission], "id=\""+item.Grantee.ID+"\"")
  332. }
  333. }
  334. if publicACL["FULL_CONTROL"] == 1 || (publicACL["READ"] == 1 && publicACL["WRITE"] == 1) {
  335. resACL["ACL"] = []string{"public-read-write"}
  336. } else if publicACL["READ"] == 1 {
  337. resACL["ACL"] = []string{"public-read"}
  338. } else {
  339. resACL["ACL"] = []string{"private"}
  340. }
  341. for item, header := range ItemMap {
  342. if len(resp.Header.Get(header)) > 0 || len(resACL[item]) == 0 {
  343. continue
  344. }
  345. resp.Header.Set(header, uniqueGrantID(resACL[item]))
  346. }
  347. }
  348. func uniqueGrantID(grantIDs []string) string {
  349. res := []string{}
  350. filter := make(map[string]int)
  351. for _, id := range grantIDs {
  352. if filter[id] != 0 {
  353. continue
  354. }
  355. filter[id] = 1
  356. res = append(res, id)
  357. }
  358. return strings.Join(res, ",")
  359. }