yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/huawei/obs/client.go (about) 1 // Copyright 2019 Yunion 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Copyright 2019 Huawei Technologies Co.,Ltd. 16 // Licensed under the Apache License, Version 2.0 (the "License"); you may not use 17 // this file except in compliance with the License. You may obtain a copy of the 18 // License at 19 // 20 // http://www.apache.org/licenses/LICENSE-2.0 21 // 22 // Unless required by applicable law or agreed to in writing, software distributed 23 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 24 // CONDITIONS OF ANY KIND, either express or implied. See the License for the 25 // specific language governing permissions and limitations under the License. 26 27 package obs 28 29 import ( 30 "errors" 31 "fmt" 32 "io" 33 "net/http" 34 "os" 35 "sort" 36 "strings" 37 ) 38 39 type ObsClient struct { 40 conf *config 41 httpClient *http.Client 42 } 43 44 func (self *ObsClient) GetClient() *http.Client { 45 return self.httpClient 46 } 47 48 func New(ak, sk, endpoint string, configurers ...configurer) (*ObsClient, error) { 49 conf := &config{securityProvider: &securityProvider{ak: ak, sk: sk}, endpoint: endpoint} 50 conf.maxRetryCount = -1 51 conf.maxRedirectCount = -1 52 for _, configurer := range configurers { 53 configurer(conf) 54 } 55 56 if err := conf.initConfigWithDefault(); err != nil { 57 return nil, err 58 } 59 err := conf.getTransport() 60 if err != nil { 61 return nil, err 62 } 63 64 if isWarnLogEnabled() { 65 info := make([]string, 3) 66 info[0] = fmt.Sprintf("[OBS SDK Version=%s", obs_sdk_version) 67 info[1] = fmt.Sprintf("Endpoint=%s", conf.endpoint) 68 accessMode := "Virtual Hosting" 69 if conf.pathStyle { 70 accessMode = "Path" 71 } 72 info[2] = fmt.Sprintf("Access Mode=%s]", accessMode) 73 doLog(LEVEL_WARN, strings.Join(info, "];[")) 74 } 75 doLog(LEVEL_DEBUG, "Create obsclient with config:\n%s\n", conf) 76 obsClient := &ObsClient{conf: conf, httpClient: &http.Client{Transport: conf.transport, CheckRedirect: checkRedirectFunc}} 77 return obsClient, nil 78 } 79 80 func (obsClient ObsClient) Refresh(ak, sk, securityToken string) { 81 sp := &securityProvider{ak: strings.TrimSpace(ak), sk: strings.TrimSpace(sk), securityToken: strings.TrimSpace(securityToken)} 82 obsClient.conf.securityProvider = sp 83 } 84 85 func (obsClient ObsClient) Close() { 86 obsClient.httpClient = nil 87 obsClient.conf.transport.CloseIdleConnections() 88 obsClient.conf = nil 89 SyncLog() 90 } 91 92 func (obsClient ObsClient) ListBuckets(input *ListBucketsInput) (output *ListBucketsOutput, err error) { 93 if input == nil { 94 input = &ListBucketsInput{} 95 } 96 output = &ListBucketsOutput{} 97 err = obsClient.doActionWithoutBucket("ListBuckets", HTTP_GET, input, output) 98 if err != nil { 99 output = nil 100 } 101 return 102 } 103 104 func (obsClient ObsClient) CreateBucket(input *CreateBucketInput) (output *BaseModel, err error) { 105 if input == nil { 106 return nil, errors.New("CreateBucketInput is nil") 107 } 108 output = &BaseModel{} 109 err = obsClient.doActionWithBucket("CreateBucket", HTTP_PUT, input.Bucket, input, output) 110 if err != nil { 111 output = nil 112 } 113 return 114 } 115 116 func (obsClient ObsClient) DeleteBucket(bucketName string) (output *BaseModel, err error) { 117 output = &BaseModel{} 118 err = obsClient.doActionWithBucket("DeleteBucket", HTTP_DELETE, bucketName, defaultSerializable, output) 119 if err != nil { 120 output = nil 121 } 122 return 123 } 124 125 func (obsClient ObsClient) SetBucketStoragePolicy(input *SetBucketStoragePolicyInput) (output *BaseModel, err error) { 126 if input == nil { 127 return nil, errors.New("SetBucketStoragePolicyInput is nil") 128 } 129 output = &BaseModel{} 130 err = obsClient.doActionWithBucket("SetBucketStoragePolicy", HTTP_PUT, input.Bucket, input, output) 131 if err != nil { 132 output = nil 133 } 134 return 135 } 136 func (obsClient ObsClient) getBucketStoragePolicyS3(bucketName string) (output *GetBucketStoragePolicyOutput, err error) { 137 output = &GetBucketStoragePolicyOutput{} 138 var outputS3 *getBucketStoragePolicyOutputS3 139 outputS3 = &getBucketStoragePolicyOutputS3{} 140 err = obsClient.doActionWithBucket("GetBucketStoragePolicy", HTTP_GET, bucketName, newSubResourceSerial(SubResourceStoragePolicy), outputS3) 141 if err != nil { 142 output = nil 143 return 144 } 145 output.BaseModel = outputS3.BaseModel 146 output.StorageClass = fmt.Sprintf("%s", outputS3.StorageClass) 147 return 148 } 149 150 func (obsClient ObsClient) getBucketStoragePolicyObs(bucketName string) (output *GetBucketStoragePolicyOutput, err error) { 151 output = &GetBucketStoragePolicyOutput{} 152 var outputObs *getBucketStoragePolicyOutputObs 153 outputObs = &getBucketStoragePolicyOutputObs{} 154 err = obsClient.doActionWithBucket("GetBucketStoragePolicy", HTTP_GET, bucketName, newSubResourceSerial(SubResourceStorageClass), outputObs) 155 if err != nil { 156 output = nil 157 return 158 } 159 output.BaseModel = outputObs.BaseModel 160 output.StorageClass = outputObs.StorageClass 161 return 162 } 163 func (obsClient ObsClient) GetBucketStoragePolicy(bucketName string) (output *GetBucketStoragePolicyOutput, err error) { 164 if obsClient.conf.signature == SignatureObs { 165 return obsClient.getBucketStoragePolicyObs(bucketName) 166 } 167 return obsClient.getBucketStoragePolicyS3(bucketName) 168 } 169 170 func (obsClient ObsClient) ListObjects(input *ListObjectsInput) (output *ListObjectsOutput, err error) { 171 if input == nil { 172 return nil, errors.New("ListObjectsInput is nil") 173 } 174 output = &ListObjectsOutput{} 175 err = obsClient.doActionWithBucket("ListObjects", HTTP_GET, input.Bucket, input, output) 176 if err != nil { 177 output = nil 178 } else { 179 if location, ok := output.ResponseHeaders[HEADER_BUCKET_REGION]; ok { 180 output.Location = location[0] 181 } 182 } 183 return 184 } 185 186 func (obsClient ObsClient) ListVersions(input *ListVersionsInput) (output *ListVersionsOutput, err error) { 187 if input == nil { 188 return nil, errors.New("ListVersionsInput is nil") 189 } 190 output = &ListVersionsOutput{} 191 err = obsClient.doActionWithBucket("ListVersions", HTTP_GET, input.Bucket, input, output) 192 if err != nil { 193 output = nil 194 } else { 195 if location, ok := output.ResponseHeaders[HEADER_BUCKET_REGION]; ok { 196 output.Location = location[0] 197 } 198 } 199 return 200 } 201 202 func (obsClient ObsClient) ListMultipartUploads(input *ListMultipartUploadsInput) (output *ListMultipartUploadsOutput, err error) { 203 if input == nil { 204 return nil, errors.New("ListMultipartUploadsInput is nil") 205 } 206 output = &ListMultipartUploadsOutput{} 207 err = obsClient.doActionWithBucket("ListMultipartUploads", HTTP_GET, input.Bucket, input, output) 208 if err != nil { 209 output = nil 210 } 211 return 212 } 213 214 func (obsClient ObsClient) SetBucketQuota(input *SetBucketQuotaInput) (output *BaseModel, err error) { 215 if input == nil { 216 return nil, errors.New("SetBucketQuotaInput is nil") 217 } 218 output = &BaseModel{} 219 err = obsClient.doActionWithBucket("SetBucketQuota", HTTP_PUT, input.Bucket, input, output) 220 if err != nil { 221 output = nil 222 } 223 return 224 } 225 226 func (obsClient ObsClient) GetBucketQuota(bucketName string) (output *GetBucketQuotaOutput, err error) { 227 output = &GetBucketQuotaOutput{} 228 err = obsClient.doActionWithBucket("GetBucketQuota", HTTP_GET, bucketName, newSubResourceSerial(SubResourceQuota), output) 229 if err != nil { 230 output = nil 231 } 232 return 233 } 234 235 func (obsClient ObsClient) HeadBucket(bucketName string) (output *BaseModel, err error) { 236 output = &BaseModel{} 237 err = obsClient.doActionWithBucket("HeadBucket", HTTP_HEAD, bucketName, defaultSerializable, output) 238 if err != nil { 239 output = nil 240 } 241 return 242 } 243 244 func (obsClient ObsClient) GetBucketMetadata(input *GetBucketMetadataInput) (output *GetBucketMetadataOutput, err error) { 245 output = &GetBucketMetadataOutput{} 246 err = obsClient.doActionWithBucket("GetBucketMetadata", HTTP_HEAD, input.Bucket, input, output) 247 if err != nil { 248 output = nil 249 } else { 250 ParseGetBucketMetadataOutput(output) 251 } 252 return 253 } 254 255 func (obsClient ObsClient) SetObjectMetadata(input *SetObjectMetadataInput) (output *SetObjectMetadataOutput, err error) { 256 output = &SetObjectMetadataOutput{} 257 err = obsClient.doActionWithBucketAndKey("SetObjectMetadata", HTTP_PUT, input.Bucket, input.Key, input, output) 258 if err != nil { 259 output = nil 260 } else { 261 ParseSetObjectMetadataOutput(output) 262 } 263 return 264 } 265 266 func (obsClient ObsClient) GetBucketStorageInfo(bucketName string) (output *GetBucketStorageInfoOutput, err error) { 267 output = &GetBucketStorageInfoOutput{} 268 err = obsClient.doActionWithBucket("GetBucketStorageInfo", HTTP_GET, bucketName, newSubResourceSerial(SubResourceStorageInfo), output) 269 if err != nil { 270 output = nil 271 } 272 return 273 } 274 275 func (obsClient ObsClient) getBucketLocationS3(bucketName string) (output *GetBucketLocationOutput, err error) { 276 output = &GetBucketLocationOutput{} 277 var outputS3 *getBucketLocationOutputS3 278 outputS3 = &getBucketLocationOutputS3{} 279 err = obsClient.doActionWithBucket("GetBucketLocation", HTTP_GET, bucketName, newSubResourceSerial(SubResourceLocation), outputS3) 280 if err != nil { 281 output = nil 282 } else { 283 output.BaseModel = outputS3.BaseModel 284 output.Location = outputS3.Location 285 } 286 return 287 } 288 func (obsClient ObsClient) getBucketLocationObs(bucketName string) (output *GetBucketLocationOutput, err error) { 289 output = &GetBucketLocationOutput{} 290 var outputObs *getBucketLocationOutputObs 291 outputObs = &getBucketLocationOutputObs{} 292 err = obsClient.doActionWithBucket("GetBucketLocation", HTTP_GET, bucketName, newSubResourceSerial(SubResourceLocation), outputObs) 293 if err != nil { 294 output = nil 295 } else { 296 output.BaseModel = outputObs.BaseModel 297 output.Location = outputObs.Location 298 } 299 return 300 } 301 func (obsClient ObsClient) GetBucketLocation(bucketName string) (output *GetBucketLocationOutput, err error) { 302 if obsClient.conf.signature == SignatureObs { 303 return obsClient.getBucketLocationObs(bucketName) 304 } 305 return obsClient.getBucketLocationS3(bucketName) 306 } 307 308 func (obsClient ObsClient) SetBucketAcl(input *SetBucketAclInput) (output *BaseModel, err error) { 309 if input == nil { 310 return nil, errors.New("SetBucketAclInput is nil") 311 } 312 output = &BaseModel{} 313 err = obsClient.doActionWithBucket("SetBucketAcl", HTTP_PUT, input.Bucket, input, output) 314 if err != nil { 315 output = nil 316 } 317 return 318 } 319 func (obsClient ObsClient) getBucketAclObs(bucketName string) (output *GetBucketAclOutput, err error) { 320 output = &GetBucketAclOutput{} 321 var outputObs *getBucketAclOutputObs 322 outputObs = &getBucketAclOutputObs{} 323 err = obsClient.doActionWithBucket("GetBucketAcl", HTTP_GET, bucketName, newSubResourceSerial(SubResourceAcl), outputObs) 324 if err != nil { 325 output = nil 326 } else { 327 output.BaseModel = outputObs.BaseModel 328 output.Owner = outputObs.Owner 329 output.Grants = make([]Grant, 0, len(outputObs.Grants)) 330 for _, valGrant := range outputObs.Grants { 331 tempOutput := Grant{} 332 tempOutput.Delivered = valGrant.Delivered 333 tempOutput.Permission = valGrant.Permission 334 tempOutput.Grantee.DisplayName = valGrant.Grantee.DisplayName 335 tempOutput.Grantee.ID = valGrant.Grantee.ID 336 tempOutput.Grantee.Type = valGrant.Grantee.Type 337 tempOutput.Grantee.URI = GroupAllUsers 338 339 output.Grants = append(output.Grants, tempOutput) 340 } 341 } 342 return 343 } 344 func (obsClient ObsClient) GetBucketAcl(bucketName string) (output *GetBucketAclOutput, err error) { 345 output = &GetBucketAclOutput{} 346 if obsClient.conf.signature == SignatureObs { 347 return obsClient.getBucketAclObs(bucketName) 348 } 349 err = obsClient.doActionWithBucket("GetBucketAcl", HTTP_GET, bucketName, newSubResourceSerial(SubResourceAcl), output) 350 if err != nil { 351 output = nil 352 } 353 return 354 } 355 356 func (obsClient ObsClient) SetBucketPolicy(input *SetBucketPolicyInput) (output *BaseModel, err error) { 357 if input == nil { 358 return nil, errors.New("SetBucketPolicy is nil") 359 } 360 output = &BaseModel{} 361 err = obsClient.doActionWithBucket("SetBucketPolicy", HTTP_PUT, input.Bucket, input, output) 362 if err != nil { 363 output = nil 364 } 365 return 366 } 367 368 func (obsClient ObsClient) GetBucketPolicy(bucketName string) (output *GetBucketPolicyOutput, err error) { 369 output = &GetBucketPolicyOutput{} 370 err = obsClient.doActionWithBucketV2("GetBucketPolicy", HTTP_GET, bucketName, newSubResourceSerial(SubResourcePolicy), output) 371 if err != nil { 372 output = nil 373 } 374 return 375 } 376 377 func (obsClient ObsClient) DeleteBucketPolicy(bucketName string) (output *BaseModel, err error) { 378 output = &BaseModel{} 379 err = obsClient.doActionWithBucket("DeleteBucketPolicy", HTTP_DELETE, bucketName, newSubResourceSerial(SubResourcePolicy), output) 380 if err != nil { 381 output = nil 382 } 383 return 384 } 385 386 func (obsClient ObsClient) SetBucketCors(input *SetBucketCorsInput) (output *BaseModel, err error) { 387 if input == nil { 388 return nil, errors.New("SetBucketCorsInput is nil") 389 } 390 output = &BaseModel{} 391 err = obsClient.doActionWithBucket("SetBucketCors", HTTP_PUT, input.Bucket, input, output) 392 if err != nil { 393 output = nil 394 } 395 return 396 } 397 398 func (obsClient ObsClient) GetBucketCors(bucketName string) (output *GetBucketCorsOutput, err error) { 399 output = &GetBucketCorsOutput{} 400 err = obsClient.doActionWithBucket("GetBucketCors", HTTP_GET, bucketName, newSubResourceSerial(SubResourceCors), output) 401 if err != nil { 402 output = nil 403 } 404 return 405 } 406 407 func (obsClient ObsClient) DeleteBucketCors(bucketName string) (output *BaseModel, err error) { 408 output = &BaseModel{} 409 err = obsClient.doActionWithBucket("DeleteBucketCors", HTTP_DELETE, bucketName, newSubResourceSerial(SubResourceCors), output) 410 if err != nil { 411 output = nil 412 } 413 return 414 } 415 416 func (obsClient ObsClient) SetBucketVersioning(input *SetBucketVersioningInput) (output *BaseModel, err error) { 417 if input == nil { 418 return nil, errors.New("SetBucketVersioningInput is nil") 419 } 420 output = &BaseModel{} 421 err = obsClient.doActionWithBucket("SetBucketVersioning", HTTP_PUT, input.Bucket, input, output) 422 if err != nil { 423 output = nil 424 } 425 return 426 } 427 428 func (obsClient ObsClient) GetBucketVersioning(bucketName string) (output *GetBucketVersioningOutput, err error) { 429 output = &GetBucketVersioningOutput{} 430 err = obsClient.doActionWithBucket("GetBucketVersioning", HTTP_GET, bucketName, newSubResourceSerial(SubResourceVersioning), output) 431 if err != nil { 432 output = nil 433 } 434 return 435 } 436 437 func (obsClient ObsClient) SetBucketWebsiteConfiguration(input *SetBucketWebsiteConfigurationInput) (output *BaseModel, err error) { 438 if input == nil { 439 return nil, errors.New("SetBucketWebsiteConfigurationInput is nil") 440 } 441 output = &BaseModel{} 442 err = obsClient.doActionWithBucket("SetBucketWebsiteConfiguration", HTTP_PUT, input.Bucket, input, output) 443 if err != nil { 444 output = nil 445 } 446 return 447 } 448 449 func (obsClient ObsClient) GetBucketWebsiteConfiguration(bucketName string) (output *GetBucketWebsiteConfigurationOutput, err error) { 450 output = &GetBucketWebsiteConfigurationOutput{} 451 err = obsClient.doActionWithBucket("GetBucketWebsiteConfiguration", HTTP_GET, bucketName, newSubResourceSerial(SubResourceWebsite), output) 452 if err != nil { 453 output = nil 454 } 455 return 456 } 457 458 func (obsClient ObsClient) DeleteBucketWebsiteConfiguration(bucketName string) (output *BaseModel, err error) { 459 output = &BaseModel{} 460 err = obsClient.doActionWithBucket("DeleteBucketWebsiteConfiguration", HTTP_DELETE, bucketName, newSubResourceSerial(SubResourceWebsite), output) 461 if err != nil { 462 output = nil 463 } 464 return 465 } 466 467 func (obsClient ObsClient) SetBucketLoggingConfiguration(input *SetBucketLoggingConfigurationInput) (output *BaseModel, err error) { 468 if input == nil { 469 return nil, errors.New("SetBucketLoggingConfigurationInput is nil") 470 } 471 output = &BaseModel{} 472 err = obsClient.doActionWithBucket("SetBucketLoggingConfiguration", HTTP_PUT, input.Bucket, input, output) 473 if err != nil { 474 output = nil 475 } 476 return 477 } 478 479 func (obsClient ObsClient) GetBucketLoggingConfiguration(bucketName string) (output *GetBucketLoggingConfigurationOutput, err error) { 480 output = &GetBucketLoggingConfigurationOutput{} 481 err = obsClient.doActionWithBucket("GetBucketLoggingConfiguration", HTTP_GET, bucketName, newSubResourceSerial(SubResourceLogging), output) 482 if err != nil { 483 output = nil 484 } 485 return 486 } 487 488 func (obsClient ObsClient) SetBucketLifecycleConfiguration(input *SetBucketLifecycleConfigurationInput) (output *BaseModel, err error) { 489 if input == nil { 490 return nil, errors.New("SetBucketLifecycleConfigurationInput is nil") 491 } 492 output = &BaseModel{} 493 err = obsClient.doActionWithBucket("SetBucketLifecycleConfiguration", HTTP_PUT, input.Bucket, input, output) 494 if err != nil { 495 output = nil 496 } 497 return 498 } 499 500 func (obsClient ObsClient) GetBucketLifecycleConfiguration(bucketName string) (output *GetBucketLifecycleConfigurationOutput, err error) { 501 output = &GetBucketLifecycleConfigurationOutput{} 502 err = obsClient.doActionWithBucket("GetBucketLifecycleConfiguration", HTTP_GET, bucketName, newSubResourceSerial(SubResourceLifecycle), output) 503 if err != nil { 504 output = nil 505 } 506 return 507 } 508 509 func (obsClient ObsClient) DeleteBucketLifecycleConfiguration(bucketName string) (output *BaseModel, err error) { 510 output = &BaseModel{} 511 err = obsClient.doActionWithBucket("DeleteBucketLifecycleConfiguration", HTTP_DELETE, bucketName, newSubResourceSerial(SubResourceLifecycle), output) 512 if err != nil { 513 output = nil 514 } 515 return 516 } 517 518 func (obsClient ObsClient) SetBucketTagging(input *SetBucketTaggingInput) (output *BaseModel, err error) { 519 if input == nil { 520 return nil, errors.New("SetBucketTaggingInput is nil") 521 } 522 output = &BaseModel{} 523 err = obsClient.doActionWithBucket("SetBucketTagging", HTTP_PUT, input.Bucket, input, output) 524 if err != nil { 525 output = nil 526 } 527 return 528 } 529 530 func (obsClient ObsClient) GetBucketTagging(bucketName string) (output *GetBucketTaggingOutput, err error) { 531 output = &GetBucketTaggingOutput{} 532 err = obsClient.doActionWithBucket("GetBucketTagging", HTTP_GET, bucketName, newSubResourceSerial(SubResourceTagging), output) 533 if err != nil { 534 output = nil 535 } 536 return 537 } 538 539 func (obsClient ObsClient) DeleteBucketTagging(bucketName string) (output *BaseModel, err error) { 540 output = &BaseModel{} 541 err = obsClient.doActionWithBucket("DeleteBucketTagging", HTTP_DELETE, bucketName, newSubResourceSerial(SubResourceTagging), output) 542 if err != nil { 543 output = nil 544 } 545 return 546 } 547 548 func (obsClient ObsClient) SetBucketNotification(input *SetBucketNotificationInput) (output *BaseModel, err error) { 549 if input == nil { 550 return nil, errors.New("SetBucketNotificationInput is nil") 551 } 552 output = &BaseModel{} 553 err = obsClient.doActionWithBucket("SetBucketNotification", HTTP_PUT, input.Bucket, input, output) 554 if err != nil { 555 output = nil 556 } 557 return 558 } 559 560 func (obsClient ObsClient) GetBucketNotification(bucketName string) (output *GetBucketNotificationOutput, err error) { 561 if obsClient.conf.signature != SignatureObs { 562 return obsClient.getBucketNotificationS3(bucketName) 563 } 564 output = &GetBucketNotificationOutput{} 565 err = obsClient.doActionWithBucket("GetBucketNotification", HTTP_GET, bucketName, newSubResourceSerial(SubResourceNotification), output) 566 if err != nil { 567 output = nil 568 } 569 return 570 } 571 572 func (obsClient ObsClient) getBucketNotificationS3(bucketName string) (output *GetBucketNotificationOutput, err error) { 573 outputS3 := &getBucketNotificationOutputS3{} 574 err = obsClient.doActionWithBucket("GetBucketNotification", HTTP_GET, bucketName, newSubResourceSerial(SubResourceNotification), outputS3) 575 if err != nil { 576 return nil, err 577 } 578 579 output = &GetBucketNotificationOutput{} 580 output.BaseModel = outputS3.BaseModel 581 topicConfigurations := make([]TopicConfiguration, 0, len(outputS3.TopicConfigurations)) 582 for _, topicConfigurationS3 := range outputS3.TopicConfigurations { 583 topicConfiguration := TopicConfiguration{} 584 topicConfiguration.ID = topicConfigurationS3.ID 585 topicConfiguration.Topic = topicConfigurationS3.Topic 586 topicConfiguration.FilterRules = topicConfigurationS3.FilterRules 587 588 events := make([]EventType, 0, len(topicConfigurationS3.Events)) 589 for _, event := range topicConfigurationS3.Events { 590 events = append(events, ParseStringToEventType(event)) 591 } 592 topicConfiguration.Events = events 593 topicConfigurations = append(topicConfigurations, topicConfiguration) 594 } 595 output.TopicConfigurations = topicConfigurations 596 return 597 } 598 599 func (obsClient ObsClient) DeleteObject(input *DeleteObjectInput) (output *DeleteObjectOutput, err error) { 600 if input == nil { 601 return nil, errors.New("DeleteObjectInput is nil") 602 } 603 output = &DeleteObjectOutput{} 604 err = obsClient.doActionWithBucketAndKey("DeleteObject", HTTP_DELETE, input.Bucket, input.Key, input, output) 605 if err != nil { 606 output = nil 607 } else { 608 ParseDeleteObjectOutput(output) 609 } 610 return 611 } 612 613 func (obsClient ObsClient) DeleteObjects(input *DeleteObjectsInput) (output *DeleteObjectsOutput, err error) { 614 if input == nil { 615 return nil, errors.New("DeleteObjectsInput is nil") 616 } 617 output = &DeleteObjectsOutput{} 618 err = obsClient.doActionWithBucket("DeleteObjects", HTTP_POST, input.Bucket, input, output) 619 if err != nil { 620 output = nil 621 } 622 return 623 } 624 625 func (obsClient ObsClient) SetObjectAcl(input *SetObjectAclInput) (output *BaseModel, err error) { 626 if input == nil { 627 return nil, errors.New("SetObjectAclInput is nil") 628 } 629 output = &BaseModel{} 630 err = obsClient.doActionWithBucketAndKey("SetObjectAcl", HTTP_PUT, input.Bucket, input.Key, input, output) 631 if err != nil { 632 output = nil 633 } 634 return 635 } 636 637 func (obsClient ObsClient) GetObjectAcl(input *GetObjectAclInput) (output *GetObjectAclOutput, err error) { 638 if input == nil { 639 return nil, errors.New("GetObjectAclInput is nil") 640 } 641 output = &GetObjectAclOutput{} 642 err = obsClient.doActionWithBucketAndKey("GetObjectAcl", HTTP_GET, input.Bucket, input.Key, input, output) 643 if err != nil { 644 output = nil 645 } else { 646 if versionId, ok := output.ResponseHeaders[HEADER_VERSION_ID]; ok { 647 output.VersionId = versionId[0] 648 } 649 } 650 return 651 } 652 653 func (obsClient ObsClient) RestoreObject(input *RestoreObjectInput) (output *BaseModel, err error) { 654 if input == nil { 655 return nil, errors.New("RestoreObjectInput is nil") 656 } 657 output = &BaseModel{} 658 err = obsClient.doActionWithBucketAndKey("RestoreObject", HTTP_POST, input.Bucket, input.Key, input, output) 659 if err != nil { 660 output = nil 661 } 662 return 663 } 664 665 func (obsClient ObsClient) GetObjectMetadata(input *GetObjectMetadataInput) (output *GetObjectMetadataOutput, err error) { 666 if input == nil { 667 return nil, errors.New("GetObjectMetadataInput is nil") 668 } 669 output = &GetObjectMetadataOutput{} 670 err = obsClient.doActionWithBucketAndKey("GetObjectMetadata", HTTP_HEAD, input.Bucket, input.Key, input, output) 671 if err != nil { 672 output = nil 673 } else { 674 ParseGetObjectMetadataOutput(output) 675 } 676 return 677 } 678 679 func (obsClient ObsClient) GetObject(input *GetObjectInput) (output *GetObjectOutput, err error) { 680 if input == nil { 681 return nil, errors.New("GetObjectInput is nil") 682 } 683 output = &GetObjectOutput{} 684 err = obsClient.doActionWithBucketAndKey("GetObject", HTTP_GET, input.Bucket, input.Key, input, output) 685 if err != nil { 686 output = nil 687 } else { 688 ParseGetObjectOutput(output) 689 } 690 return 691 } 692 693 func (obsClient ObsClient) PutObject(input *PutObjectInput) (output *PutObjectOutput, err error) { 694 if input == nil { 695 return nil, errors.New("PutObjectInput is nil") 696 } 697 698 if input.ContentType == "" && input.Key != "" { 699 if contentType, ok := mime_types[strings.ToLower(input.Key[strings.LastIndex(input.Key, ".")+1:])]; ok { 700 input.ContentType = contentType 701 } 702 } 703 704 output = &PutObjectOutput{} 705 var repeatable bool 706 if input.Body != nil { 707 _, repeatable = input.Body.(*strings.Reader) 708 if input.ContentLength > 0 { 709 input.Body = &readerWrapper{reader: input.Body, totalCount: input.ContentLength} 710 } 711 } 712 if repeatable { 713 err = obsClient.doActionWithBucketAndKey("PutObject", HTTP_PUT, input.Bucket, input.Key, input, output) 714 } else { 715 err = obsClient.doActionWithBucketAndKeyUnRepeatable("PutObject", HTTP_PUT, input.Bucket, input.Key, input, output) 716 } 717 if err != nil { 718 output = nil 719 } else { 720 ParsePutObjectOutput(output) 721 } 722 return 723 } 724 725 func (obsClient ObsClient) getContentType(input *PutObjectInput, sourceFile string) (contentType string) { 726 if contentType, ok := mime_types[strings.ToLower(input.Key[strings.LastIndex(input.Key, ".")+1:])]; ok { 727 return contentType 728 } 729 if contentType, ok := mime_types[strings.ToLower(sourceFile[strings.LastIndex(sourceFile, ".")+1:])]; ok { 730 return contentType 731 } 732 return 733 } 734 735 func (ObsClient ObsClient) isGetContentType(input *PutObjectInput) bool { 736 if input.ContentType == "" && input.Key != "" { 737 return true 738 } 739 return false 740 } 741 742 func (obsClient ObsClient) PutFile(input *PutFileInput) (output *PutObjectOutput, err error) { 743 if input == nil { 744 return nil, errors.New("PutFileInput is nil") 745 } 746 747 var body io.Reader 748 sourceFile := strings.TrimSpace(input.SourceFile) 749 if sourceFile != "" { 750 fd, _err := os.Open(sourceFile) 751 if _err != nil { 752 err = _err 753 return nil, err 754 } 755 defer func() { 756 errMsg := fd.Close() 757 if errMsg != nil { 758 doLog(LEVEL_WARN, "Failed to close file with reason: %v", errMsg) 759 } 760 }() 761 762 stat, _err := fd.Stat() 763 if _err != nil { 764 err = _err 765 return nil, err 766 } 767 fileReaderWrapper := &fileReaderWrapper{filePath: sourceFile} 768 fileReaderWrapper.reader = fd 769 if input.ContentLength > 0 { 770 if input.ContentLength > stat.Size() { 771 input.ContentLength = stat.Size() 772 } 773 fileReaderWrapper.totalCount = input.ContentLength 774 } else { 775 fileReaderWrapper.totalCount = stat.Size() 776 } 777 body = fileReaderWrapper 778 } 779 780 _input := &PutObjectInput{} 781 _input.PutObjectBasicInput = input.PutObjectBasicInput 782 _input.Body = body 783 784 if obsClient.isGetContentType(_input) { 785 _input.ContentType = obsClient.getContentType(_input, sourceFile) 786 } 787 788 output = &PutObjectOutput{} 789 err = obsClient.doActionWithBucketAndKey("PutFile", HTTP_PUT, _input.Bucket, _input.Key, _input, output) 790 if err != nil { 791 output = nil 792 } else { 793 ParsePutObjectOutput(output) 794 } 795 return 796 } 797 798 func (obsClient ObsClient) CopyObject(input *CopyObjectInput) (output *CopyObjectOutput, err error) { 799 if input == nil { 800 return nil, errors.New("CopyObjectInput is nil") 801 } 802 803 if strings.TrimSpace(input.CopySourceBucket) == "" { 804 return nil, errors.New("Source bucket is empty") 805 } 806 if strings.TrimSpace(input.CopySourceKey) == "" { 807 return nil, errors.New("Source key is empty") 808 } 809 810 output = &CopyObjectOutput{} 811 err = obsClient.doActionWithBucketAndKey("CopyObject", HTTP_PUT, input.Bucket, input.Key, input, output) 812 if err != nil { 813 output = nil 814 } else { 815 ParseCopyObjectOutput(output) 816 } 817 return 818 } 819 820 func (obsClient ObsClient) AbortMultipartUpload(input *AbortMultipartUploadInput) (output *BaseModel, err error) { 821 if input == nil { 822 return nil, errors.New("AbortMultipartUploadInput is nil") 823 } 824 if input.UploadId == "" { 825 return nil, errors.New("UploadId is empty") 826 } 827 output = &BaseModel{} 828 err = obsClient.doActionWithBucketAndKey("AbortMultipartUpload", HTTP_DELETE, input.Bucket, input.Key, input, output) 829 if err != nil { 830 output = nil 831 } 832 return 833 } 834 835 func (obsClient ObsClient) InitiateMultipartUpload(input *InitiateMultipartUploadInput) (output *InitiateMultipartUploadOutput, err error) { 836 if input == nil { 837 return nil, errors.New("InitiateMultipartUploadInput is nil") 838 } 839 840 if input.ContentType == "" && input.Key != "" { 841 if contentType, ok := mime_types[strings.ToLower(input.Key[strings.LastIndex(input.Key, ".")+1:])]; ok { 842 input.ContentType = contentType 843 } 844 } 845 846 output = &InitiateMultipartUploadOutput{} 847 err = obsClient.doActionWithBucketAndKey("InitiateMultipartUpload", HTTP_POST, input.Bucket, input.Key, input, output) 848 if err != nil { 849 output = nil 850 } else { 851 ParseInitiateMultipartUploadOutput(output) 852 } 853 return 854 } 855 856 func (obsClient ObsClient) UploadPart(_input *UploadPartInput) (output *UploadPartOutput, err error) { 857 if _input == nil { 858 return nil, errors.New("UploadPartInput is nil") 859 } 860 861 if _input.UploadId == "" { 862 return nil, errors.New("UploadId is empty") 863 } 864 865 input := &UploadPartInput{} 866 input.Bucket = _input.Bucket 867 input.Key = _input.Key 868 input.PartNumber = _input.PartNumber 869 input.UploadId = _input.UploadId 870 input.ContentMD5 = _input.ContentMD5 871 input.SourceFile = _input.SourceFile 872 input.Offset = _input.Offset 873 input.PartSize = _input.PartSize 874 input.SseHeader = _input.SseHeader 875 input.Body = _input.Body 876 877 output = &UploadPartOutput{} 878 var repeatable bool 879 if input.Body != nil { 880 _, repeatable = input.Body.(*strings.Reader) 881 if _, ok := input.Body.(*readerWrapper); !ok && input.PartSize > 0 { 882 input.Body = &readerWrapper{reader: input.Body, totalCount: input.PartSize} 883 } 884 } else if sourceFile := strings.TrimSpace(input.SourceFile); sourceFile != "" { 885 fd, _err := os.Open(sourceFile) 886 if _err != nil { 887 err = _err 888 return nil, err 889 } 890 defer func() { 891 errMsg := fd.Close() 892 if errMsg != nil { 893 doLog(LEVEL_WARN, "Failed to close file with reason: %v", errMsg) 894 } 895 }() 896 897 stat, _err := fd.Stat() 898 if _err != nil { 899 err = _err 900 return nil, err 901 } 902 fileSize := stat.Size() 903 fileReaderWrapper := &fileReaderWrapper{filePath: sourceFile} 904 fileReaderWrapper.reader = fd 905 906 if input.Offset < 0 || input.Offset > fileSize { 907 input.Offset = 0 908 } 909 910 if input.PartSize <= 0 || input.PartSize > (fileSize-input.Offset) { 911 input.PartSize = fileSize - input.Offset 912 } 913 fileReaderWrapper.totalCount = input.PartSize 914 if _, err = fd.Seek(input.Offset, io.SeekStart); err != nil { 915 return nil, err 916 } 917 input.Body = fileReaderWrapper 918 repeatable = true 919 } 920 if repeatable { 921 err = obsClient.doActionWithBucketAndKey("UploadPart", HTTP_PUT, input.Bucket, input.Key, input, output) 922 } else { 923 err = obsClient.doActionWithBucketAndKeyUnRepeatable("UploadPart", HTTP_PUT, input.Bucket, input.Key, input, output) 924 } 925 if err != nil { 926 output = nil 927 } else { 928 ParseUploadPartOutput(output) 929 output.PartNumber = input.PartNumber 930 } 931 return 932 } 933 934 func (obsClient ObsClient) CompleteMultipartUpload(input *CompleteMultipartUploadInput) (output *CompleteMultipartUploadOutput, err error) { 935 if input == nil { 936 return nil, errors.New("CompleteMultipartUploadInput is nil") 937 } 938 939 if input.UploadId == "" { 940 return nil, errors.New("UploadId is empty") 941 } 942 943 var parts partSlice = input.Parts 944 sort.Sort(parts) 945 946 output = &CompleteMultipartUploadOutput{} 947 err = obsClient.doActionWithBucketAndKey("CompleteMultipartUpload", HTTP_POST, input.Bucket, input.Key, input, output) 948 if err != nil { 949 output = nil 950 } else { 951 ParseCompleteMultipartUploadOutput(output) 952 } 953 return 954 } 955 956 func (obsClient ObsClient) ListParts(input *ListPartsInput) (output *ListPartsOutput, err error) { 957 if input == nil { 958 return nil, errors.New("ListPartsInput is nil") 959 } 960 if input.UploadId == "" { 961 return nil, errors.New("UploadId is empty") 962 } 963 output = &ListPartsOutput{} 964 err = obsClient.doActionWithBucketAndKey("ListParts", HTTP_GET, input.Bucket, input.Key, input, output) 965 if err != nil { 966 output = nil 967 } 968 return 969 } 970 971 func (obsClient ObsClient) CopyPart(input *CopyPartInput) (output *CopyPartOutput, err error) { 972 if input == nil { 973 return nil, errors.New("CopyPartInput is nil") 974 } 975 if input.UploadId == "" { 976 return nil, errors.New("UploadId is empty") 977 } 978 if strings.TrimSpace(input.CopySourceBucket) == "" { 979 return nil, errors.New("Source bucket is empty") 980 } 981 if strings.TrimSpace(input.CopySourceKey) == "" { 982 return nil, errors.New("Source key is empty") 983 } 984 985 output = &CopyPartOutput{} 986 err = obsClient.doActionWithBucketAndKey("CopyPart", HTTP_PUT, input.Bucket, input.Key, input, output) 987 if err != nil { 988 output = nil 989 } else { 990 ParseCopyPartOutput(output) 991 output.PartNumber = input.PartNumber 992 } 993 return 994 }