select_object.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package oss
  2. import (
  3. "bytes"
  4. "encoding/xml"
  5. "hash/crc32"
  6. "io"
  7. "io/ioutil"
  8. "net/http"
  9. "os"
  10. "strings"
  11. )
  12. // CreateSelectCsvObjectMeta is Creating csv object meta
  13. //
  14. // key the object key.
  15. // csvMeta the csv file meta
  16. // options the options for create csv Meta of the object.
  17. //
  18. // MetaEndFrameCSV the csv file meta info
  19. // error it's nil if no error, otherwise it's an error object.
  20. //
  21. func (bucket Bucket) CreateSelectCsvObjectMeta(key string, csvMeta CsvMetaRequest, options ...Option) (MetaEndFrameCSV, error) {
  22. var endFrame MetaEndFrameCSV
  23. params := map[string]interface{}{}
  24. params["x-oss-process"] = "csv/meta"
  25. csvMeta.encodeBase64()
  26. bs, err := xml.Marshal(csvMeta)
  27. if err != nil {
  28. return endFrame, err
  29. }
  30. buffer := new(bytes.Buffer)
  31. buffer.Write(bs)
  32. resp, err := bucket.DoPostSelectObject(key, params, buffer, options...)
  33. if err != nil {
  34. return endFrame, err
  35. }
  36. defer resp.Body.Close()
  37. _, err = ioutil.ReadAll(resp)
  38. return resp.Frame.MetaEndFrameCSV, err
  39. }
  40. // CreateSelectJsonObjectMeta is Creating json object meta
  41. //
  42. // key the object key.
  43. // csvMeta the json file meta
  44. // options the options for create json Meta of the object.
  45. //
  46. // MetaEndFrameJSON the json file meta info
  47. // error it's nil if no error, otherwise it's an error object.
  48. //
  49. func (bucket Bucket) CreateSelectJsonObjectMeta(key string, jsonMeta JsonMetaRequest, options ...Option) (MetaEndFrameJSON, error) {
  50. var endFrame MetaEndFrameJSON
  51. params := map[string]interface{}{}
  52. params["x-oss-process"] = "json/meta"
  53. bs, err := xml.Marshal(jsonMeta)
  54. if err != nil {
  55. return endFrame, err
  56. }
  57. buffer := new(bytes.Buffer)
  58. buffer.Write(bs)
  59. resp, err := bucket.DoPostSelectObject(key, params, buffer, options...)
  60. if err != nil {
  61. return endFrame, err
  62. }
  63. defer resp.Body.Close()
  64. _, err = ioutil.ReadAll(resp)
  65. return resp.Frame.MetaEndFrameJSON, err
  66. }
  67. // SelectObject is the select object api, approve csv and json file.
  68. //
  69. // key the object key.
  70. // selectReq the request data for select object
  71. // options the options for select file of the object.
  72. //
  73. // o.ReadCloser reader instance for reading data from response. It must be called close() after the usage and only valid when error is nil.
  74. // error it's nil if no error, otherwise it's an error object.
  75. //
  76. func (bucket Bucket) SelectObject(key string, selectReq SelectRequest, options ...Option) (io.ReadCloser, error) {
  77. params := map[string]interface{}{}
  78. if selectReq.InputSerializationSelect.JsonBodyInput.JsonIsEmpty() {
  79. params["x-oss-process"] = "csv/select" // default select csv file
  80. } else {
  81. params["x-oss-process"] = "json/select"
  82. }
  83. selectReq.encodeBase64()
  84. bs, err := xml.Marshal(selectReq)
  85. if err != nil {
  86. return nil, err
  87. }
  88. buffer := new(bytes.Buffer)
  89. buffer.Write(bs)
  90. resp, err := bucket.DoPostSelectObject(key, params, buffer, options...)
  91. if err != nil {
  92. return nil, err
  93. }
  94. if selectReq.OutputSerializationSelect.EnablePayloadCrc != nil && *selectReq.OutputSerializationSelect.EnablePayloadCrc == true {
  95. resp.Frame.EnablePayloadCrc = true
  96. }
  97. resp.Frame.OutputRawData = strings.ToUpper(resp.Headers.Get("x-oss-select-output-raw")) == "TRUE"
  98. return resp, err
  99. }
  100. // DoPostSelectObject is the SelectObject/CreateMeta api, approve csv and json file.
  101. //
  102. // key the object key.
  103. // params the resource of oss approve csv/meta, json/meta, csv/select, json/select.
  104. // buf the request data trans to buffer.
  105. // options the options for select file of the object.
  106. //
  107. // SelectObjectResponse the response of select object.
  108. // error it's nil if no error, otherwise it's an error object.
  109. //
  110. func (bucket Bucket) DoPostSelectObject(key string, params map[string]interface{}, buf *bytes.Buffer, options ...Option) (*SelectObjectResponse, error) {
  111. resp, err := bucket.do("POST", key, params, options, buf, nil)
  112. if err != nil {
  113. return nil, err
  114. }
  115. result := &SelectObjectResponse{
  116. Body: resp.Body,
  117. StatusCode: resp.StatusCode,
  118. Frame: SelectObjectResult{},
  119. }
  120. result.Headers = resp.Headers
  121. // result.Frame = SelectObjectResult{}
  122. result.ReadTimeOut = bucket.GetConfig().Timeout
  123. // Progress
  124. listener := GetProgressListener(options)
  125. // CRC32
  126. crcCalc := crc32.NewIEEE()
  127. result.WriterForCheckCrc32 = crcCalc
  128. result.Body = TeeReader(resp.Body, nil, 0, listener, nil)
  129. err = CheckRespCode(resp.StatusCode, []int{http.StatusPartialContent, http.StatusOK})
  130. return result, err
  131. }
  132. // SelectObjectIntoFile is the selectObject to file api
  133. //
  134. // key the object key.
  135. // fileName saving file's name to localstation.
  136. // selectReq the request data for select object
  137. // options the options for select file of the object.
  138. //
  139. // error it's nil if no error, otherwise it's an error object.
  140. //
  141. func (bucket Bucket) SelectObjectIntoFile(key, fileName string, selectReq SelectRequest, options ...Option) error {
  142. tempFilePath := fileName + TempFileSuffix
  143. params := map[string]interface{}{}
  144. if selectReq.InputSerializationSelect.JsonBodyInput.JsonIsEmpty() {
  145. params["x-oss-process"] = "csv/select" // default select csv file
  146. } else {
  147. params["x-oss-process"] = "json/select"
  148. }
  149. selectReq.encodeBase64()
  150. bs, err := xml.Marshal(selectReq)
  151. if err != nil {
  152. return err
  153. }
  154. buffer := new(bytes.Buffer)
  155. buffer.Write(bs)
  156. resp, err := bucket.DoPostSelectObject(key, params, buffer, options...)
  157. if err != nil {
  158. return err
  159. }
  160. defer resp.Close()
  161. // If the local file does not exist, create a new one. If it exists, overwrite it.
  162. fd, err := os.OpenFile(tempFilePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, FilePermMode)
  163. if err != nil {
  164. return err
  165. }
  166. // Copy the data to the local file path.
  167. _, err = io.Copy(fd, resp)
  168. fd.Close()
  169. if err != nil {
  170. return err
  171. }
  172. return os.Rename(tempFilePath, fileName)
  173. }