You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

client.go 48 kB

4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  1. // Copyright 2019 Huawei Technologies Co.,Ltd.
  2. // Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  3. // this file except in compliance with the License. You may obtain a copy of the
  4. // License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software distributed
  9. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  10. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  11. // specific language governing permissions and limitations under the License.
  12. //nolint:golint, unused
  13. package obs
  14. import (
  15. "errors"
  16. "fmt"
  17. "io"
  18. "net/http"
  19. "os"
  20. "sort"
  21. "strings"
  22. )
  23. // ObsClient defines OBS client.
  24. type ObsClient struct {
  25. conf *config
  26. httpClient *http.Client
  27. }
  28. // New creates a new ObsClient instance.
  29. func New(ak, sk, endpoint string, configurers ...configurer) (*ObsClient, error) {
  30. conf := &config{securityProvider: &securityProvider{ak: ak, sk: sk}, endpoint: endpoint}
  31. conf.maxRetryCount = -1
  32. conf.maxRedirectCount = -1
  33. for _, configurer := range configurers {
  34. configurer(conf)
  35. }
  36. if err := conf.initConfigWithDefault(); err != nil {
  37. return nil, err
  38. }
  39. err := conf.getTransport()
  40. if err != nil {
  41. return nil, err
  42. }
  43. if isWarnLogEnabled() {
  44. info := make([]string, 3)
  45. info[0] = fmt.Sprintf("[OBS SDK Version=%s", obsSdkVersion)
  46. info[1] = fmt.Sprintf("Endpoint=%s", conf.endpoint)
  47. accessMode := "Virtual Hosting"
  48. if conf.pathStyle {
  49. accessMode = "Path"
  50. }
  51. info[2] = fmt.Sprintf("Access Mode=%s]", accessMode)
  52. doLog(LEVEL_WARN, strings.Join(info, "];["))
  53. }
  54. doLog(LEVEL_DEBUG, "Create obsclient with config:\n%s\n", conf)
  55. obsClient := &ObsClient{conf: conf, httpClient: &http.Client{Transport: conf.transport, CheckRedirect: checkRedirectFunc}}
  56. return obsClient, nil
  57. }
  58. // Refresh refreshes ak, sk and securityToken for obsClient.
  59. func (obsClient ObsClient) Refresh(ak, sk, securityToken string) {
  60. sp := &securityProvider{ak: strings.TrimSpace(ak), sk: strings.TrimSpace(sk), securityToken: strings.TrimSpace(securityToken)}
  61. obsClient.conf.securityProvider = sp
  62. }
  63. // Close closes ObsClient.
  64. func (obsClient ObsClient) Close() {
  65. obsClient.httpClient = nil
  66. obsClient.conf.transport.CloseIdleConnections()
  67. obsClient.conf = nil
  68. }
  69. // ListBuckets lists buckets.
  70. //
  71. // You can use this API to obtain the bucket list. In the list, bucket names are displayed in lexicographical order.
  72. func (obsClient ObsClient) ListBuckets(input *ListBucketsInput, extensions ...extensionOptions) (output *ListBucketsOutput, err error) {
  73. if input == nil {
  74. input = &ListBucketsInput{}
  75. }
  76. output = &ListBucketsOutput{}
  77. err = obsClient.doActionWithoutBucket("ListBuckets", HTTP_GET, input, output, extensions)
  78. if err != nil {
  79. output = nil
  80. }
  81. return
  82. }
  83. // CreateBucket creates a bucket.
  84. //
  85. // You can use this API to create a bucket and name it as you specify. The created bucket name must be unique in OBS.
  86. func (obsClient ObsClient) CreateBucket(input *CreateBucketInput, extensions ...extensionOptions) (output *BaseModel, err error) {
  87. if input == nil {
  88. return nil, errors.New("CreateBucketInput is nil")
  89. }
  90. output = &BaseModel{}
  91. err = obsClient.doActionWithBucket("CreateBucket", HTTP_PUT, input.Bucket, input, output, extensions)
  92. if err != nil {
  93. output = nil
  94. }
  95. return
  96. }
  97. // DeleteBucket deletes a bucket.
  98. //
  99. // You can use this API to delete a bucket. The bucket to be deleted must be empty
  100. // (containing no objects, noncurrent object versions, or part fragments).
  101. func (obsClient ObsClient) DeleteBucket(bucketName string, extensions ...extensionOptions) (output *BaseModel, err error) {
  102. output = &BaseModel{}
  103. err = obsClient.doActionWithBucket("DeleteBucket", HTTP_DELETE, bucketName, defaultSerializable, output, extensions)
  104. if err != nil {
  105. output = nil
  106. }
  107. return
  108. }
  109. // SetBucketStoragePolicy sets bucket storage class.
  110. //
  111. // You can use this API to set storage class for bucket.
  112. func (obsClient ObsClient) SetBucketStoragePolicy(input *SetBucketStoragePolicyInput, extensions ...extensionOptions) (output *BaseModel, err error) {
  113. if input == nil {
  114. return nil, errors.New("SetBucketStoragePolicyInput is nil")
  115. }
  116. output = &BaseModel{}
  117. err = obsClient.doActionWithBucket("SetBucketStoragePolicy", HTTP_PUT, input.Bucket, input, output, extensions)
  118. if err != nil {
  119. output = nil
  120. }
  121. return
  122. }
  123. func (obsClient ObsClient) getBucketStoragePolicyS3(bucketName string, extensions []extensionOptions) (output *GetBucketStoragePolicyOutput, err error) {
  124. output = &GetBucketStoragePolicyOutput{}
  125. var outputS3 *getBucketStoragePolicyOutputS3
  126. outputS3 = &getBucketStoragePolicyOutputS3{}
  127. err = obsClient.doActionWithBucket("GetBucketStoragePolicy", HTTP_GET, bucketName, newSubResourceSerial(SubResourceStoragePolicy), outputS3, extensions)
  128. if err != nil {
  129. output = nil
  130. return
  131. }
  132. output.BaseModel = outputS3.BaseModel
  133. output.StorageClass = fmt.Sprintf("%s", outputS3.StorageClass)
  134. return
  135. }
  136. func (obsClient ObsClient) getBucketStoragePolicyObs(bucketName string, extensions []extensionOptions) (output *GetBucketStoragePolicyOutput, err error) {
  137. output = &GetBucketStoragePolicyOutput{}
  138. var outputObs *getBucketStoragePolicyOutputObs
  139. outputObs = &getBucketStoragePolicyOutputObs{}
  140. err = obsClient.doActionWithBucket("GetBucketStoragePolicy", HTTP_GET, bucketName, newSubResourceSerial(SubResourceStorageClass), outputObs, extensions)
  141. if err != nil {
  142. output = nil
  143. return
  144. }
  145. output.BaseModel = outputObs.BaseModel
  146. output.StorageClass = outputObs.StorageClass
  147. return
  148. }
  149. // GetBucketStoragePolicy gets bucket storage class.
  150. //
  151. // You can use this API to obtain the storage class of a bucket.
  152. func (obsClient ObsClient) GetBucketStoragePolicy(bucketName string, extensions ...extensionOptions) (output *GetBucketStoragePolicyOutput, err error) {
  153. if obsClient.conf.signature == SignatureObs {
  154. return obsClient.getBucketStoragePolicyObs(bucketName, extensions)
  155. }
  156. return obsClient.getBucketStoragePolicyS3(bucketName, extensions)
  157. }
  158. // ListObjects lists objects in a bucket.
  159. //
  160. // You can use this API to list objects in a bucket. By default, a maximum of 1000 objects are listed.
  161. func (obsClient ObsClient) ListObjects(input *ListObjectsInput, extensions ...extensionOptions) (output *ListObjectsOutput, err error) {
  162. if input == nil {
  163. return nil, errors.New("ListObjectsInput is nil")
  164. }
  165. output = &ListObjectsOutput{}
  166. err = obsClient.doActionWithBucket("ListObjects", HTTP_GET, input.Bucket, input, output, extensions)
  167. if err != nil {
  168. output = nil
  169. } else {
  170. if location, ok := output.ResponseHeaders[HEADER_BUCKET_REGION]; ok {
  171. output.Location = location[0]
  172. }
  173. }
  174. return
  175. }
  176. // ListVersions lists versioning objects in a bucket.
  177. //
  178. // You can use this API to list versioning objects in a bucket. By default, a maximum of 1000 versioning objects are listed.
  179. func (obsClient ObsClient) ListVersions(input *ListVersionsInput, extensions ...extensionOptions) (output *ListVersionsOutput, err error) {
  180. if input == nil {
  181. return nil, errors.New("ListVersionsInput is nil")
  182. }
  183. output = &ListVersionsOutput{}
  184. err = obsClient.doActionWithBucket("ListVersions", HTTP_GET, input.Bucket, input, output, extensions)
  185. if err != nil {
  186. output = nil
  187. } else {
  188. if location, ok := output.ResponseHeaders[HEADER_BUCKET_REGION]; ok {
  189. output.Location = location[0]
  190. }
  191. }
  192. return
  193. }
  194. // ListMultipartUploads lists the multipart uploads.
  195. //
  196. // You can use this API to list the multipart uploads that are initialized but not combined or aborted in a specified bucket.
  197. func (obsClient ObsClient) ListMultipartUploads(input *ListMultipartUploadsInput, extensions ...extensionOptions) (output *ListMultipartUploadsOutput, err error) {
  198. if input == nil {
  199. return nil, errors.New("ListMultipartUploadsInput is nil")
  200. }
  201. output = &ListMultipartUploadsOutput{}
  202. err = obsClient.doActionWithBucket("ListMultipartUploads", HTTP_GET, input.Bucket, input, output, extensions)
  203. if err != nil {
  204. output = nil
  205. }
  206. return
  207. }
  208. // SetBucketQuota sets the bucket quota.
  209. //
  210. // You can use this API to set the bucket quota. A bucket quota must be expressed in bytes and the maximum value is 2^63-1.
  211. func (obsClient ObsClient) SetBucketQuota(input *SetBucketQuotaInput, extensions ...extensionOptions) (output *BaseModel, err error) {
  212. if input == nil {
  213. return nil, errors.New("SetBucketQuotaInput is nil")
  214. }
  215. output = &BaseModel{}
  216. err = obsClient.doActionWithBucket("SetBucketQuota", HTTP_PUT, input.Bucket, input, output, extensions)
  217. if err != nil {
  218. output = nil
  219. }
  220. return
  221. }
  222. // GetBucketQuota gets the bucket quota.
  223. //
  224. // You can use this API to obtain the bucket quota. Value 0 indicates that no upper limit is set for the bucket quota.
  225. func (obsClient ObsClient) GetBucketQuota(bucketName string, extensions ...extensionOptions) (output *GetBucketQuotaOutput, err error) {
  226. output = &GetBucketQuotaOutput{}
  227. err = obsClient.doActionWithBucket("GetBucketQuota", HTTP_GET, bucketName, newSubResourceSerial(SubResourceQuota), output, extensions)
  228. if err != nil {
  229. output = nil
  230. }
  231. return
  232. }
  233. // HeadBucket checks whether a bucket exists.
  234. //
  235. // You can use this API to check whether a bucket exists.
  236. func (obsClient ObsClient) HeadBucket(bucketName string, extensions ...extensionOptions) (output *BaseModel, err error) {
  237. output = &BaseModel{}
  238. err = obsClient.doActionWithBucket("HeadBucket", HTTP_HEAD, bucketName, defaultSerializable, output, extensions)
  239. if err != nil {
  240. output = nil
  241. }
  242. return
  243. }
  244. // HeadObject checks whether an object exists.
  245. //
  246. // You can use this API to check whether an object exists.
  247. func (obsClient ObsClient) HeadObject(input *HeadObjectInput, extensions ...extensionOptions) (output *BaseModel, err error) {
  248. if input == nil {
  249. return nil, errors.New("HeadObjectInput is nil")
  250. }
  251. output = &BaseModel{}
  252. err = obsClient.doActionWithBucketAndKey("HeadObject", HTTP_HEAD, input.Bucket, input.Key, input, output, extensions)
  253. if err != nil {
  254. output = nil
  255. }
  256. return
  257. }
  258. // GetBucketMetadata gets the metadata of a bucket.
  259. //
  260. // You can use this API to send a HEAD request to a bucket to obtain the bucket
  261. // metadata such as the storage class and CORS rules (if set).
  262. func (obsClient ObsClient) GetBucketMetadata(input *GetBucketMetadataInput, extensions ...extensionOptions) (output *GetBucketMetadataOutput, err error) {
  263. output = &GetBucketMetadataOutput{}
  264. err = obsClient.doActionWithBucket("GetBucketMetadata", HTTP_HEAD, input.Bucket, input, output, extensions)
  265. if err != nil {
  266. output = nil
  267. } else {
  268. ParseGetBucketMetadataOutput(output)
  269. }
  270. return
  271. }
  272. // SetObjectMetadata sets object metadata.
  273. func (obsClient ObsClient) SetObjectMetadata(input *SetObjectMetadataInput, extensions ...extensionOptions) (output *SetObjectMetadataOutput, err error) {
  274. output = &SetObjectMetadataOutput{}
  275. err = obsClient.doActionWithBucketAndKey("SetObjectMetadata", HTTP_PUT, input.Bucket, input.Key, input, output, extensions)
  276. if err != nil {
  277. output = nil
  278. } else {
  279. ParseSetObjectMetadataOutput(output)
  280. }
  281. return
  282. }
  283. // GetBucketStorageInfo gets storage information about a bucket.
  284. //
  285. // You can use this API to obtain storage information about a bucket, including the
  286. // bucket size and number of objects in the bucket.
  287. func (obsClient ObsClient) GetBucketStorageInfo(bucketName string, extensions ...extensionOptions) (output *GetBucketStorageInfoOutput, err error) {
  288. output = &GetBucketStorageInfoOutput{}
  289. err = obsClient.doActionWithBucket("GetBucketStorageInfo", HTTP_GET, bucketName, newSubResourceSerial(SubResourceStorageInfo), output, extensions)
  290. if err != nil {
  291. output = nil
  292. }
  293. return
  294. }
  295. func (obsClient ObsClient) getBucketLocationS3(bucketName string, extensions []extensionOptions) (output *GetBucketLocationOutput, err error) {
  296. output = &GetBucketLocationOutput{}
  297. var outputS3 *getBucketLocationOutputS3
  298. outputS3 = &getBucketLocationOutputS3{}
  299. err = obsClient.doActionWithBucket("GetBucketLocation", HTTP_GET, bucketName, newSubResourceSerial(SubResourceLocation), outputS3, extensions)
  300. if err != nil {
  301. output = nil
  302. } else {
  303. output.BaseModel = outputS3.BaseModel
  304. output.Location = outputS3.Location
  305. }
  306. return
  307. }
  308. func (obsClient ObsClient) getBucketLocationObs(bucketName string, extensions []extensionOptions) (output *GetBucketLocationOutput, err error) {
  309. output = &GetBucketLocationOutput{}
  310. var outputObs *getBucketLocationOutputObs
  311. outputObs = &getBucketLocationOutputObs{}
  312. err = obsClient.doActionWithBucket("GetBucketLocation", HTTP_GET, bucketName, newSubResourceSerial(SubResourceLocation), outputObs, extensions)
  313. if err != nil {
  314. output = nil
  315. } else {
  316. output.BaseModel = outputObs.BaseModel
  317. output.Location = outputObs.Location
  318. }
  319. return
  320. }
  321. // GetBucketLocation gets the location of a bucket.
  322. //
  323. // You can use this API to obtain the bucket location.
  324. func (obsClient ObsClient) GetBucketLocation(bucketName string, extensions ...extensionOptions) (output *GetBucketLocationOutput, err error) {
  325. if obsClient.conf.signature == SignatureObs {
  326. return obsClient.getBucketLocationObs(bucketName, extensions)
  327. }
  328. return obsClient.getBucketLocationS3(bucketName, extensions)
  329. }
  330. // SetBucketAcl sets the bucket ACL.
  331. //
  332. // You can use this API to set the ACL for a bucket.
  333. func (obsClient ObsClient) SetBucketAcl(input *SetBucketAclInput, extensions ...extensionOptions) (output *BaseModel, err error) {
  334. if input == nil {
  335. return nil, errors.New("SetBucketAclInput is nil")
  336. }
  337. output = &BaseModel{}
  338. err = obsClient.doActionWithBucket("SetBucketAcl", HTTP_PUT, input.Bucket, input, output, extensions)
  339. if err != nil {
  340. output = nil
  341. }
  342. return
  343. }
  344. func (obsClient ObsClient) getBucketACLObs(bucketName string, extensions []extensionOptions) (output *GetBucketAclOutput, err error) {
  345. output = &GetBucketAclOutput{}
  346. var outputObs *getBucketACLOutputObs
  347. outputObs = &getBucketACLOutputObs{}
  348. err = obsClient.doActionWithBucket("GetBucketAcl", HTTP_GET, bucketName, newSubResourceSerial(SubResourceAcl), outputObs, extensions)
  349. if err != nil {
  350. output = nil
  351. } else {
  352. output.BaseModel = outputObs.BaseModel
  353. output.Owner = outputObs.Owner
  354. output.Grants = make([]Grant, 0, len(outputObs.Grants))
  355. for _, valGrant := range outputObs.Grants {
  356. tempOutput := Grant{}
  357. tempOutput.Delivered = valGrant.Delivered
  358. tempOutput.Permission = valGrant.Permission
  359. tempOutput.Grantee.DisplayName = valGrant.Grantee.DisplayName
  360. tempOutput.Grantee.ID = valGrant.Grantee.ID
  361. tempOutput.Grantee.Type = valGrant.Grantee.Type
  362. tempOutput.Grantee.URI = GroupAllUsers
  363. output.Grants = append(output.Grants, tempOutput)
  364. }
  365. }
  366. return
  367. }
  368. // GetBucketAcl gets the bucket ACL.
  369. //
  370. // You can use this API to obtain a bucket ACL.
  371. func (obsClient ObsClient) GetBucketAcl(bucketName string, extensions ...extensionOptions) (output *GetBucketAclOutput, err error) {
  372. output = &GetBucketAclOutput{}
  373. if obsClient.conf.signature == SignatureObs {
  374. return obsClient.getBucketACLObs(bucketName, extensions)
  375. }
  376. err = obsClient.doActionWithBucket("GetBucketAcl", HTTP_GET, bucketName, newSubResourceSerial(SubResourceAcl), output, extensions)
  377. if err != nil {
  378. output = nil
  379. }
  380. return
  381. }
  382. // SetBucketPolicy sets the bucket policy.
  383. //
  384. // You can use this API to set a bucket policy. If the bucket already has a policy, the
  385. // policy will be overwritten by the one specified in this request.
  386. func (obsClient ObsClient) SetBucketPolicy(input *SetBucketPolicyInput, extensions ...extensionOptions) (output *BaseModel, err error) {
  387. if input == nil {
  388. return nil, errors.New("SetBucketPolicy is nil")
  389. }
  390. output = &BaseModel{}
  391. err = obsClient.doActionWithBucket("SetBucketPolicy", HTTP_PUT, input.Bucket, input, output, extensions)
  392. if err != nil {
  393. output = nil
  394. }
  395. return
  396. }
  397. // GetBucketPolicy gets the bucket policy.
  398. //
  399. // You can use this API to obtain the policy of a bucket.
  400. func (obsClient ObsClient) GetBucketPolicy(bucketName string, extensions ...extensionOptions) (output *GetBucketPolicyOutput, err error) {
  401. output = &GetBucketPolicyOutput{}
  402. err = obsClient.doActionWithBucketV2("GetBucketPolicy", HTTP_GET, bucketName, newSubResourceSerial(SubResourcePolicy), output, extensions)
  403. if err != nil {
  404. output = nil
  405. }
  406. return
  407. }
  408. // DeleteBucketPolicy deletes the bucket policy.
  409. //
  410. // You can use this API to delete the policy of a bucket.
  411. func (obsClient ObsClient) DeleteBucketPolicy(bucketName string, extensions ...extensionOptions) (output *BaseModel, err error) {
  412. output = &BaseModel{}
  413. err = obsClient.doActionWithBucket("DeleteBucketPolicy", HTTP_DELETE, bucketName, newSubResourceSerial(SubResourcePolicy), output, extensions)
  414. if err != nil {
  415. output = nil
  416. }
  417. return
  418. }
  419. // SetBucketCors sets CORS rules for a bucket.
  420. //
  421. // You can use this API to set CORS rules for a bucket to allow client browsers to send cross-origin requests.
  422. func (obsClient ObsClient) SetBucketCors(input *SetBucketCorsInput, extensions ...extensionOptions) (output *BaseModel, err error) {
  423. if input == nil {
  424. return nil, errors.New("SetBucketCorsInput is nil")
  425. }
  426. output = &BaseModel{}
  427. err = obsClient.doActionWithBucket("SetBucketCors", HTTP_PUT, input.Bucket, input, output, extensions)
  428. if err != nil {
  429. output = nil
  430. }
  431. return
  432. }
  433. // GetBucketCors gets CORS rules of a bucket.
  434. //
  435. // You can use this API to obtain the CORS rules of a specified bucket.
  436. func (obsClient ObsClient) GetBucketCors(bucketName string, extensions ...extensionOptions) (output *GetBucketCorsOutput, err error) {
  437. output = &GetBucketCorsOutput{}
  438. err = obsClient.doActionWithBucket("GetBucketCors", HTTP_GET, bucketName, newSubResourceSerial(SubResourceCors), output, extensions)
  439. if err != nil {
  440. output = nil
  441. }
  442. return
  443. }
  444. // DeleteBucketCors deletes CORS rules of a bucket.
  445. //
  446. // You can use this API to delete the CORS rules of a specified bucket.
  447. func (obsClient ObsClient) DeleteBucketCors(bucketName string, extensions ...extensionOptions) (output *BaseModel, err error) {
  448. output = &BaseModel{}
  449. err = obsClient.doActionWithBucket("DeleteBucketCors", HTTP_DELETE, bucketName, newSubResourceSerial(SubResourceCors), output, extensions)
  450. if err != nil {
  451. output = nil
  452. }
  453. return
  454. }
  455. // SetBucketVersioning sets the versioning status for a bucket.
  456. //
  457. // You can use this API to set the versioning status for a bucket.
  458. func (obsClient ObsClient) SetBucketVersioning(input *SetBucketVersioningInput, extensions ...extensionOptions) (output *BaseModel, err error) {
  459. if input == nil {
  460. return nil, errors.New("SetBucketVersioningInput is nil")
  461. }
  462. output = &BaseModel{}
  463. err = obsClient.doActionWithBucket("SetBucketVersioning", HTTP_PUT, input.Bucket, input, output, extensions)
  464. if err != nil {
  465. output = nil
  466. }
  467. return
  468. }
  469. // GetBucketVersioning gets the versioning status of a bucket.
  470. //
  471. // You can use this API to obtain the versioning status of a bucket.
  472. func (obsClient ObsClient) GetBucketVersioning(bucketName string, extensions ...extensionOptions) (output *GetBucketVersioningOutput, err error) {
  473. output = &GetBucketVersioningOutput{}
  474. err = obsClient.doActionWithBucket("GetBucketVersioning", HTTP_GET, bucketName, newSubResourceSerial(SubResourceVersioning), output, extensions)
  475. if err != nil {
  476. output = nil
  477. }
  478. return
  479. }
  480. // SetBucketWebsiteConfiguration sets website hosting for a bucket.
  481. //
  482. // You can use this API to set website hosting for a bucket.
  483. func (obsClient ObsClient) SetBucketWebsiteConfiguration(input *SetBucketWebsiteConfigurationInput, extensions ...extensionOptions) (output *BaseModel, err error) {
  484. if input == nil {
  485. return nil, errors.New("SetBucketWebsiteConfigurationInput is nil")
  486. }
  487. output = &BaseModel{}
  488. err = obsClient.doActionWithBucket("SetBucketWebsiteConfiguration", HTTP_PUT, input.Bucket, input, output, extensions)
  489. if err != nil {
  490. output = nil
  491. }
  492. return
  493. }
  494. // GetBucketWebsiteConfiguration gets the website hosting settings of a bucket.
  495. //
  496. // You can use this API to obtain the website hosting settings of a bucket.
  497. func (obsClient ObsClient) GetBucketWebsiteConfiguration(bucketName string, extensions ...extensionOptions) (output *GetBucketWebsiteConfigurationOutput, err error) {
  498. output = &GetBucketWebsiteConfigurationOutput{}
  499. err = obsClient.doActionWithBucket("GetBucketWebsiteConfiguration", HTTP_GET, bucketName, newSubResourceSerial(SubResourceWebsite), output, extensions)
  500. if err != nil {
  501. output = nil
  502. }
  503. return
  504. }
  505. // DeleteBucketWebsiteConfiguration deletes the website hosting settings of a bucket.
  506. //
  507. // You can use this API to delete the website hosting settings of a bucket.
  508. func (obsClient ObsClient) DeleteBucketWebsiteConfiguration(bucketName string, extensions ...extensionOptions) (output *BaseModel, err error) {
  509. output = &BaseModel{}
  510. err = obsClient.doActionWithBucket("DeleteBucketWebsiteConfiguration", HTTP_DELETE, bucketName, newSubResourceSerial(SubResourceWebsite), output, extensions)
  511. if err != nil {
  512. output = nil
  513. }
  514. return
  515. }
  516. // SetBucketLoggingConfiguration sets the bucket logging.
  517. //
  518. // You can use this API to configure access logging for a bucket.
  519. func (obsClient ObsClient) SetBucketLoggingConfiguration(input *SetBucketLoggingConfigurationInput, extensions ...extensionOptions) (output *BaseModel, err error) {
  520. if input == nil {
  521. return nil, errors.New("SetBucketLoggingConfigurationInput is nil")
  522. }
  523. output = &BaseModel{}
  524. err = obsClient.doActionWithBucket("SetBucketLoggingConfiguration", HTTP_PUT, input.Bucket, input, output, extensions)
  525. if err != nil {
  526. output = nil
  527. }
  528. return
  529. }
  530. // GetBucketLoggingConfiguration gets the logging settings of a bucket.
  531. //
  532. // You can use this API to obtain the access logging settings of a bucket.
  533. func (obsClient ObsClient) GetBucketLoggingConfiguration(bucketName string, extensions ...extensionOptions) (output *GetBucketLoggingConfigurationOutput, err error) {
  534. output = &GetBucketLoggingConfigurationOutput{}
  535. err = obsClient.doActionWithBucket("GetBucketLoggingConfiguration", HTTP_GET, bucketName, newSubResourceSerial(SubResourceLogging), output, extensions)
  536. if err != nil {
  537. output = nil
  538. }
  539. return
  540. }
  541. // SetBucketLifecycleConfiguration sets lifecycle rules for a bucket.
  542. //
  543. // You can use this API to set lifecycle rules for a bucket, to periodically transit
  544. // storage classes of objects and delete objects in the bucket.
  545. func (obsClient ObsClient) SetBucketLifecycleConfiguration(input *SetBucketLifecycleConfigurationInput, extensions ...extensionOptions) (output *BaseModel, err error) {
  546. if input == nil {
  547. return nil, errors.New("SetBucketLifecycleConfigurationInput is nil")
  548. }
  549. output = &BaseModel{}
  550. err = obsClient.doActionWithBucket("SetBucketLifecycleConfiguration", HTTP_PUT, input.Bucket, input, output, extensions)
  551. if err != nil {
  552. output = nil
  553. }
  554. return
  555. }
  556. // GetBucketLifecycleConfiguration gets lifecycle rules of a bucket.
  557. //
  558. // You can use this API to obtain the lifecycle rules of a bucket.
  559. func (obsClient ObsClient) GetBucketLifecycleConfiguration(bucketName string, extensions ...extensionOptions) (output *GetBucketLifecycleConfigurationOutput, err error) {
  560. output = &GetBucketLifecycleConfigurationOutput{}
  561. err = obsClient.doActionWithBucket("GetBucketLifecycleConfiguration", HTTP_GET, bucketName, newSubResourceSerial(SubResourceLifecycle), output, extensions)
  562. if err != nil {
  563. output = nil
  564. }
  565. return
  566. }
  567. // DeleteBucketLifecycleConfiguration deletes lifecycle rules of a bucket.
  568. //
  569. // You can use this API to delete all lifecycle rules of a bucket.
  570. func (obsClient ObsClient) DeleteBucketLifecycleConfiguration(bucketName string, extensions ...extensionOptions) (output *BaseModel, err error) {
  571. output = &BaseModel{}
  572. err = obsClient.doActionWithBucket("DeleteBucketLifecycleConfiguration", HTTP_DELETE, bucketName, newSubResourceSerial(SubResourceLifecycle), output, extensions)
  573. if err != nil {
  574. output = nil
  575. }
  576. return
  577. }
  578. // SetBucketTagging sets bucket tags.
  579. //
  580. // You can use this API to set bucket tags.
  581. func (obsClient ObsClient) SetBucketTagging(input *SetBucketTaggingInput, extensions ...extensionOptions) (output *BaseModel, err error) {
  582. if input == nil {
  583. return nil, errors.New("SetBucketTaggingInput is nil")
  584. }
  585. output = &BaseModel{}
  586. err = obsClient.doActionWithBucket("SetBucketTagging", HTTP_PUT, input.Bucket, input, output, extensions)
  587. if err != nil {
  588. output = nil
  589. }
  590. return
  591. }
  592. // GetBucketTagging gets bucket tags.
  593. //
  594. // You can use this API to obtain the tags of a specified bucket.
  595. func (obsClient ObsClient) GetBucketTagging(bucketName string, extensions ...extensionOptions) (output *GetBucketTaggingOutput, err error) {
  596. output = &GetBucketTaggingOutput{}
  597. err = obsClient.doActionWithBucket("GetBucketTagging", HTTP_GET, bucketName, newSubResourceSerial(SubResourceTagging), output, extensions)
  598. if err != nil {
  599. output = nil
  600. }
  601. return
  602. }
  603. // DeleteBucketTagging deletes bucket tags.
  604. //
  605. // You can use this API to delete the tags of a specified bucket.
  606. func (obsClient ObsClient) DeleteBucketTagging(bucketName string, extensions ...extensionOptions) (output *BaseModel, err error) {
  607. output = &BaseModel{}
  608. err = obsClient.doActionWithBucket("DeleteBucketTagging", HTTP_DELETE, bucketName, newSubResourceSerial(SubResourceTagging), output, extensions)
  609. if err != nil {
  610. output = nil
  611. }
  612. return
  613. }
  614. // SetBucketNotification sets event notification for a bucket.
  615. //
  616. // You can use this API to configure event notification for a bucket. You will be notified of all
  617. // specified operations performed on the bucket.
  618. func (obsClient ObsClient) SetBucketNotification(input *SetBucketNotificationInput, extensions ...extensionOptions) (output *BaseModel, err error) {
  619. if input == nil {
  620. return nil, errors.New("SetBucketNotificationInput is nil")
  621. }
  622. output = &BaseModel{}
  623. err = obsClient.doActionWithBucket("SetBucketNotification", HTTP_PUT, input.Bucket, input, output, extensions)
  624. if err != nil {
  625. output = nil
  626. }
  627. return
  628. }
  629. // GetBucketNotification gets event notification settings of a bucket.
  630. //
  631. // You can use this API to obtain the event notification configuration of a bucket.
  632. func (obsClient ObsClient) GetBucketNotification(bucketName string, extensions ...extensionOptions) (output *GetBucketNotificationOutput, err error) {
  633. if obsClient.conf.signature != SignatureObs {
  634. return obsClient.getBucketNotificationS3(bucketName, extensions)
  635. }
  636. output = &GetBucketNotificationOutput{}
  637. err = obsClient.doActionWithBucket("GetBucketNotification", HTTP_GET, bucketName, newSubResourceSerial(SubResourceNotification), output, extensions)
  638. if err != nil {
  639. output = nil
  640. }
  641. return
  642. }
  643. func (obsClient ObsClient) getBucketNotificationS3(bucketName string, extensions []extensionOptions) (output *GetBucketNotificationOutput, err error) {
  644. outputS3 := &getBucketNotificationOutputS3{}
  645. err = obsClient.doActionWithBucket("GetBucketNotification", HTTP_GET, bucketName, newSubResourceSerial(SubResourceNotification), outputS3, extensions)
  646. if err != nil {
  647. return nil, err
  648. }
  649. output = &GetBucketNotificationOutput{}
  650. output.BaseModel = outputS3.BaseModel
  651. topicConfigurations := make([]TopicConfiguration, 0, len(outputS3.TopicConfigurations))
  652. for _, topicConfigurationS3 := range outputS3.TopicConfigurations {
  653. topicConfiguration := TopicConfiguration{}
  654. topicConfiguration.ID = topicConfigurationS3.ID
  655. topicConfiguration.Topic = topicConfigurationS3.Topic
  656. topicConfiguration.FilterRules = topicConfigurationS3.FilterRules
  657. events := make([]EventType, 0, len(topicConfigurationS3.Events))
  658. for _, event := range topicConfigurationS3.Events {
  659. events = append(events, ParseStringToEventType(event))
  660. }
  661. topicConfiguration.Events = events
  662. topicConfigurations = append(topicConfigurations, topicConfiguration)
  663. }
  664. output.TopicConfigurations = topicConfigurations
  665. return
  666. }
  667. // DeleteObject deletes an object.
  668. //
  669. // You can use this API to delete an object from a specified bucket.
  670. func (obsClient ObsClient) DeleteObject(input *DeleteObjectInput, extensions ...extensionOptions) (output *DeleteObjectOutput, err error) {
  671. if input == nil {
  672. return nil, errors.New("DeleteObjectInput is nil")
  673. }
  674. output = &DeleteObjectOutput{}
  675. err = obsClient.doActionWithBucketAndKey("DeleteObject", HTTP_DELETE, input.Bucket, input.Key, input, output, extensions)
  676. if err != nil {
  677. output = nil
  678. } else {
  679. ParseDeleteObjectOutput(output)
  680. }
  681. return
  682. }
  683. // DeleteObjects deletes objects in a batch.
  684. //
  685. // You can use this API to batch delete objects from a specified bucket.
  686. func (obsClient ObsClient) DeleteObjects(input *DeleteObjectsInput, extensions ...extensionOptions) (output *DeleteObjectsOutput, err error) {
  687. if input == nil {
  688. return nil, errors.New("DeleteObjectsInput is nil")
  689. }
  690. output = &DeleteObjectsOutput{}
  691. err = obsClient.doActionWithBucket("DeleteObjects", HTTP_POST, input.Bucket, input, output, extensions)
  692. if err != nil {
  693. output = nil
  694. }
  695. return
  696. }
  697. // SetObjectAcl sets ACL for an object.
  698. //
  699. // You can use this API to set the ACL for an object in a specified bucket.
  700. func (obsClient ObsClient) SetObjectAcl(input *SetObjectAclInput, extensions ...extensionOptions) (output *BaseModel, err error) {
  701. if input == nil {
  702. return nil, errors.New("SetObjectAclInput is nil")
  703. }
  704. output = &BaseModel{}
  705. err = obsClient.doActionWithBucketAndKey("SetObjectAcl", HTTP_PUT, input.Bucket, input.Key, input, output, extensions)
  706. if err != nil {
  707. output = nil
  708. }
  709. return
  710. }
  711. // GetObjectAcl gets the ACL of an object.
  712. //
  713. // You can use this API to obtain the ACL of an object in a specified bucket.
  714. func (obsClient ObsClient) GetObjectAcl(input *GetObjectAclInput, extensions ...extensionOptions) (output *GetObjectAclOutput, err error) {
  715. if input == nil {
  716. return nil, errors.New("GetObjectAclInput is nil")
  717. }
  718. output = &GetObjectAclOutput{}
  719. err = obsClient.doActionWithBucketAndKey("GetObjectAcl", HTTP_GET, input.Bucket, input.Key, input, output, extensions)
  720. if err != nil {
  721. output = nil
  722. } else {
  723. if versionID, ok := output.ResponseHeaders[HEADER_VERSION_ID]; ok {
  724. output.VersionId = versionID[0]
  725. }
  726. }
  727. return
  728. }
  729. // RestoreObject restores an object.
  730. func (obsClient ObsClient) RestoreObject(input *RestoreObjectInput, extensions ...extensionOptions) (output *BaseModel, err error) {
  731. if input == nil {
  732. return nil, errors.New("RestoreObjectInput is nil")
  733. }
  734. output = &BaseModel{}
  735. err = obsClient.doActionWithBucketAndKey("RestoreObject", HTTP_POST, input.Bucket, input.Key, input, output, extensions)
  736. if err != nil {
  737. output = nil
  738. }
  739. return
  740. }
  741. // GetObjectMetadata gets object metadata.
  742. //
  743. // You can use this API to send a HEAD request to the object of a specified bucket to obtain its metadata.
  744. func (obsClient ObsClient) GetObjectMetadata(input *GetObjectMetadataInput, extensions ...extensionOptions) (output *GetObjectMetadataOutput, err error) {
  745. if input == nil {
  746. return nil, errors.New("GetObjectMetadataInput is nil")
  747. }
  748. output = &GetObjectMetadataOutput{}
  749. err = obsClient.doActionWithBucketAndKey("GetObjectMetadata", HTTP_HEAD, input.Bucket, input.Key, input, output, extensions)
  750. if err != nil {
  751. output = nil
  752. } else {
  753. ParseGetObjectMetadataOutput(output)
  754. }
  755. return
  756. }
  757. // GetObject downloads object.
  758. //
  759. // You can use this API to download an object in a specified bucket.
  760. func (obsClient ObsClient) GetObject(input *GetObjectInput, extensions ...extensionOptions) (output *GetObjectOutput, err error) {
  761. if input == nil {
  762. return nil, errors.New("GetObjectInput is nil")
  763. }
  764. output = &GetObjectOutput{}
  765. err = obsClient.doActionWithBucketAndKey("GetObject", HTTP_GET, input.Bucket, input.Key, input, output, extensions)
  766. if err != nil {
  767. output = nil
  768. } else {
  769. ParseGetObjectOutput(output)
  770. }
  771. return
  772. }
  773. // PutObject uploads an object to the specified bucket.
  774. func (obsClient ObsClient) PutObject(input *PutObjectInput, extensions ...extensionOptions) (output *PutObjectOutput, err error) {
  775. if input == nil {
  776. return nil, errors.New("PutObjectInput is nil")
  777. }
  778. if input.ContentType == "" && input.Key != "" {
  779. if contentType, ok := mimeTypes[strings.ToLower(input.Key[strings.LastIndex(input.Key, ".")+1:])]; ok {
  780. input.ContentType = contentType
  781. }
  782. }
  783. output = &PutObjectOutput{}
  784. var repeatable bool
  785. if input.Body != nil {
  786. _, repeatable = input.Body.(*strings.Reader)
  787. if input.ContentLength > 0 {
  788. input.Body = &readerWrapper{reader: input.Body, totalCount: input.ContentLength}
  789. }
  790. }
  791. if repeatable {
  792. err = obsClient.doActionWithBucketAndKey("PutObject", HTTP_PUT, input.Bucket, input.Key, input, output, extensions)
  793. } else {
  794. err = obsClient.doActionWithBucketAndKeyUnRepeatable("PutObject", HTTP_PUT, input.Bucket, input.Key, input, output, extensions)
  795. }
  796. if err != nil {
  797. output = nil
  798. } else {
  799. ParsePutObjectOutput(output)
  800. }
  801. return
  802. }
  803. func (obsClient ObsClient) getContentType(input *PutObjectInput, sourceFile string) (contentType string) {
  804. if contentType, ok := mimeTypes[strings.ToLower(input.Key[strings.LastIndex(input.Key, ".")+1:])]; ok {
  805. return contentType
  806. }
  807. if contentType, ok := mimeTypes[strings.ToLower(sourceFile[strings.LastIndex(sourceFile, ".")+1:])]; ok {
  808. return contentType
  809. }
  810. return
  811. }
  812. func (obsClient ObsClient) isGetContentType(input *PutObjectInput) bool {
  813. if input.ContentType == "" && input.Key != "" {
  814. return true
  815. }
  816. return false
  817. }
  818. // PutFile uploads a file to the specified bucket.
  819. func (obsClient ObsClient) PutFile(input *PutFileInput, extensions ...extensionOptions) (output *PutObjectOutput, err error) {
  820. if input == nil {
  821. return nil, errors.New("PutFileInput is nil")
  822. }
  823. var body io.Reader
  824. sourceFile := strings.TrimSpace(input.SourceFile)
  825. if sourceFile != "" {
  826. fd, _err := os.Open(sourceFile)
  827. if _err != nil {
  828. err = _err
  829. return nil, err
  830. }
  831. defer func() {
  832. errMsg := fd.Close()
  833. if errMsg != nil {
  834. doLog(LEVEL_WARN, "Failed to close file with reason: %v", errMsg)
  835. }
  836. }()
  837. stat, _err := fd.Stat()
  838. if _err != nil {
  839. err = _err
  840. return nil, err
  841. }
  842. fileReaderWrapper := &fileReaderWrapper{filePath: sourceFile}
  843. fileReaderWrapper.reader = fd
  844. if input.ContentLength > 0 {
  845. if input.ContentLength > stat.Size() {
  846. input.ContentLength = stat.Size()
  847. }
  848. fileReaderWrapper.totalCount = input.ContentLength
  849. } else {
  850. fileReaderWrapper.totalCount = stat.Size()
  851. }
  852. body = fileReaderWrapper
  853. }
  854. _input := &PutObjectInput{}
  855. _input.PutObjectBasicInput = input.PutObjectBasicInput
  856. _input.Body = body
  857. if obsClient.isGetContentType(_input) {
  858. _input.ContentType = obsClient.getContentType(_input, sourceFile)
  859. }
  860. output = &PutObjectOutput{}
  861. err = obsClient.doActionWithBucketAndKey("PutFile", HTTP_PUT, _input.Bucket, _input.Key, _input, output, extensions)
  862. if err != nil {
  863. output = nil
  864. } else {
  865. ParsePutObjectOutput(output)
  866. }
  867. return
  868. }
  869. // CopyObject creates a copy for an existing object.
  870. //
  871. // You can use this API to create a copy for an object in a specified bucket.
  872. func (obsClient ObsClient) CopyObject(input *CopyObjectInput, extensions ...extensionOptions) (output *CopyObjectOutput, err error) {
  873. if input == nil {
  874. return nil, errors.New("CopyObjectInput is nil")
  875. }
  876. if strings.TrimSpace(input.CopySourceBucket) == "" {
  877. return nil, errors.New("Source bucket is empty")
  878. }
  879. if strings.TrimSpace(input.CopySourceKey) == "" {
  880. return nil, errors.New("Source key is empty")
  881. }
  882. output = &CopyObjectOutput{}
  883. err = obsClient.doActionWithBucketAndKey("CopyObject", HTTP_PUT, input.Bucket, input.Key, input, output, extensions)
  884. if err != nil {
  885. output = nil
  886. } else {
  887. ParseCopyObjectOutput(output)
  888. }
  889. return
  890. }
  891. // AbortMultipartUpload aborts a multipart upload in a specified bucket by using the multipart upload ID.
  892. func (obsClient ObsClient) AbortMultipartUpload(input *AbortMultipartUploadInput, extensions ...extensionOptions) (output *BaseModel, err error) {
  893. if input == nil {
  894. return nil, errors.New("AbortMultipartUploadInput is nil")
  895. }
  896. if input.UploadId == "" {
  897. return nil, errors.New("UploadId is empty")
  898. }
  899. output = &BaseModel{}
  900. err = obsClient.doActionWithBucketAndKey("AbortMultipartUpload", HTTP_DELETE, input.Bucket, input.Key, input, output, extensions)
  901. if err != nil {
  902. output = nil
  903. }
  904. return
  905. }
  906. // InitiateMultipartUpload initializes a multipart upload.
  907. func (obsClient ObsClient) InitiateMultipartUpload(input *InitiateMultipartUploadInput, extensions ...extensionOptions) (output *InitiateMultipartUploadOutput, err error) {
  908. if input == nil {
  909. return nil, errors.New("InitiateMultipartUploadInput is nil")
  910. }
  911. if input.ContentType == "" && input.Key != "" {
  912. if contentType, ok := mimeTypes[strings.ToLower(input.Key[strings.LastIndex(input.Key, ".")+1:])]; ok {
  913. input.ContentType = contentType
  914. }
  915. }
  916. output = &InitiateMultipartUploadOutput{}
  917. err = obsClient.doActionWithBucketAndKey("InitiateMultipartUpload", HTTP_POST, input.Bucket, input.Key, input, output, extensions)
  918. if err != nil {
  919. output = nil
  920. } else {
  921. ParseInitiateMultipartUploadOutput(output)
  922. }
  923. return
  924. }
  925. // UploadPart uploads a part to a specified bucket by using a specified multipart upload ID.
  926. //
  927. // After a multipart upload is initialized, you can use this API to upload a part to a specified bucket
  928. // by using the multipart upload ID. Except for the last uploaded part whose size ranges from 0 to 5 GB,
  929. // sizes of the other parts range from 100 KB to 5 GB. The upload part ID ranges from 1 to 10000.
  930. func (obsClient ObsClient) UploadPart(_input *UploadPartInput, extensions ...extensionOptions) (output *UploadPartOutput, err error) {
  931. if _input == nil {
  932. return nil, errors.New("UploadPartInput is nil")
  933. }
  934. if _input.UploadId == "" {
  935. return nil, errors.New("UploadId is empty")
  936. }
  937. input := &UploadPartInput{}
  938. input.Bucket = _input.Bucket
  939. input.Key = _input.Key
  940. input.PartNumber = _input.PartNumber
  941. input.UploadId = _input.UploadId
  942. input.ContentMD5 = _input.ContentMD5
  943. input.SourceFile = _input.SourceFile
  944. input.Offset = _input.Offset
  945. input.PartSize = _input.PartSize
  946. input.SseHeader = _input.SseHeader
  947. input.Body = _input.Body
  948. output = &UploadPartOutput{}
  949. var repeatable bool
  950. if input.Body != nil {
  951. _, repeatable = input.Body.(*strings.Reader)
  952. if _, ok := input.Body.(*readerWrapper); !ok && input.PartSize > 0 {
  953. input.Body = &readerWrapper{reader: input.Body, totalCount: input.PartSize}
  954. }
  955. } else if sourceFile := strings.TrimSpace(input.SourceFile); sourceFile != "" {
  956. fd, _err := os.Open(sourceFile)
  957. if _err != nil {
  958. err = _err
  959. return nil, err
  960. }
  961. defer func() {
  962. errMsg := fd.Close()
  963. if errMsg != nil {
  964. doLog(LEVEL_WARN, "Failed to close file with reason: %v", errMsg)
  965. }
  966. }()
  967. stat, _err := fd.Stat()
  968. if _err != nil {
  969. err = _err
  970. return nil, err
  971. }
  972. fileSize := stat.Size()
  973. fileReaderWrapper := &fileReaderWrapper{filePath: sourceFile}
  974. fileReaderWrapper.reader = fd
  975. if input.Offset < 0 || input.Offset > fileSize {
  976. input.Offset = 0
  977. }
  978. if input.PartSize <= 0 || input.PartSize > (fileSize-input.Offset) {
  979. input.PartSize = fileSize - input.Offset
  980. }
  981. fileReaderWrapper.totalCount = input.PartSize
  982. if _, err = fd.Seek(input.Offset, io.SeekStart); err != nil {
  983. return nil, err
  984. }
  985. input.Body = fileReaderWrapper
  986. repeatable = true
  987. }
  988. if repeatable {
  989. err = obsClient.doActionWithBucketAndKey("UploadPart", HTTP_PUT, input.Bucket, input.Key, input, output, extensions)
  990. } else {
  991. err = obsClient.doActionWithBucketAndKeyUnRepeatable("UploadPart", HTTP_PUT, input.Bucket, input.Key, input, output, extensions)
  992. }
  993. if err != nil {
  994. output = nil
  995. } else {
  996. ParseUploadPartOutput(output)
  997. output.PartNumber = input.PartNumber
  998. }
  999. return
  1000. }
  1001. // CompleteMultipartUpload combines the uploaded parts in a specified bucket by using the multipart upload ID.
  1002. func (obsClient ObsClient) CompleteMultipartUpload(input *CompleteMultipartUploadInput, extensions ...extensionOptions) (output *CompleteMultipartUploadOutput, err error) {
  1003. if input == nil {
  1004. return nil, errors.New("CompleteMultipartUploadInput is nil")
  1005. }
  1006. if input.UploadId == "" {
  1007. return nil, errors.New("UploadId is empty")
  1008. }
  1009. var parts partSlice = input.Parts
  1010. sort.Sort(parts)
  1011. output = &CompleteMultipartUploadOutput{}
  1012. err = obsClient.doActionWithBucketAndKey("CompleteMultipartUpload", HTTP_POST, input.Bucket, input.Key, input, output, extensions)
  1013. if err != nil {
  1014. output = nil
  1015. } else {
  1016. ParseCompleteMultipartUploadOutput(output)
  1017. }
  1018. return
  1019. }
  1020. // ListParts lists the uploaded parts in a bucket by using the multipart upload ID.
  1021. func (obsClient ObsClient) ListParts(input *ListPartsInput, extensions ...extensionOptions) (output *ListPartsOutput, err error) {
  1022. if input == nil {
  1023. return nil, errors.New("ListPartsInput is nil")
  1024. }
  1025. if input.UploadId == "" {
  1026. return nil, errors.New("UploadId is empty")
  1027. }
  1028. output = &ListPartsOutput{}
  1029. err = obsClient.doActionWithBucketAndKey("ListParts", HTTP_GET, input.Bucket, input.Key, input, output, extensions)
  1030. if err != nil {
  1031. output = nil
  1032. }
  1033. return
  1034. }
  1035. // CopyPart copy a part to a specified bucket by using a specified multipart upload ID.
  1036. //
  1037. // After a multipart upload is initialized, you can use this API to copy a part to a specified bucket by using the multipart upload ID.
  1038. func (obsClient ObsClient) CopyPart(input *CopyPartInput, extensions ...extensionOptions) (output *CopyPartOutput, err error) {
  1039. if input == nil {
  1040. return nil, errors.New("CopyPartInput is nil")
  1041. }
  1042. if input.UploadId == "" {
  1043. return nil, errors.New("UploadId is empty")
  1044. }
  1045. if strings.TrimSpace(input.CopySourceBucket) == "" {
  1046. return nil, errors.New("Source bucket is empty")
  1047. }
  1048. if strings.TrimSpace(input.CopySourceKey) == "" {
  1049. return nil, errors.New("Source key is empty")
  1050. }
  1051. output = &CopyPartOutput{}
  1052. err = obsClient.doActionWithBucketAndKey("CopyPart", HTTP_PUT, input.Bucket, input.Key, input, output, extensions)
  1053. if err != nil {
  1054. output = nil
  1055. } else {
  1056. ParseCopyPartOutput(output)
  1057. output.PartNumber = input.PartNumber
  1058. }
  1059. return
  1060. }
  1061. // SetBucketRequestPayment sets requester-pays setting for a bucket.
  1062. func (obsClient ObsClient) SetBucketRequestPayment(input *SetBucketRequestPaymentInput, extensions ...extensionOptions) (output *BaseModel, err error) {
  1063. if input == nil {
  1064. return nil, errors.New("SetBucketRequestPaymentInput is nil")
  1065. }
  1066. output = &BaseModel{}
  1067. err = obsClient.doActionWithBucket("SetBucketRequestPayment", HTTP_PUT, input.Bucket, input, output, extensions)
  1068. if err != nil {
  1069. output = nil
  1070. }
  1071. return
  1072. }
  1073. // GetBucketRequestPayment gets requester-pays setting of a bucket.
  1074. func (obsClient ObsClient) GetBucketRequestPayment(bucketName string, extensions ...extensionOptions) (output *GetBucketRequestPaymentOutput, err error) {
  1075. output = &GetBucketRequestPaymentOutput{}
  1076. err = obsClient.doActionWithBucket("GetBucketRequestPayment", HTTP_GET, bucketName, newSubResourceSerial(SubResourceRequestPayment), output, extensions)
  1077. if err != nil {
  1078. output = nil
  1079. }
  1080. return
  1081. }
  1082. // UploadFile resume uploads.
  1083. //
  1084. // This API is an encapsulated and enhanced version of multipart upload, and aims to eliminate large file
  1085. // upload failures caused by poor network conditions and program breakdowns.
  1086. func (obsClient ObsClient) UploadFile(input *UploadFileInput, extensions ...extensionOptions) (output *CompleteMultipartUploadOutput, err error) {
  1087. if input.EnableCheckpoint && input.CheckpointFile == "" {
  1088. input.CheckpointFile = input.UploadFile + ".uploadfile_record"
  1089. }
  1090. if input.TaskNum <= 0 {
  1091. input.TaskNum = 1
  1092. }
  1093. if input.PartSize < MIN_PART_SIZE {
  1094. input.PartSize = MIN_PART_SIZE
  1095. } else if input.PartSize > MAX_PART_SIZE {
  1096. input.PartSize = MAX_PART_SIZE
  1097. }
  1098. output, err = obsClient.resumeUpload(input, extensions)
  1099. return
  1100. }
  1101. // DownloadFile resume downloads.
  1102. //
  1103. // This API is an encapsulated and enhanced version of partial download, and aims to eliminate large file
  1104. // download failures caused by poor network conditions and program breakdowns.
  1105. func (obsClient ObsClient) DownloadFile(input *DownloadFileInput, extensions ...extensionOptions) (output *GetObjectMetadataOutput, err error) {
  1106. if input.DownloadFile == "" {
  1107. input.DownloadFile = input.Key
  1108. }
  1109. if input.EnableCheckpoint && input.CheckpointFile == "" {
  1110. input.CheckpointFile = input.DownloadFile + ".downloadfile_record"
  1111. }
  1112. if input.TaskNum <= 0 {
  1113. input.TaskNum = 1
  1114. }
  1115. if input.PartSize <= 0 {
  1116. input.PartSize = DEFAULT_PART_SIZE
  1117. }
  1118. output, err = obsClient.resumeDownload(input, extensions)
  1119. return
  1120. }
  1121. // SetBucketFetchPolicy sets the bucket fetch policy.
  1122. //
  1123. // You can use this API to set a bucket fetch policy.
  1124. func (obsClient ObsClient) SetBucketFetchPolicy(input *SetBucketFetchPolicyInput, extensions ...extensionOptions) (output *BaseModel, err error) {
  1125. if input == nil {
  1126. return nil, errors.New("SetBucketFetchPolicyInput is nil")
  1127. }
  1128. if strings.TrimSpace(string(input.Status)) == "" {
  1129. return nil, errors.New("Fetch policy status is empty")
  1130. }
  1131. if strings.TrimSpace(input.Agency) == "" {
  1132. return nil, errors.New("Fetch policy agency is empty")
  1133. }
  1134. output = &BaseModel{}
  1135. err = obsClient.doActionWithBucketAndKey("SetBucketFetchPolicy", HTTP_PUT, input.Bucket, string(objectKeyExtensionPolicy), input, output, extensions)
  1136. if err != nil {
  1137. output = nil
  1138. }
  1139. return
  1140. }
  1141. // GetBucketFetchPolicy gets the bucket fetch policy.
  1142. //
  1143. // You can use this API to obtain the fetch policy of a bucket.
  1144. func (obsClient ObsClient) GetBucketFetchPolicy(input *GetBucketFetchPolicyInput, extensions ...extensionOptions) (output *GetBucketFetchPolicyOutput, err error) {
  1145. if input == nil {
  1146. return nil, errors.New("GetBucketFetchPolicyInput is nil")
  1147. }
  1148. output = &GetBucketFetchPolicyOutput{}
  1149. err = obsClient.doActionWithBucketAndKeyV2("GetBucketFetchPolicy", HTTP_GET, input.Bucket, string(objectKeyExtensionPolicy), input, output, extensions)
  1150. if err != nil {
  1151. output = nil
  1152. }
  1153. return
  1154. }
  1155. // DeleteBucketFetchPolicy deletes the bucket fetch policy.
  1156. //
  1157. // You can use this API to delete the fetch policy of a bucket.
  1158. func (obsClient ObsClient) DeleteBucketFetchPolicy(input *DeleteBucketFetchPolicyInput, extensions ...extensionOptions) (output *BaseModel, err error) {
  1159. if input == nil {
  1160. return nil, errors.New("DeleteBucketFetchPolicyInput is nil")
  1161. }
  1162. output = &BaseModel{}
  1163. err = obsClient.doActionWithBucketAndKey("DeleteBucketFetchPolicy", HTTP_DELETE, input.Bucket, string(objectKeyExtensionPolicy), input, output, extensions)
  1164. if err != nil {
  1165. output = nil
  1166. }
  1167. return
  1168. }
  1169. // SetBucketFetchJob sets the bucket fetch job.
  1170. //
  1171. // You can use this API to set a bucket fetch job.
  1172. func (obsClient ObsClient) SetBucketFetchJob(input *SetBucketFetchJobInput, extensions ...extensionOptions) (output *SetBucketFetchJobOutput, err error) {
  1173. if input == nil {
  1174. return nil, errors.New("SetBucketFetchJobInput is nil")
  1175. }
  1176. if strings.TrimSpace(input.URL) == "" {
  1177. return nil, errors.New("URL is empty")
  1178. }
  1179. output = &SetBucketFetchJobOutput{}
  1180. err = obsClient.doActionWithBucketAndKeyV2("SetBucketFetchJob", HTTP_POST, input.Bucket, string(objectKeyAsyncFetchJob), input, output, extensions)
  1181. if err != nil {
  1182. output = nil
  1183. }
  1184. return
  1185. }
  1186. // GetBucketFetchJob gets the bucket fetch job.
  1187. //
  1188. // You can use this API to obtain the fetch job of a bucket.
  1189. func (obsClient ObsClient) GetBucketFetchJob(input *GetBucketFetchJobInput, extensions ...extensionOptions) (output *GetBucketFetchJobOutput, err error) {
  1190. if input == nil {
  1191. return nil, errors.New("GetBucketFetchJobInput is nil")
  1192. }
  1193. if strings.TrimSpace(input.JobID) == "" {
  1194. return nil, errors.New("JobID is empty")
  1195. }
  1196. output = &GetBucketFetchJobOutput{}
  1197. err = obsClient.doActionWithBucketAndKeyV2("GetBucketFetchJob", HTTP_GET, input.Bucket, string(objectKeyAsyncFetchJob)+"/"+input.JobID, input, output, extensions)
  1198. if err != nil {
  1199. output = nil
  1200. }
  1201. return
  1202. }