14 // CreateSelectCsvObjectMeta is Creating csv object meta
16 // key the object key.
17 // csvMeta the csv file meta
18 // options the options for create csv Meta of the object.
20 // MetaEndFrameCSV the csv file meta info
21 // error it's nil if no error, otherwise it's an error object.
23 func (bucket Bucket) CreateSelectCsvObjectMeta(key string, csvMeta CsvMetaRequest, options ...Option) (MetaEndFrameCSV, error) {
24 var endFrame MetaEndFrameCSV
25 params := map[string]interface{}{}
26 params["x-oss-process"] = "csv/meta"
28 csvMeta.encodeBase64()
29 bs, err := xml.Marshal(csvMeta)
33 buffer := new(bytes.Buffer)
36 resp, err := bucket.DoPostSelectObject(key, params, buffer, options...)
40 defer resp.Body.Close()
42 _, err = ioutil.ReadAll(resp)
44 return resp.Frame.MetaEndFrameCSV, err
47 // CreateSelectJsonObjectMeta is Creating json object meta
49 // key the object key.
50 // csvMeta the json file meta
51 // options the options for create json Meta of the object.
53 // MetaEndFrameJSON the json file meta info
54 // error it's nil if no error, otherwise it's an error object.
56 func (bucket Bucket) CreateSelectJsonObjectMeta(key string, jsonMeta JsonMetaRequest, options ...Option) (MetaEndFrameJSON, error) {
57 var endFrame MetaEndFrameJSON
58 params := map[string]interface{}{}
59 params["x-oss-process"] = "json/meta"
61 bs, err := xml.Marshal(jsonMeta)
65 buffer := new(bytes.Buffer)
68 resp, err := bucket.DoPostSelectObject(key, params, buffer, options...)
72 defer resp.Body.Close()
74 _, err = ioutil.ReadAll(resp)
76 return resp.Frame.MetaEndFrameJSON, err
79 // SelectObject is the select object api, approve csv and json file.
81 // key the object key.
82 // selectReq the request data for select object
83 // options the options for select file of the object.
85 // o.ReadCloser reader instance for reading data from response. It must be called close() after the usage and only valid when error is nil.
86 // error it's nil if no error, otherwise it's an error object.
88 func (bucket Bucket) SelectObject(key string, selectReq SelectRequest, options ...Option) (io.ReadCloser, error) {
89 params := map[string]interface{}{}
90 if selectReq.InputSerializationSelect.JsonBodyInput.JsonIsEmpty() {
91 params["x-oss-process"] = "csv/select" // default select csv file
93 params["x-oss-process"] = "json/select"
95 selectReq.encodeBase64()
96 bs, err := xml.Marshal(selectReq)
100 buffer := new(bytes.Buffer)
102 resp, err := bucket.DoPostSelectObject(key, params, buffer, options...)
106 if selectReq.OutputSerializationSelect.EnablePayloadCrc != nil && *selectReq.OutputSerializationSelect.EnablePayloadCrc == true {
107 resp.Frame.EnablePayloadCrc = true
109 resp.Frame.OutputRawData = strings.ToUpper(resp.Headers.Get("x-oss-select-output-raw")) == "TRUE"
114 // DoPostSelectObject is the SelectObject/CreateMeta api, approve csv and json file.
116 // key the object key.
117 // params the resource of oss approve csv/meta, json/meta, csv/select, json/select.
118 // buf the request data trans to buffer.
119 // options the options for select file of the object.
121 // SelectObjectResponse the response of select object.
122 // error it's nil if no error, otherwise it's an error object.
124 func (bucket Bucket) DoPostSelectObject(key string, params map[string]interface{}, buf *bytes.Buffer, options ...Option) (*SelectObjectResponse, error) {
125 resp, err := bucket.do("POST", key, params, options, buf, nil)
130 result := &SelectObjectResponse{
132 StatusCode: resp.StatusCode,
133 Frame: SelectObjectResult{},
135 result.Headers = resp.Headers
136 // result.Frame = SelectObjectResult{}
137 result.ReadTimeOut = bucket.GetConfig().Timeout
140 listener := GetProgressListener(options)
143 crcCalc := crc32.NewIEEE()
144 result.WriterForCheckCrc32 = crcCalc
145 result.Body = TeeReader(resp.Body, nil, 0, listener, nil)
147 err = CheckRespCode(resp.StatusCode, []int{http.StatusPartialContent, http.StatusOK})
152 // SelectObjectIntoFile is the selectObject to file api
154 // key the object key.
155 // fileName saving file's name to localstation.
156 // selectReq the request data for select object
157 // options the options for select file of the object.
159 // error it's nil if no error, otherwise it's an error object.
161 func (bucket Bucket) SelectObjectIntoFile(key, fileName string, selectReq SelectRequest, options ...Option) error {
162 tempFilePath := fileName + TempFileSuffix
164 params := map[string]interface{}{}
165 if selectReq.InputSerializationSelect.JsonBodyInput.JsonIsEmpty() {
166 params["x-oss-process"] = "csv/select" // default select csv file
168 params["x-oss-process"] = "json/select"
170 selectReq.encodeBase64()
171 bs, err := xml.Marshal(selectReq)
175 buffer := new(bytes.Buffer)
177 resp, err := bucket.DoPostSelectObject(key, params, buffer, options...)
183 // If the local file does not exist, create a new one. If it exists, overwrite it.
184 fd, err := os.OpenFile(tempFilePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, FilePermMode)
189 // Copy the data to the local file path.
190 _, err = io.Copy(fd, resp)
196 return os.Rename(tempFilePath, fileName)