github.com/aliyun/aliyun-oss-go-sdk@v3.0.2+incompatible/oss/client.go (about) 1 // Package oss implements functions for access oss service. 2 // It has two main struct Client and Bucket. 3 package oss 4 5 import ( 6 "bytes" 7 "encoding/xml" 8 "fmt" 9 "io" 10 "io/ioutil" 11 "log" 12 "net" 13 "net/http" 14 "strings" 15 "time" 16 ) 17 18 // Client SDK's entry point. It's for bucket related options such as create/delete/set bucket (such as set/get ACL/lifecycle/referer/logging/website). 19 // Object related operations are done by Bucket class. 20 // Users use oss.New to create Client instance. 21 // 22 type ( 23 // Client OSS client 24 Client struct { 25 Config *Config // OSS client configuration 26 Conn *Conn // Send HTTP request 27 HTTPClient *http.Client //http.Client to use - if nil will make its own 28 } 29 30 // ClientOption client option such as UseCname, Timeout, SecurityToken. 31 ClientOption func(*Client) 32 ) 33 34 // New creates a new client. 35 // 36 // endpoint the OSS datacenter endpoint such as http://oss-cn-hangzhou.aliyuncs.com . 37 // accessKeyId access key Id. 38 // accessKeySecret access key secret. 39 // 40 // Client creates the new client instance, the returned value is valid when error is nil. 41 // error it's nil if no error, otherwise it's an error object. 42 // 43 func New(endpoint, accessKeyID, accessKeySecret string, options ...ClientOption) (*Client, error) { 44 // Configuration 45 config := getDefaultOssConfig() 46 config.Endpoint = endpoint 47 config.AccessKeyID = accessKeyID 48 config.AccessKeySecret = accessKeySecret 49 50 // URL parse 51 url := &urlMaker{} 52 53 // HTTP connect 54 conn := &Conn{config: config, url: url} 55 56 // OSS client 57 client := &Client{ 58 Config: config, 59 Conn: conn, 60 } 61 62 // Client options parse 63 for _, option := range options { 64 option(client) 65 } 66 67 err := url.InitExt(config.Endpoint, config.IsCname, config.IsUseProxy, config.IsPathStyle) 68 if err != nil { 69 return nil, err 70 } 71 72 if config.AuthVersion != AuthV1 && config.AuthVersion != AuthV2 && config.AuthVersion != AuthV4 { 73 return nil, fmt.Errorf("Init client Error, invalid Auth version: %v", config.AuthVersion) 74 } 75 76 // Create HTTP connection 77 err = conn.init(config, url, client.HTTPClient) 78 79 return client, err 80 } 81 82 // SetRegion set region for client 83 // 84 // region the region, such as cn-hangzhou 85 func (client *Client) SetRegion(region string) { 86 client.Config.Region = region 87 } 88 89 // SetCloudBoxId set CloudBoxId for client 90 // 91 // cloudBoxId the id of cloudBox 92 func (client *Client) SetCloudBoxId(cloudBoxId string) { 93 client.Config.CloudBoxId = cloudBoxId 94 } 95 96 // SetProduct set Product type for client 97 // 98 // Product product type 99 func (client *Client) SetProduct(product string) { 100 client.Config.Product = product 101 } 102 103 // Bucket gets the bucket instance. 104 // 105 // bucketName the bucket name. 106 // Bucket the bucket object, when error is nil. 107 // 108 // error it's nil if no error, otherwise it's an error object. 109 // 110 func (client Client) Bucket(bucketName string) (*Bucket, error) { 111 err := CheckBucketName(bucketName) 112 if err != nil { 113 return nil, err 114 } 115 116 return &Bucket{ 117 client, 118 bucketName, 119 }, nil 120 } 121 122 // CreateBucket creates a bucket. 123 // 124 // bucketName the bucket name, it's globably unique and immutable. The bucket name can only consist of lowercase letters, numbers and dash ('-'). 125 // It must start with lowercase letter or number and the length can only be between 3 and 255. 126 // options options for creating the bucket, with optional ACL. The ACL could be ACLPrivate, ACLPublicRead, and ACLPublicReadWrite. By default it's ACLPrivate. 127 // It could also be specified with StorageClass option, which supports StorageStandard, StorageIA(infrequent access), StorageArchive. 128 // 129 // error it's nil if no error, otherwise it's an error object. 130 // 131 func (client Client) CreateBucket(bucketName string, options ...Option) error { 132 headers := make(map[string]string) 133 handleOptions(headers, options) 134 135 buffer := new(bytes.Buffer) 136 137 var cbConfig createBucketConfiguration 138 cbConfig.StorageClass = StorageStandard 139 140 isStorageSet, valStroage, _ := IsOptionSet(options, storageClass) 141 isRedundancySet, valRedundancy, _ := IsOptionSet(options, redundancyType) 142 isObjectHashFuncSet, valHashFunc, _ := IsOptionSet(options, objectHashFunc) 143 if isStorageSet { 144 cbConfig.StorageClass = valStroage.(StorageClassType) 145 } 146 147 if isRedundancySet { 148 cbConfig.DataRedundancyType = valRedundancy.(DataRedundancyType) 149 } 150 151 if isObjectHashFuncSet { 152 cbConfig.ObjectHashFunction = valHashFunc.(ObjecthashFuncType) 153 } 154 155 bs, err := xml.Marshal(cbConfig) 156 if err != nil { 157 return err 158 } 159 buffer.Write(bs) 160 contentType := http.DetectContentType(buffer.Bytes()) 161 headers[HTTPHeaderContentType] = contentType 162 163 params := map[string]interface{}{} 164 resp, err := client.do("PUT", bucketName, params, headers, buffer, options...) 165 if err != nil { 166 return err 167 } 168 169 defer resp.Body.Close() 170 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 171 } 172 173 // create bucket xml 174 func (client Client) CreateBucketXml(bucketName string, xmlBody string, options ...Option) error { 175 buffer := new(bytes.Buffer) 176 buffer.Write([]byte(xmlBody)) 177 contentType := http.DetectContentType(buffer.Bytes()) 178 headers := map[string]string{} 179 headers[HTTPHeaderContentType] = contentType 180 181 params := map[string]interface{}{} 182 resp, err := client.do("PUT", bucketName, params, headers, buffer, options...) 183 if err != nil { 184 return err 185 } 186 187 defer resp.Body.Close() 188 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 189 } 190 191 // ListBuckets lists buckets of the current account under the given endpoint, with optional filters. 192 // 193 // options specifies the filters such as Prefix, Marker and MaxKeys. Prefix is the bucket name's prefix filter. 194 // And marker makes sure the returned buckets' name are greater than it in lexicographic order. 195 // Maxkeys limits the max keys to return, and by default it's 100 and up to 1000. 196 // For the common usage scenario, please check out list_bucket.go in the sample. 197 // ListBucketsResponse the response object if error is nil. 198 // 199 // error it's nil if no error, otherwise it's an error object. 200 // 201 func (client Client) ListBuckets(options ...Option) (ListBucketsResult, error) { 202 var out ListBucketsResult 203 204 params, err := GetRawParams(options) 205 if err != nil { 206 return out, err 207 } 208 209 resp, err := client.do("GET", "", params, nil, nil, options...) 210 if err != nil { 211 return out, err 212 } 213 defer resp.Body.Close() 214 215 err = xmlUnmarshal(resp.Body, &out) 216 return out, err 217 } 218 219 // ListCloudBoxes lists cloud boxes of the current account under the given endpoint, with optional filters. 220 // 221 // options specifies the filters such as Prefix, Marker and MaxKeys. Prefix is the bucket name's prefix filter. 222 // And marker makes sure the returned buckets' name are greater than it in lexicographic order. 223 // Maxkeys limits the max keys to return, and by default it's 100 and up to 1000. 224 // For the common usage scenario, please check out list_bucket.go in the sample. 225 // ListBucketsResponse the response object if error is nil. 226 // 227 // error it's nil if no error, otherwise it's an error object. 228 // 229 func (client Client) ListCloudBoxes(options ...Option) (ListCloudBoxResult, error) { 230 var out ListCloudBoxResult 231 232 params, err := GetRawParams(options) 233 if err != nil { 234 return out, err 235 } 236 237 params["cloudboxes"] = nil 238 239 resp, err := client.do("GET", "", params, nil, nil, options...) 240 if err != nil { 241 return out, err 242 } 243 defer resp.Body.Close() 244 245 err = xmlUnmarshal(resp.Body, &out) 246 return out, err 247 } 248 249 // IsBucketExist checks if the bucket exists 250 // 251 // bucketName the bucket name. 252 // 253 // bool true if it exists, and it's only valid when error is nil. 254 // error it's nil if no error, otherwise it's an error object. 255 // 256 func (client Client) IsBucketExist(bucketName string) (bool, error) { 257 listRes, err := client.ListBuckets(Prefix(bucketName), MaxKeys(1)) 258 if err != nil { 259 return false, err 260 } 261 262 if len(listRes.Buckets) == 1 && listRes.Buckets[0].Name == bucketName { 263 return true, nil 264 } 265 return false, nil 266 } 267 268 // DeleteBucket deletes the bucket. Only empty bucket can be deleted (no object and parts). 269 // 270 // bucketName the bucket name. 271 // 272 // error it's nil if no error, otherwise it's an error object. 273 // 274 func (client Client) DeleteBucket(bucketName string, options ...Option) error { 275 params := map[string]interface{}{} 276 resp, err := client.do("DELETE", bucketName, params, nil, nil, options...) 277 if err != nil { 278 return err 279 } 280 281 defer resp.Body.Close() 282 return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent}) 283 } 284 285 // GetBucketLocation gets the bucket location. 286 // 287 // Checks out the following link for more information : 288 // https://www.alibabacloud.com/help/en/object-storage-service/latest/getbucketlocation 289 // 290 // bucketName the bucket name 291 // 292 // string bucket's datacenter location 293 // error it's nil if no error, otherwise it's an error object. 294 // 295 func (client Client) GetBucketLocation(bucketName string, options ...Option) (string, error) { 296 params := map[string]interface{}{} 297 params["location"] = nil 298 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 299 if err != nil { 300 return "", err 301 } 302 defer resp.Body.Close() 303 304 var LocationConstraint string 305 err = xmlUnmarshal(resp.Body, &LocationConstraint) 306 return LocationConstraint, err 307 } 308 309 // SetBucketACL sets bucket's ACL. 310 // 311 // bucketName the bucket name 312 // bucketAcl the bucket ACL: ACLPrivate, ACLPublicRead and ACLPublicReadWrite. 313 // 314 // error it's nil if no error, otherwise it's an error object. 315 // 316 func (client Client) SetBucketACL(bucketName string, bucketACL ACLType, options ...Option) error { 317 headers := map[string]string{HTTPHeaderOssACL: string(bucketACL)} 318 params := map[string]interface{}{} 319 params["acl"] = nil 320 resp, err := client.do("PUT", bucketName, params, headers, nil, options...) 321 if err != nil { 322 return err 323 } 324 defer resp.Body.Close() 325 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 326 } 327 328 // GetBucketACL gets the bucket ACL. 329 // 330 // bucketName the bucket name. 331 // 332 // GetBucketAclResponse the result object, and it's only valid when error is nil. 333 // error it's nil if no error, otherwise it's an error object. 334 // 335 func (client Client) GetBucketACL(bucketName string, options ...Option) (GetBucketACLResult, error) { 336 var out GetBucketACLResult 337 params := map[string]interface{}{} 338 params["acl"] = nil 339 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 340 if err != nil { 341 return out, err 342 } 343 defer resp.Body.Close() 344 345 err = xmlUnmarshal(resp.Body, &out) 346 return out, err 347 } 348 349 // SetBucketLifecycle sets the bucket's lifecycle. 350 // 351 // For more information, checks out following link: 352 // https://www.alibabacloud.com/help/en/object-storage-service/latest/putbucketlifecycle 353 // 354 // bucketName the bucket name. 355 // rules the lifecycle rules. There're two kind of rules: absolute time expiration and relative time expiration in days and day/month/year respectively. 356 // Check out sample/bucket_lifecycle.go for more details. 357 // 358 // error it's nil if no error, otherwise it's an error object. 359 // 360 func (client Client) SetBucketLifecycle(bucketName string, rules []LifecycleRule, options ...Option) error { 361 err := verifyLifecycleRules(rules) 362 if err != nil { 363 return err 364 } 365 lifecycleCfg := LifecycleConfiguration{Rules: rules} 366 bs, err := xml.Marshal(lifecycleCfg) 367 if err != nil { 368 return err 369 } 370 buffer := new(bytes.Buffer) 371 buffer.Write(bs) 372 373 contentType := http.DetectContentType(buffer.Bytes()) 374 headers := map[string]string{} 375 headers[HTTPHeaderContentType] = contentType 376 377 params := map[string]interface{}{} 378 params["lifecycle"] = nil 379 resp, err := client.do("PUT", bucketName, params, headers, buffer, options...) 380 if err != nil { 381 return err 382 } 383 defer resp.Body.Close() 384 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 385 } 386 387 // SetBucketLifecycleXml sets the bucket's lifecycle rule from xml config 388 func (client Client) SetBucketLifecycleXml(bucketName string, xmlBody string, options ...Option) error { 389 buffer := new(bytes.Buffer) 390 buffer.Write([]byte(xmlBody)) 391 392 contentType := http.DetectContentType(buffer.Bytes()) 393 headers := map[string]string{} 394 headers[HTTPHeaderContentType] = contentType 395 396 params := map[string]interface{}{} 397 params["lifecycle"] = nil 398 resp, err := client.do("PUT", bucketName, params, headers, buffer, options...) 399 if err != nil { 400 return err 401 } 402 defer resp.Body.Close() 403 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 404 } 405 406 // DeleteBucketLifecycle deletes the bucket's lifecycle. 407 // 408 // 409 // bucketName the bucket name. 410 // 411 // error it's nil if no error, otherwise it's an error object. 412 // 413 func (client Client) DeleteBucketLifecycle(bucketName string, options ...Option) error { 414 params := map[string]interface{}{} 415 params["lifecycle"] = nil 416 resp, err := client.do("DELETE", bucketName, params, nil, nil, options...) 417 if err != nil { 418 return err 419 } 420 defer resp.Body.Close() 421 return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent}) 422 } 423 424 // GetBucketLifecycle gets the bucket's lifecycle settings. 425 // 426 // bucketName the bucket name. 427 // 428 // GetBucketLifecycleResponse the result object upon successful request. It's only valid when error is nil. 429 // error it's nil if no error, otherwise it's an error object. 430 // 431 func (client Client) GetBucketLifecycle(bucketName string, options ...Option) (GetBucketLifecycleResult, error) { 432 var out GetBucketLifecycleResult 433 params := map[string]interface{}{} 434 params["lifecycle"] = nil 435 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 436 if err != nil { 437 return out, err 438 } 439 defer resp.Body.Close() 440 441 err = xmlUnmarshal(resp.Body, &out) 442 443 // NonVersionTransition is not suggested to use 444 // to keep compatible 445 for k, rule := range out.Rules { 446 if len(rule.NonVersionTransitions) > 0 { 447 out.Rules[k].NonVersionTransition = &(out.Rules[k].NonVersionTransitions[0]) 448 } 449 } 450 return out, err 451 } 452 453 func (client Client) GetBucketLifecycleXml(bucketName string, options ...Option) (string, error) { 454 params := map[string]interface{}{} 455 params["lifecycle"] = nil 456 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 457 if err != nil { 458 return "", err 459 } 460 defer resp.Body.Close() 461 462 body, err := ioutil.ReadAll(resp.Body) 463 out := string(body) 464 return out, err 465 } 466 467 // SetBucketReferer sets the bucket's referer whitelist and the flag if allowing empty referrer. 468 // 469 // To avoid stealing link on OSS data, OSS supports the HTTP referrer header. A whitelist referrer could be set either by API or web console, as well as 470 // the allowing empty referrer flag. Note that this applies to requests from web browser only. 471 // For example, for a bucket os-example and its referrer http://www.aliyun.com, all requests from this URL could access the bucket. 472 // For more information, please check out this link : 473 // https://www.alibabacloud.com/help/en/object-storage-service/latest/putbucketreferer 474 // 475 // bucketName the bucket name. 476 // referrers the referrer white list. A bucket could have a referrer list and each referrer supports one '*' and multiple '?' as wildcards. 477 // The sample could be found in sample/bucket_referer.go 478 // allowEmptyReferer the flag of allowing empty referrer. By default it's true. 479 // 480 // error it's nil if no error, otherwise it's an error object. 481 // 482 func (client Client) SetBucketReferer(bucketName string, referrers []string, allowEmptyReferer bool, options ...Option) error { 483 rxml := RefererXML{} 484 rxml.AllowEmptyReferer = allowEmptyReferer 485 if referrers == nil { 486 rxml.RefererList = append(rxml.RefererList, "") 487 } else { 488 for _, referrer := range referrers { 489 rxml.RefererList = append(rxml.RefererList, referrer) 490 } 491 } 492 493 bs, err := xml.Marshal(rxml) 494 if err != nil { 495 return err 496 } 497 498 return client.PutBucketRefererXml(bucketName, string(bs), options...) 499 } 500 501 // SetBucketRefererV2 gets the bucket's referer white list. 502 // 503 // setBucketReferer SetBucketReferer bucket referer config in struct format. 504 // 505 // GetBucketRefererResponse the result object upon successful request. It's only valid when error is nil. 506 // error it's nil if no error, otherwise it's an error object. 507 // 508 func (client Client) SetBucketRefererV2(bucketName string, setBucketReferer RefererXML, options ...Option) error { 509 bs, err := xml.Marshal(setBucketReferer) 510 if err != nil { 511 return err 512 } 513 return client.PutBucketRefererXml(bucketName, string(bs), options...) 514 } 515 516 // PutBucketRefererXml set bucket's style 517 // bucketName the bucket name. 518 // xmlData the style in xml format 519 // error it's nil if no error, otherwise it's an error object. 520 func (client Client) PutBucketRefererXml(bucketName, xmlData string, options ...Option) error { 521 buffer := new(bytes.Buffer) 522 buffer.Write([]byte(xmlData)) 523 contentType := http.DetectContentType(buffer.Bytes()) 524 headers := map[string]string{} 525 headers[HTTPHeaderContentType] = contentType 526 527 params := map[string]interface{}{} 528 params["referer"] = nil 529 resp, err := client.do("PUT", bucketName, params, headers, buffer, options...) 530 if err != nil { 531 return err 532 } 533 defer resp.Body.Close() 534 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 535 } 536 537 // GetBucketReferer gets the bucket's referrer white list. 538 // bucketName the bucket name. 539 // GetBucketRefererResult the result object upon successful request. It's only valid when error is nil. 540 // error it's nil if no error, otherwise it's an error object. 541 func (client Client) GetBucketReferer(bucketName string, options ...Option) (GetBucketRefererResult, error) { 542 var out GetBucketRefererResult 543 body, err := client.GetBucketRefererXml(bucketName, options...) 544 if err != nil { 545 return out, err 546 } 547 err = xmlUnmarshal(strings.NewReader(body), &out) 548 return out, err 549 } 550 551 // GetBucketRefererXml gets the bucket's referrer white list. 552 // bucketName the bucket name. 553 // GetBucketRefererResponse the bucket referer config result in xml format. 554 // error it's nil if no error, otherwise it's an error object. 555 func (client Client) GetBucketRefererXml(bucketName string, options ...Option) (string, error) { 556 params := map[string]interface{}{} 557 params["referer"] = nil 558 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 559 if err != nil { 560 return "", err 561 } 562 defer resp.Body.Close() 563 body, err := ioutil.ReadAll(resp.Body) 564 return string(body), err 565 } 566 567 // SetBucketLogging sets the bucket logging settings. 568 // 569 // OSS could automatically store the access log. Only the bucket owner could enable the logging. 570 // Once enabled, OSS would save all the access log into hourly log files in a specified bucket. 571 // For more information, please check out https://www.alibabacloud.com/help/en/object-storage-service/latest/putbucketlogging 572 // 573 // bucketName bucket name to enable the log. 574 // targetBucket the target bucket name to store the log files. 575 // targetPrefix the log files' prefix. 576 // 577 // error it's nil if no error, otherwise it's an error object. 578 // 579 func (client Client) SetBucketLogging(bucketName, targetBucket, targetPrefix string, 580 isEnable bool, options ...Option) error { 581 var err error 582 var bs []byte 583 if isEnable { 584 lxml := LoggingXML{} 585 lxml.LoggingEnabled.TargetBucket = targetBucket 586 lxml.LoggingEnabled.TargetPrefix = targetPrefix 587 bs, err = xml.Marshal(lxml) 588 } else { 589 lxml := loggingXMLEmpty{} 590 bs, err = xml.Marshal(lxml) 591 } 592 593 if err != nil { 594 return err 595 } 596 597 buffer := new(bytes.Buffer) 598 buffer.Write(bs) 599 600 contentType := http.DetectContentType(buffer.Bytes()) 601 headers := map[string]string{} 602 headers[HTTPHeaderContentType] = contentType 603 604 params := map[string]interface{}{} 605 params["logging"] = nil 606 resp, err := client.do("PUT", bucketName, params, headers, buffer, options...) 607 if err != nil { 608 return err 609 } 610 defer resp.Body.Close() 611 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 612 } 613 614 // DeleteBucketLogging deletes the logging configuration to disable the logging on the bucket. 615 // 616 // bucketName the bucket name to disable the logging. 617 // 618 // error it's nil if no error, otherwise it's an error object. 619 // 620 func (client Client) DeleteBucketLogging(bucketName string, options ...Option) error { 621 params := map[string]interface{}{} 622 params["logging"] = nil 623 resp, err := client.do("DELETE", bucketName, params, nil, nil, options...) 624 if err != nil { 625 return err 626 } 627 defer resp.Body.Close() 628 return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent}) 629 } 630 631 // GetBucketLogging gets the bucket's logging settings 632 // 633 // bucketName the bucket name 634 // GetBucketLoggingResponse the result object upon successful request. It's only valid when error is nil. 635 // 636 // error it's nil if no error, otherwise it's an error object. 637 // 638 func (client Client) GetBucketLogging(bucketName string, options ...Option) (GetBucketLoggingResult, error) { 639 var out GetBucketLoggingResult 640 params := map[string]interface{}{} 641 params["logging"] = nil 642 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 643 if err != nil { 644 return out, err 645 } 646 defer resp.Body.Close() 647 648 err = xmlUnmarshal(resp.Body, &out) 649 return out, err 650 } 651 652 // SetBucketWebsite sets the bucket's static website's index and error page. 653 // 654 // OSS supports static web site hosting for the bucket data. When the bucket is enabled with that, you can access the file in the bucket like the way to access a static website. 655 // For more information, please check out: https://www.alibabacloud.com/help/en/object-storage-service/latest/putbucketwebsite 656 // 657 // bucketName the bucket name to enable static web site. 658 // indexDocument index page. 659 // errorDocument error page. 660 // 661 // error it's nil if no error, otherwise it's an error object. 662 // 663 func (client Client) SetBucketWebsite(bucketName, indexDocument, errorDocument string, options ...Option) error { 664 wxml := WebsiteXML{} 665 wxml.IndexDocument.Suffix = indexDocument 666 wxml.ErrorDocument.Key = errorDocument 667 668 bs, err := xml.Marshal(wxml) 669 if err != nil { 670 return err 671 } 672 buffer := new(bytes.Buffer) 673 buffer.Write(bs) 674 675 contentType := http.DetectContentType(buffer.Bytes()) 676 headers := make(map[string]string) 677 headers[HTTPHeaderContentType] = contentType 678 679 params := map[string]interface{}{} 680 params["website"] = nil 681 resp, err := client.do("PUT", bucketName, params, headers, buffer, options...) 682 if err != nil { 683 return err 684 } 685 defer resp.Body.Close() 686 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 687 } 688 689 // SetBucketWebsiteDetail sets the bucket's static website's detail 690 // 691 // OSS supports static web site hosting for the bucket data. When the bucket is enabled with that, you can access the file in the bucket like the way to access a static website. 692 // For more information, please check out: https://www.alibabacloud.com/help/en/object-storage-service/latest/putbucketwebsite 693 // 694 // bucketName the bucket name to enable static web site. 695 // 696 // wxml the website's detail 697 // 698 // error it's nil if no error, otherwise it's an error object. 699 // 700 func (client Client) SetBucketWebsiteDetail(bucketName string, wxml WebsiteXML, options ...Option) error { 701 bs, err := xml.Marshal(wxml) 702 if err != nil { 703 return err 704 } 705 buffer := new(bytes.Buffer) 706 buffer.Write(bs) 707 708 contentType := http.DetectContentType(buffer.Bytes()) 709 headers := make(map[string]string) 710 headers[HTTPHeaderContentType] = contentType 711 712 params := map[string]interface{}{} 713 params["website"] = nil 714 resp, err := client.do("PUT", bucketName, params, headers, buffer, options...) 715 if err != nil { 716 return err 717 } 718 defer resp.Body.Close() 719 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 720 } 721 722 // SetBucketWebsiteXml sets the bucket's static website's rule 723 // 724 // OSS supports static web site hosting for the bucket data. When the bucket is enabled with that, you can access the file in the bucket like the way to access a static website. 725 // For more information, please check out: https://www.alibabacloud.com/help/en/object-storage-service/latest/putbucketwebsite 726 // 727 // bucketName the bucket name to enable static web site. 728 // 729 // wxml the website's detail 730 // 731 // error it's nil if no error, otherwise it's an error object. 732 // 733 func (client Client) SetBucketWebsiteXml(bucketName string, webXml string, options ...Option) error { 734 buffer := new(bytes.Buffer) 735 buffer.Write([]byte(webXml)) 736 737 contentType := http.DetectContentType(buffer.Bytes()) 738 headers := make(map[string]string) 739 headers[HTTPHeaderContentType] = contentType 740 741 params := map[string]interface{}{} 742 params["website"] = nil 743 resp, err := client.do("PUT", bucketName, params, headers, buffer, options...) 744 if err != nil { 745 return err 746 } 747 defer resp.Body.Close() 748 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 749 } 750 751 // DeleteBucketWebsite deletes the bucket's static web site settings. 752 // 753 // bucketName the bucket name. 754 // 755 // error it's nil if no error, otherwise it's an error object. 756 // 757 func (client Client) DeleteBucketWebsite(bucketName string, options ...Option) error { 758 params := map[string]interface{}{} 759 params["website"] = nil 760 resp, err := client.do("DELETE", bucketName, params, nil, nil, options...) 761 if err != nil { 762 return err 763 } 764 defer resp.Body.Close() 765 return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent}) 766 } 767 768 // OpenMetaQuery Enables the metadata management feature for a bucket. 769 // 770 // bucketName the bucket name. 771 // 772 // error it's nil if no error, otherwise it's an error object. 773 // 774 func (client Client) OpenMetaQuery(bucketName string, options ...Option) error { 775 params := map[string]interface{}{} 776 params["metaQuery"] = nil 777 params["comp"] = "add" 778 resp, err := client.do("POST", bucketName, params, nil, nil, options...) 779 if err != nil { 780 return err 781 } 782 defer resp.Body.Close() 783 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 784 } 785 786 // GetMetaQueryStatus Queries the information about the metadata index library of a bucket. 787 // 788 // bucketName the bucket name 789 // 790 // GetMetaQueryStatusResult the result object upon successful request. It's only valid when error is nil. 791 // error it's nil if no error, otherwise it's an error object. 792 // 793 func (client Client) GetMetaQueryStatus(bucketName string, options ...Option) (GetMetaQueryStatusResult, error) { 794 var out GetMetaQueryStatusResult 795 params := map[string]interface{}{} 796 params["metaQuery"] = nil 797 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 798 if err != nil { 799 return out, err 800 } 801 defer resp.Body.Close() 802 err = xmlUnmarshal(resp.Body, &out) 803 return out, err 804 } 805 806 // DoMetaQuery Queries the objects that meet specified conditions and lists the information about objects based on specified fields and sorting methods. 807 // 808 // bucketName the bucket name 809 // 810 // metaQuery the option of query 811 // 812 // DoMetaQueryResult the result object upon successful request. It's only valid when error is nil. 813 // error it's nil if no error, otherwise it's an error object. 814 // 815 func (client Client) DoMetaQuery(bucketName string, metaQuery MetaQuery, options ...Option) (DoMetaQueryResult, error) { 816 var out DoMetaQueryResult 817 bs, err := xml.Marshal(metaQuery) 818 if err != nil { 819 return out, err 820 } 821 out, err = client.DoMetaQueryXml(bucketName, string(bs), options...) 822 return out, err 823 } 824 825 // DoMetaQueryXml Queries the objects that meet specified conditions and lists the information about objects based on specified fields and sorting methods. 826 // 827 // bucketName the bucket name 828 // 829 // metaQuery the option of query 830 // 831 // DoMetaQueryResult the result object upon successful request. It's only valid when error is nil. 832 // error it's nil if no error, otherwise it's an error object. 833 // 834 func (client Client) DoMetaQueryXml(bucketName string, metaQueryXml string, options ...Option) (DoMetaQueryResult, error) { 835 var out DoMetaQueryResult 836 buffer := new(bytes.Buffer) 837 buffer.Write([]byte(metaQueryXml)) 838 contentType := http.DetectContentType(buffer.Bytes()) 839 headers := map[string]string{} 840 headers[HTTPHeaderContentType] = contentType 841 842 params := map[string]interface{}{} 843 params["metaQuery"] = nil 844 params["comp"] = "query" 845 resp, err := client.do("POST", bucketName, params, headers, buffer, options...) 846 if err != nil { 847 return out, err 848 } 849 defer resp.Body.Close() 850 err = xmlUnmarshal(resp.Body, &out) 851 return out, err 852 } 853 854 // CloseMetaQuery Disables the metadata management feature for a bucket. 855 // 856 // bucketName the bucket name. 857 // 858 // error it's nil if no error, otherwise it's an error object. 859 // 860 func (client Client) CloseMetaQuery(bucketName string, options ...Option) error { 861 params := map[string]interface{}{} 862 params["metaQuery"] = nil 863 params["comp"] = "delete" 864 resp, err := client.do("POST", bucketName, params, nil, nil, options...) 865 if err != nil { 866 return err 867 } 868 defer resp.Body.Close() 869 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 870 } 871 872 // GetBucketWebsite gets the bucket's default page (index page) and the error page. 873 // 874 // bucketName the bucket name 875 // 876 // GetBucketWebsiteResponse the result object upon successful request. It's only valid when error is nil. 877 // error it's nil if no error, otherwise it's an error object. 878 // 879 func (client Client) GetBucketWebsite(bucketName string, options ...Option) (GetBucketWebsiteResult, error) { 880 var out GetBucketWebsiteResult 881 params := map[string]interface{}{} 882 params["website"] = nil 883 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 884 if err != nil { 885 return out, err 886 } 887 defer resp.Body.Close() 888 889 err = xmlUnmarshal(resp.Body, &out) 890 return out, err 891 } 892 893 // GetBucketWebsiteXml gets the bucket's website config xml config. 894 // 895 // bucketName the bucket name 896 // 897 // string the bucket's xml config, It's only valid when error is nil. 898 // error it's nil if no error, otherwise it's an error object. 899 // 900 func (client Client) GetBucketWebsiteXml(bucketName string, options ...Option) (string, error) { 901 params := map[string]interface{}{} 902 params["website"] = nil 903 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 904 if err != nil { 905 return "", err 906 } 907 defer resp.Body.Close() 908 909 body, err := ioutil.ReadAll(resp.Body) 910 911 out := string(body) 912 return out, err 913 } 914 915 // SetBucketCORS sets the bucket's CORS rules 916 // 917 // For more information, please check out https://help.aliyun.com/document_detail/oss/user_guide/security_management/cors.html 918 // 919 // bucketName the bucket name 920 // corsRules the CORS rules to set. The related sample code is in sample/bucket_cors.go. 921 // 922 // error it's nil if no error, otherwise it's an error object. 923 // 924 func (client Client) SetBucketCORS(bucketName string, corsRules []CORSRule, options ...Option) error { 925 corsxml := CORSXML{} 926 for _, v := range corsRules { 927 cr := CORSRule{} 928 cr.AllowedMethod = v.AllowedMethod 929 cr.AllowedOrigin = v.AllowedOrigin 930 cr.AllowedHeader = v.AllowedHeader 931 cr.ExposeHeader = v.ExposeHeader 932 cr.MaxAgeSeconds = v.MaxAgeSeconds 933 corsxml.CORSRules = append(corsxml.CORSRules, cr) 934 } 935 936 bs, err := xml.Marshal(corsxml) 937 if err != nil { 938 return err 939 } 940 buffer := new(bytes.Buffer) 941 buffer.Write(bs) 942 943 contentType := http.DetectContentType(buffer.Bytes()) 944 headers := map[string]string{} 945 headers[HTTPHeaderContentType] = contentType 946 947 params := map[string]interface{}{} 948 params["cors"] = nil 949 resp, err := client.do("PUT", bucketName, params, headers, buffer, options...) 950 if err != nil { 951 return err 952 } 953 defer resp.Body.Close() 954 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 955 } 956 957 // SetBucketCORSV2 sets the bucket's CORS rules 958 // 959 // bucketName the bucket name 960 // putBucketCORS the CORS rules to set. 961 // 962 // error it's nil if no error, otherwise it's an error object. 963 // 964 func (client Client) SetBucketCORSV2(bucketName string, putBucketCORS PutBucketCORS, options ...Option) error { 965 bs, err := xml.Marshal(putBucketCORS) 966 if err != nil { 967 return err 968 } 969 err = client.SetBucketCORSXml(bucketName, string(bs), options...) 970 return err 971 } 972 973 func (client Client) SetBucketCORSXml(bucketName string, xmlBody string, options ...Option) error { 974 buffer := new(bytes.Buffer) 975 buffer.Write([]byte(xmlBody)) 976 contentType := http.DetectContentType(buffer.Bytes()) 977 headers := map[string]string{} 978 headers[HTTPHeaderContentType] = contentType 979 980 params := map[string]interface{}{} 981 params["cors"] = nil 982 resp, err := client.do("PUT", bucketName, params, headers, buffer, options...) 983 if err != nil { 984 return err 985 } 986 defer resp.Body.Close() 987 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 988 } 989 990 // DeleteBucketCORS deletes the bucket's static website settings. 991 // 992 // bucketName the bucket name. 993 // 994 // error it's nil if no error, otherwise it's an error object. 995 // 996 func (client Client) DeleteBucketCORS(bucketName string, options ...Option) error { 997 params := map[string]interface{}{} 998 params["cors"] = nil 999 resp, err := client.do("DELETE", bucketName, params, nil, nil, options...) 1000 if err != nil { 1001 return err 1002 } 1003 defer resp.Body.Close() 1004 return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent}) 1005 } 1006 1007 // GetBucketCORS gets the bucket's CORS settings. 1008 // 1009 // bucketName the bucket name. 1010 // GetBucketCORSResult the result object upon successful request. It's only valid when error is nil. 1011 // 1012 // error it's nil if no error, otherwise it's an error object. 1013 // 1014 func (client Client) GetBucketCORS(bucketName string, options ...Option) (GetBucketCORSResult, error) { 1015 var out GetBucketCORSResult 1016 params := map[string]interface{}{} 1017 params["cors"] = nil 1018 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 1019 if err != nil { 1020 return out, err 1021 } 1022 defer resp.Body.Close() 1023 1024 err = xmlUnmarshal(resp.Body, &out) 1025 return out, err 1026 } 1027 1028 func (client Client) GetBucketCORSXml(bucketName string, options ...Option) (string, error) { 1029 params := map[string]interface{}{} 1030 params["cors"] = nil 1031 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 1032 if err != nil { 1033 return "", err 1034 } 1035 defer resp.Body.Close() 1036 1037 body, err := ioutil.ReadAll(resp.Body) 1038 out := string(body) 1039 return out, err 1040 } 1041 1042 // GetBucketInfo gets the bucket information. 1043 // 1044 // bucketName the bucket name. 1045 // GetBucketInfoResult the result object upon successful request. It's only valid when error is nil. 1046 // 1047 // error it's nil if no error, otherwise it's an error object. 1048 // 1049 func (client Client) GetBucketInfo(bucketName string, options ...Option) (GetBucketInfoResult, error) { 1050 var out GetBucketInfoResult 1051 params := map[string]interface{}{} 1052 params["bucketInfo"] = nil 1053 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 1054 if err != nil { 1055 return out, err 1056 } 1057 defer resp.Body.Close() 1058 1059 err = xmlUnmarshal(resp.Body, &out) 1060 1061 // convert None to "" 1062 if err == nil { 1063 if out.BucketInfo.SseRule.KMSMasterKeyID == "None" { 1064 out.BucketInfo.SseRule.KMSMasterKeyID = "" 1065 } 1066 1067 if out.BucketInfo.SseRule.SSEAlgorithm == "None" { 1068 out.BucketInfo.SseRule.SSEAlgorithm = "" 1069 } 1070 1071 if out.BucketInfo.SseRule.KMSDataEncryption == "None" { 1072 out.BucketInfo.SseRule.KMSDataEncryption = "" 1073 } 1074 } 1075 return out, err 1076 } 1077 1078 // SetBucketVersioning set bucket versioning:Enabled、Suspended 1079 // bucketName the bucket name. 1080 // error it's nil if no error, otherwise it's an error object. 1081 func (client Client) SetBucketVersioning(bucketName string, versioningConfig VersioningConfig, options ...Option) error { 1082 var err error 1083 var bs []byte 1084 bs, err = xml.Marshal(versioningConfig) 1085 1086 if err != nil { 1087 return err 1088 } 1089 1090 buffer := new(bytes.Buffer) 1091 buffer.Write(bs) 1092 1093 contentType := http.DetectContentType(buffer.Bytes()) 1094 headers := map[string]string{} 1095 headers[HTTPHeaderContentType] = contentType 1096 1097 params := map[string]interface{}{} 1098 params["versioning"] = nil 1099 resp, err := client.do("PUT", bucketName, params, headers, buffer, options...) 1100 1101 if err != nil { 1102 return err 1103 } 1104 defer resp.Body.Close() 1105 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 1106 } 1107 1108 // GetBucketVersioning get bucket versioning status:Enabled、Suspended 1109 // bucketName the bucket name. 1110 // error it's nil if no error, otherwise it's an error object. 1111 func (client Client) GetBucketVersioning(bucketName string, options ...Option) (GetBucketVersioningResult, error) { 1112 var out GetBucketVersioningResult 1113 params := map[string]interface{}{} 1114 params["versioning"] = nil 1115 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 1116 1117 if err != nil { 1118 return out, err 1119 } 1120 defer resp.Body.Close() 1121 1122 err = xmlUnmarshal(resp.Body, &out) 1123 return out, err 1124 } 1125 1126 // SetBucketEncryption set bucket encryption config 1127 // bucketName the bucket name. 1128 // error it's nil if no error, otherwise it's an error object. 1129 func (client Client) SetBucketEncryption(bucketName string, encryptionRule ServerEncryptionRule, options ...Option) error { 1130 var err error 1131 var bs []byte 1132 bs, err = xml.Marshal(encryptionRule) 1133 1134 if err != nil { 1135 return err 1136 } 1137 1138 buffer := new(bytes.Buffer) 1139 buffer.Write(bs) 1140 1141 contentType := http.DetectContentType(buffer.Bytes()) 1142 headers := map[string]string{} 1143 headers[HTTPHeaderContentType] = contentType 1144 1145 params := map[string]interface{}{} 1146 params["encryption"] = nil 1147 resp, err := client.do("PUT", bucketName, params, headers, buffer, options...) 1148 1149 if err != nil { 1150 return err 1151 } 1152 defer resp.Body.Close() 1153 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 1154 } 1155 1156 // GetBucketEncryption get bucket encryption 1157 // bucketName the bucket name. 1158 // error it's nil if no error, otherwise it's an error object. 1159 func (client Client) GetBucketEncryption(bucketName string, options ...Option) (GetBucketEncryptionResult, error) { 1160 var out GetBucketEncryptionResult 1161 params := map[string]interface{}{} 1162 params["encryption"] = nil 1163 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 1164 1165 if err != nil { 1166 return out, err 1167 } 1168 defer resp.Body.Close() 1169 1170 err = xmlUnmarshal(resp.Body, &out) 1171 return out, err 1172 } 1173 1174 // DeleteBucketEncryption delete bucket encryption config 1175 // bucketName the bucket name. 1176 // error it's nil if no error, otherwise it's an error bucket 1177 func (client Client) DeleteBucketEncryption(bucketName string, options ...Option) error { 1178 params := map[string]interface{}{} 1179 params["encryption"] = nil 1180 resp, err := client.do("DELETE", bucketName, params, nil, nil, options...) 1181 1182 if err != nil { 1183 return err 1184 } 1185 defer resp.Body.Close() 1186 return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent}) 1187 } 1188 1189 // 1190 // SetBucketTagging add tagging to bucket 1191 // bucketName name of bucket 1192 // tagging tagging to be added 1193 // error nil if success, otherwise error 1194 func (client Client) SetBucketTagging(bucketName string, tagging Tagging, options ...Option) error { 1195 var err error 1196 var bs []byte 1197 bs, err = xml.Marshal(tagging) 1198 1199 if err != nil { 1200 return err 1201 } 1202 1203 buffer := new(bytes.Buffer) 1204 buffer.Write(bs) 1205 1206 contentType := http.DetectContentType(buffer.Bytes()) 1207 headers := map[string]string{} 1208 headers[HTTPHeaderContentType] = contentType 1209 1210 params := map[string]interface{}{} 1211 params["tagging"] = nil 1212 resp, err := client.do("PUT", bucketName, params, headers, buffer, options...) 1213 if err != nil { 1214 return err 1215 } 1216 defer resp.Body.Close() 1217 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 1218 } 1219 1220 // GetBucketTagging get tagging of the bucket 1221 // bucketName name of bucket 1222 // error nil if success, otherwise error 1223 func (client Client) GetBucketTagging(bucketName string, options ...Option) (GetBucketTaggingResult, error) { 1224 var out GetBucketTaggingResult 1225 params := map[string]interface{}{} 1226 params["tagging"] = nil 1227 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 1228 if err != nil { 1229 return out, err 1230 } 1231 defer resp.Body.Close() 1232 1233 err = xmlUnmarshal(resp.Body, &out) 1234 return out, err 1235 } 1236 1237 // 1238 // DeleteBucketTagging delete bucket tagging 1239 // bucketName name of bucket 1240 // error nil if success, otherwise error 1241 // 1242 func (client Client) DeleteBucketTagging(bucketName string, options ...Option) error { 1243 key, _ := FindOption(options, "tagging", nil) 1244 params := map[string]interface{}{} 1245 if key == nil { 1246 params["tagging"] = nil 1247 } else { 1248 params["tagging"] = key.(string) 1249 } 1250 1251 resp, err := client.do("DELETE", bucketName, params, nil, nil, options...) 1252 if err != nil { 1253 return err 1254 } 1255 defer resp.Body.Close() 1256 return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent}) 1257 } 1258 1259 // GetBucketStat get bucket stat 1260 // bucketName the bucket name. 1261 // error it's nil if no error, otherwise it's an error object. 1262 func (client Client) GetBucketStat(bucketName string, options ...Option) (GetBucketStatResult, error) { 1263 var out GetBucketStatResult 1264 params := map[string]interface{}{} 1265 params["stat"] = nil 1266 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 1267 if err != nil { 1268 return out, err 1269 } 1270 defer resp.Body.Close() 1271 1272 err = xmlUnmarshal(resp.Body, &out) 1273 return out, err 1274 } 1275 1276 // GetBucketPolicy API operation for Object Storage Service. 1277 // 1278 // Get the policy from the bucket. 1279 // 1280 // bucketName the bucket name. 1281 // 1282 // string return the bucket's policy, and it's only valid when error is nil. 1283 // 1284 // error it's nil if no error, otherwise it's an error object. 1285 // 1286 func (client Client) GetBucketPolicy(bucketName string, options ...Option) (string, error) { 1287 params := map[string]interface{}{} 1288 params["policy"] = nil 1289 1290 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 1291 if err != nil { 1292 return "", err 1293 } 1294 defer resp.Body.Close() 1295 1296 body, err := ioutil.ReadAll(resp.Body) 1297 1298 out := string(body) 1299 return out, err 1300 } 1301 1302 // SetBucketPolicy API operation for Object Storage Service. 1303 // 1304 // Set the policy from the bucket. 1305 // 1306 // bucketName the bucket name. 1307 // 1308 // policy the bucket policy. 1309 // 1310 // error it's nil if no error, otherwise it's an error object. 1311 // 1312 func (client Client) SetBucketPolicy(bucketName string, policy string, options ...Option) error { 1313 params := map[string]interface{}{} 1314 params["policy"] = nil 1315 1316 buffer := strings.NewReader(policy) 1317 1318 resp, err := client.do("PUT", bucketName, params, nil, buffer, options...) 1319 if err != nil { 1320 return err 1321 } 1322 defer resp.Body.Close() 1323 1324 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 1325 } 1326 1327 // DeleteBucketPolicy API operation for Object Storage Service. 1328 // 1329 // Deletes the policy from the bucket. 1330 // 1331 // bucketName the bucket name. 1332 // 1333 // error it's nil if no error, otherwise it's an error object. 1334 // 1335 func (client Client) DeleteBucketPolicy(bucketName string, options ...Option) error { 1336 params := map[string]interface{}{} 1337 params["policy"] = nil 1338 resp, err := client.do("DELETE", bucketName, params, nil, nil, options...) 1339 if err != nil { 1340 return err 1341 } 1342 1343 defer resp.Body.Close() 1344 return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent}) 1345 } 1346 1347 // SetBucketRequestPayment API operation for Object Storage Service. 1348 // 1349 // Set the requestPayment of bucket 1350 // 1351 // bucketName the bucket name. 1352 // 1353 // paymentConfig the payment configuration 1354 // 1355 // error it's nil if no error, otherwise it's an error object. 1356 // 1357 func (client Client) SetBucketRequestPayment(bucketName string, paymentConfig RequestPaymentConfiguration, options ...Option) error { 1358 params := map[string]interface{}{} 1359 params["requestPayment"] = nil 1360 1361 var bs []byte 1362 bs, err := xml.Marshal(paymentConfig) 1363 1364 if err != nil { 1365 return err 1366 } 1367 1368 buffer := new(bytes.Buffer) 1369 buffer.Write(bs) 1370 1371 contentType := http.DetectContentType(buffer.Bytes()) 1372 headers := map[string]string{} 1373 headers[HTTPHeaderContentType] = contentType 1374 1375 resp, err := client.do("PUT", bucketName, params, headers, buffer, options...) 1376 if err != nil { 1377 return err 1378 } 1379 defer resp.Body.Close() 1380 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 1381 } 1382 1383 // GetBucketRequestPayment API operation for Object Storage Service. 1384 // 1385 // Get bucket requestPayment 1386 // 1387 // bucketName the bucket name. 1388 // 1389 // RequestPaymentConfiguration the payment configuration 1390 // 1391 // error it's nil if no error, otherwise it's an error object. 1392 // 1393 func (client Client) GetBucketRequestPayment(bucketName string, options ...Option) (RequestPaymentConfiguration, error) { 1394 var out RequestPaymentConfiguration 1395 params := map[string]interface{}{} 1396 params["requestPayment"] = nil 1397 1398 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 1399 if err != nil { 1400 return out, err 1401 } 1402 defer resp.Body.Close() 1403 1404 err = xmlUnmarshal(resp.Body, &out) 1405 return out, err 1406 } 1407 1408 // GetUserQoSInfo API operation for Object Storage Service. 1409 // 1410 // Get user qos. 1411 // 1412 // UserQoSConfiguration the User Qos and range Information. 1413 // 1414 // error it's nil if no error, otherwise it's an error object. 1415 // 1416 func (client Client) GetUserQoSInfo(options ...Option) (UserQoSConfiguration, error) { 1417 var out UserQoSConfiguration 1418 params := map[string]interface{}{} 1419 params["qosInfo"] = nil 1420 1421 resp, err := client.do("GET", "", params, nil, nil, options...) 1422 if err != nil { 1423 return out, err 1424 } 1425 defer resp.Body.Close() 1426 1427 err = xmlUnmarshal(resp.Body, &out) 1428 return out, err 1429 } 1430 1431 // SetBucketQoSInfo API operation for Object Storage Service. 1432 // 1433 // Set Bucket Qos information. 1434 // 1435 // bucketName the bucket name. 1436 // 1437 // qosConf the qos configuration. 1438 // 1439 // error it's nil if no error, otherwise it's an error object. 1440 // 1441 func (client Client) SetBucketQoSInfo(bucketName string, qosConf BucketQoSConfiguration, options ...Option) error { 1442 params := map[string]interface{}{} 1443 params["qosInfo"] = nil 1444 1445 var bs []byte 1446 bs, err := xml.Marshal(qosConf) 1447 if err != nil { 1448 return err 1449 } 1450 buffer := new(bytes.Buffer) 1451 buffer.Write(bs) 1452 1453 contentTpye := http.DetectContentType(buffer.Bytes()) 1454 headers := map[string]string{} 1455 headers[HTTPHeaderContentType] = contentTpye 1456 1457 resp, err := client.do("PUT", bucketName, params, headers, buffer, options...) 1458 if err != nil { 1459 return err 1460 } 1461 1462 defer resp.Body.Close() 1463 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 1464 } 1465 1466 // GetBucketQosInfo API operation for Object Storage Service. 1467 // 1468 // Get Bucket Qos information. 1469 // 1470 // bucketName the bucket name. 1471 // 1472 // BucketQoSConfiguration the return qos configuration. 1473 // 1474 // error it's nil if no error, otherwise it's an error object. 1475 // 1476 func (client Client) GetBucketQosInfo(bucketName string, options ...Option) (BucketQoSConfiguration, error) { 1477 var out BucketQoSConfiguration 1478 params := map[string]interface{}{} 1479 params["qosInfo"] = nil 1480 1481 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 1482 if err != nil { 1483 return out, err 1484 } 1485 defer resp.Body.Close() 1486 1487 err = xmlUnmarshal(resp.Body, &out) 1488 return out, err 1489 } 1490 1491 // DeleteBucketQosInfo API operation for Object Storage Service. 1492 // 1493 // Delete Bucket QoS information. 1494 // 1495 // bucketName the bucket name. 1496 // 1497 // error it's nil if no error, otherwise it's an error object. 1498 // 1499 func (client Client) DeleteBucketQosInfo(bucketName string, options ...Option) error { 1500 params := map[string]interface{}{} 1501 params["qosInfo"] = nil 1502 1503 resp, err := client.do("DELETE", bucketName, params, nil, nil, options...) 1504 if err != nil { 1505 return err 1506 } 1507 defer resp.Body.Close() 1508 1509 return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent}) 1510 } 1511 1512 // SetBucketInventory API operation for Object Storage Service 1513 // 1514 // Set the Bucket inventory. 1515 // 1516 // bucketName the bucket name. 1517 // 1518 // inventoryConfig the inventory configuration. 1519 // 1520 // error it's nil if no error, otherwise it's an error. 1521 // 1522 func (client Client) SetBucketInventory(bucketName string, inventoryConfig InventoryConfiguration, options ...Option) error { 1523 params := map[string]interface{}{} 1524 params["inventoryId"] = inventoryConfig.Id 1525 params["inventory"] = nil 1526 1527 var bs []byte 1528 bs, err := xml.Marshal(inventoryConfig) 1529 1530 if err != nil { 1531 return err 1532 } 1533 1534 buffer := new(bytes.Buffer) 1535 buffer.Write(bs) 1536 1537 contentType := http.DetectContentType(buffer.Bytes()) 1538 headers := make(map[string]string) 1539 headers[HTTPHeaderContentType] = contentType 1540 1541 resp, err := client.do("PUT", bucketName, params, headers, buffer, options...) 1542 1543 if err != nil { 1544 return err 1545 } 1546 1547 defer resp.Body.Close() 1548 1549 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 1550 } 1551 1552 // SetBucketInventoryXml API operation for Object Storage Service 1553 // 1554 // Set the Bucket inventory 1555 // 1556 // bucketName the bucket name. 1557 // 1558 // xmlBody the inventory configuration. 1559 // 1560 // error it's nil if no error, otherwise it's an error. 1561 // 1562 func (client Client) SetBucketInventoryXml(bucketName string, xmlBody string, options ...Option) error { 1563 var inventoryConfig InventoryConfiguration 1564 err := xml.Unmarshal([]byte(xmlBody), &inventoryConfig) 1565 if err != nil { 1566 return err 1567 } 1568 1569 if inventoryConfig.Id == "" { 1570 return fmt.Errorf("inventory id is empty in xml") 1571 } 1572 1573 params := map[string]interface{}{} 1574 params["inventoryId"] = inventoryConfig.Id 1575 params["inventory"] = nil 1576 1577 buffer := new(bytes.Buffer) 1578 buffer.Write([]byte(xmlBody)) 1579 1580 contentType := http.DetectContentType(buffer.Bytes()) 1581 headers := make(map[string]string) 1582 headers[HTTPHeaderContentType] = contentType 1583 1584 resp, err := client.do("PUT", bucketName, params, headers, buffer, options...) 1585 if err != nil { 1586 return err 1587 } 1588 1589 defer resp.Body.Close() 1590 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 1591 } 1592 1593 // GetBucketInventory API operation for Object Storage Service 1594 // 1595 // Get the Bucket inventory. 1596 // 1597 // bucketName tht bucket name. 1598 // 1599 // strInventoryId the inventory id. 1600 // 1601 // InventoryConfiguration the inventory configuration. 1602 // 1603 // error it's nil if no error, otherwise it's an error. 1604 // 1605 func (client Client) GetBucketInventory(bucketName string, strInventoryId string, options ...Option) (InventoryConfiguration, error) { 1606 var out InventoryConfiguration 1607 params := map[string]interface{}{} 1608 params["inventory"] = nil 1609 params["inventoryId"] = strInventoryId 1610 1611 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 1612 if err != nil { 1613 return out, err 1614 } 1615 defer resp.Body.Close() 1616 1617 err = xmlUnmarshal(resp.Body, &out) 1618 return out, err 1619 } 1620 1621 // GetBucketInventoryXml API operation for Object Storage Service 1622 // 1623 // Get the Bucket inventory. 1624 // 1625 // bucketName tht bucket name. 1626 // 1627 // strInventoryId the inventory id. 1628 // 1629 // InventoryConfiguration the inventory configuration. 1630 // 1631 // error it's nil if no error, otherwise it's an error. 1632 // 1633 func (client Client) GetBucketInventoryXml(bucketName string, strInventoryId string, options ...Option) (string, error) { 1634 params := map[string]interface{}{} 1635 params["inventory"] = nil 1636 params["inventoryId"] = strInventoryId 1637 1638 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 1639 if err != nil { 1640 return "", err 1641 } 1642 defer resp.Body.Close() 1643 body, err := ioutil.ReadAll(resp.Body) 1644 out := string(body) 1645 return out, err 1646 } 1647 1648 // ListBucketInventory API operation for Object Storage Service 1649 // 1650 // List the Bucket inventory. 1651 // 1652 // bucketName tht bucket name. 1653 // 1654 // continuationToken the users token. 1655 // 1656 // ListInventoryConfigurationsResult list all inventory configuration by . 1657 // 1658 // error it's nil if no error, otherwise it's an error. 1659 // 1660 func (client Client) ListBucketInventory(bucketName, continuationToken string, options ...Option) (ListInventoryConfigurationsResult, error) { 1661 var out ListInventoryConfigurationsResult 1662 params := map[string]interface{}{} 1663 params["inventory"] = nil 1664 if continuationToken == "" { 1665 params["continuation-token"] = nil 1666 } else { 1667 params["continuation-token"] = continuationToken 1668 } 1669 1670 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 1671 if err != nil { 1672 return out, err 1673 } 1674 defer resp.Body.Close() 1675 1676 err = xmlUnmarshal(resp.Body, &out) 1677 return out, err 1678 } 1679 1680 // ListBucketInventoryXml API operation for Object Storage Service 1681 // 1682 // List the Bucket inventory. 1683 // 1684 // bucketName tht bucket name. 1685 // 1686 // continuationToken the users token. 1687 // 1688 // ListInventoryConfigurationsResult list all inventory configuration by . 1689 // 1690 // error it's nil if no error, otherwise it's an error. 1691 // 1692 func (client Client) ListBucketInventoryXml(bucketName, continuationToken string, options ...Option) (string, error) { 1693 params := map[string]interface{}{} 1694 params["inventory"] = nil 1695 if continuationToken == "" { 1696 params["continuation-token"] = nil 1697 } else { 1698 params["continuation-token"] = continuationToken 1699 } 1700 1701 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 1702 if err != nil { 1703 return "", err 1704 } 1705 defer resp.Body.Close() 1706 body, err := ioutil.ReadAll(resp.Body) 1707 out := string(body) 1708 return out, err 1709 } 1710 1711 // DeleteBucketInventory API operation for Object Storage Service. 1712 // 1713 // Delete Bucket inventory information. 1714 // 1715 // bucketName tht bucket name. 1716 // 1717 // strInventoryId the inventory id. 1718 // 1719 // error it's nil if no error, otherwise it's an error. 1720 // 1721 func (client Client) DeleteBucketInventory(bucketName, strInventoryId string, options ...Option) error { 1722 params := map[string]interface{}{} 1723 params["inventory"] = nil 1724 params["inventoryId"] = strInventoryId 1725 1726 resp, err := client.do("DELETE", bucketName, params, nil, nil, options...) 1727 if err != nil { 1728 return err 1729 } 1730 defer resp.Body.Close() 1731 1732 return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent}) 1733 } 1734 1735 // SetBucketAsyncTask API operation for set async fetch task 1736 // 1737 // bucketName tht bucket name. 1738 // 1739 // asynConf configruation 1740 // 1741 // error it's nil if success, otherwise it's an error. 1742 func (client Client) SetBucketAsyncTask(bucketName string, asynConf AsyncFetchTaskConfiguration, options ...Option) (AsyncFetchTaskResult, error) { 1743 var out AsyncFetchTaskResult 1744 params := map[string]interface{}{} 1745 params["asyncFetch"] = nil 1746 1747 var bs []byte 1748 bs, err := xml.Marshal(asynConf) 1749 1750 if err != nil { 1751 return out, err 1752 } 1753 1754 buffer := new(bytes.Buffer) 1755 buffer.Write(bs) 1756 1757 contentType := http.DetectContentType(buffer.Bytes()) 1758 headers := make(map[string]string) 1759 headers[HTTPHeaderContentType] = contentType 1760 1761 resp, err := client.do("POST", bucketName, params, headers, buffer, options...) 1762 1763 if err != nil { 1764 return out, err 1765 } 1766 1767 defer resp.Body.Close() 1768 err = xmlUnmarshal(resp.Body, &out) 1769 return out, err 1770 } 1771 1772 // GetBucketAsyncTask API operation for set async fetch task 1773 // 1774 // bucketName tht bucket name. 1775 // 1776 // taskid returned by SetBucketAsyncTask 1777 // 1778 // error it's nil if success, otherwise it's an error. 1779 func (client Client) GetBucketAsyncTask(bucketName string, taskID string, options ...Option) (AsynFetchTaskInfo, error) { 1780 var out AsynFetchTaskInfo 1781 params := map[string]interface{}{} 1782 params["asyncFetch"] = nil 1783 1784 headers := make(map[string]string) 1785 headers[HTTPHeaderOssTaskID] = taskID 1786 resp, err := client.do("GET", bucketName, params, headers, nil, options...) 1787 if err != nil { 1788 return out, err 1789 } 1790 defer resp.Body.Close() 1791 err = xmlUnmarshal(resp.Body, &out) 1792 return out, err 1793 } 1794 1795 // InitiateBucketWorm creates bucket worm Configuration 1796 // bucketName the bucket name. 1797 // retentionDays the retention period in days 1798 // error it's nil if no error, otherwise it's an error object. 1799 // 1800 func (client Client) InitiateBucketWorm(bucketName string, retentionDays int, options ...Option) (string, error) { 1801 var initiateWormConf InitiateWormConfiguration 1802 initiateWormConf.RetentionPeriodInDays = retentionDays 1803 1804 var respHeader http.Header 1805 isOptSet, _, _ := IsOptionSet(options, responseHeader) 1806 if !isOptSet { 1807 options = append(options, GetResponseHeader(&respHeader)) 1808 } 1809 1810 bs, err := xml.Marshal(initiateWormConf) 1811 if err != nil { 1812 return "", err 1813 } 1814 buffer := new(bytes.Buffer) 1815 buffer.Write(bs) 1816 1817 contentType := http.DetectContentType(buffer.Bytes()) 1818 headers := make(map[string]string) 1819 headers[HTTPHeaderContentType] = contentType 1820 1821 params := map[string]interface{}{} 1822 params["worm"] = nil 1823 1824 resp, err := client.do("POST", bucketName, params, headers, buffer, options...) 1825 if err != nil { 1826 return "", err 1827 } 1828 defer resp.Body.Close() 1829 1830 respOpt, _ := FindOption(options, responseHeader, nil) 1831 wormID := "" 1832 err = CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 1833 if err == nil && respOpt != nil { 1834 wormID = (respOpt.(*http.Header)).Get("x-oss-worm-id") 1835 } 1836 return wormID, err 1837 } 1838 1839 // AbortBucketWorm delete bucket worm Configuration 1840 // bucketName the bucket name. 1841 // error it's nil if no error, otherwise it's an error object. 1842 // 1843 func (client Client) AbortBucketWorm(bucketName string, options ...Option) error { 1844 params := map[string]interface{}{} 1845 params["worm"] = nil 1846 resp, err := client.do("DELETE", bucketName, params, nil, nil, options...) 1847 if err != nil { 1848 return err 1849 } 1850 defer resp.Body.Close() 1851 return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent}) 1852 } 1853 1854 // CompleteBucketWorm complete bucket worm Configuration 1855 // bucketName the bucket name. 1856 // wormID the worm id 1857 // error it's nil if no error, otherwise it's an error object. 1858 // 1859 func (client Client) CompleteBucketWorm(bucketName string, wormID string, options ...Option) error { 1860 params := map[string]interface{}{} 1861 params["wormId"] = wormID 1862 resp, err := client.do("POST", bucketName, params, nil, nil, options...) 1863 if err != nil { 1864 return err 1865 } 1866 defer resp.Body.Close() 1867 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 1868 } 1869 1870 // ExtendBucketWorm exetend bucket worm Configuration 1871 // bucketName the bucket name. 1872 // retentionDays the retention period in days 1873 // wormID the worm id 1874 // error it's nil if no error, otherwise it's an error object. 1875 // 1876 func (client Client) ExtendBucketWorm(bucketName string, retentionDays int, wormID string, options ...Option) error { 1877 var extendWormConf ExtendWormConfiguration 1878 extendWormConf.RetentionPeriodInDays = retentionDays 1879 1880 bs, err := xml.Marshal(extendWormConf) 1881 if err != nil { 1882 return err 1883 } 1884 buffer := new(bytes.Buffer) 1885 buffer.Write(bs) 1886 1887 contentType := http.DetectContentType(buffer.Bytes()) 1888 headers := make(map[string]string) 1889 headers[HTTPHeaderContentType] = contentType 1890 1891 params := map[string]interface{}{} 1892 params["wormId"] = wormID 1893 params["wormExtend"] = nil 1894 1895 resp, err := client.do("POST", bucketName, params, headers, buffer, options...) 1896 if err != nil { 1897 return err 1898 } 1899 defer resp.Body.Close() 1900 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 1901 } 1902 1903 // GetBucketWorm get bucket worm Configuration 1904 // bucketName the bucket name. 1905 // error it's nil if no error, otherwise it's an error object. 1906 // 1907 func (client Client) GetBucketWorm(bucketName string, options ...Option) (WormConfiguration, error) { 1908 var out WormConfiguration 1909 params := map[string]interface{}{} 1910 params["worm"] = nil 1911 1912 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 1913 if err != nil { 1914 return out, err 1915 } 1916 defer resp.Body.Close() 1917 err = xmlUnmarshal(resp.Body, &out) 1918 return out, err 1919 } 1920 1921 // SetBucketTransferAcc set bucket transfer acceleration configuration 1922 // bucketName the bucket name. 1923 // accConf bucket transfer acceleration configuration 1924 // error it's nil if no error, otherwise it's an error object. 1925 // 1926 func (client Client) SetBucketTransferAcc(bucketName string, accConf TransferAccConfiguration, options ...Option) error { 1927 bs, err := xml.Marshal(accConf) 1928 if err != nil { 1929 return err 1930 } 1931 buffer := new(bytes.Buffer) 1932 buffer.Write(bs) 1933 1934 contentType := http.DetectContentType(buffer.Bytes()) 1935 headers := make(map[string]string) 1936 headers[HTTPHeaderContentType] = contentType 1937 1938 params := map[string]interface{}{} 1939 params["transferAcceleration"] = nil 1940 resp, err := client.do("PUT", bucketName, params, headers, buffer, options...) 1941 if err != nil { 1942 return err 1943 } 1944 defer resp.Body.Close() 1945 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 1946 } 1947 1948 // GetBucketTransferAcc get bucket transfer acceleration configuration 1949 // bucketName the bucket name. 1950 // accConf bucket transfer acceleration configuration 1951 // error it's nil if no error, otherwise it's an error object. 1952 // 1953 func (client Client) GetBucketTransferAcc(bucketName string, options ...Option) (TransferAccConfiguration, error) { 1954 var out TransferAccConfiguration 1955 params := map[string]interface{}{} 1956 params["transferAcceleration"] = nil 1957 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 1958 if err != nil { 1959 return out, err 1960 } 1961 defer resp.Body.Close() 1962 1963 err = xmlUnmarshal(resp.Body, &out) 1964 return out, err 1965 } 1966 1967 // DeleteBucketTransferAcc delete bucket transfer acceleration configuration 1968 // bucketName the bucket name. 1969 // error it's nil if no error, otherwise it's an error object. 1970 // 1971 func (client Client) DeleteBucketTransferAcc(bucketName string, options ...Option) error { 1972 params := map[string]interface{}{} 1973 params["transferAcceleration"] = nil 1974 resp, err := client.do("DELETE", bucketName, params, nil, nil, options...) 1975 if err != nil { 1976 return err 1977 } 1978 defer resp.Body.Close() 1979 return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent}) 1980 } 1981 1982 // PutBucketReplication put bucket replication configuration 1983 // bucketName the bucket name. 1984 // xmlBody the replication configuration. 1985 // error it's nil if no error, otherwise it's an error object. 1986 // 1987 func (client Client) PutBucketReplication(bucketName string, xmlBody string, options ...Option) error { 1988 buffer := new(bytes.Buffer) 1989 buffer.Write([]byte(xmlBody)) 1990 1991 contentType := http.DetectContentType(buffer.Bytes()) 1992 headers := map[string]string{} 1993 headers[HTTPHeaderContentType] = contentType 1994 1995 params := map[string]interface{}{} 1996 params["replication"] = nil 1997 params["comp"] = "add" 1998 resp, err := client.do("POST", bucketName, params, headers, buffer, options...) 1999 if err != nil { 2000 return err 2001 } 2002 defer resp.Body.Close() 2003 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 2004 } 2005 2006 // PutBucketRTC put bucket replication rtc 2007 // bucketName the bucket name. 2008 // rtc the bucket rtc config. 2009 // error it's nil if no error, otherwise it's an error object. 2010 // 2011 func (client Client) PutBucketRTC(bucketName string, rtc PutBucketRTC, options ...Option) error { 2012 bs, err := xml.Marshal(rtc) 2013 if err != nil { 2014 return err 2015 } 2016 err = client.PutBucketRTCXml(bucketName, string(bs), options...) 2017 return err 2018 } 2019 2020 // PutBucketRTCXml put bucket rtc configuration 2021 // bucketName the bucket name. 2022 // xmlBody the rtc configuration in xml format. 2023 // error it's nil if no error, otherwise it's an error object. 2024 // 2025 func (client Client) PutBucketRTCXml(bucketName string, xmlBody string, options ...Option) error { 2026 buffer := new(bytes.Buffer) 2027 buffer.Write([]byte(xmlBody)) 2028 2029 contentType := http.DetectContentType(buffer.Bytes()) 2030 headers := map[string]string{} 2031 headers[HTTPHeaderContentType] = contentType 2032 2033 params := map[string]interface{}{} 2034 params["rtc"] = nil 2035 resp, err := client.do("PUT", bucketName, params, headers, buffer, options...) 2036 if err != nil { 2037 return err 2038 } 2039 defer resp.Body.Close() 2040 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 2041 } 2042 2043 // GetBucketReplication get bucket replication configuration 2044 // bucketName the bucket name. 2045 // string the replication configuration. 2046 // error it's nil if no error, otherwise it's an error object. 2047 // 2048 func (client Client) GetBucketReplication(bucketName string, options ...Option) (string, error) { 2049 params := map[string]interface{}{} 2050 params["replication"] = nil 2051 2052 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 2053 if err != nil { 2054 return "", err 2055 } 2056 defer resp.Body.Close() 2057 2058 data, err := ioutil.ReadAll(resp.Body) 2059 if err != nil { 2060 return "", err 2061 } 2062 return string(data), err 2063 } 2064 2065 // DeleteBucketReplication delete bucket replication configuration 2066 // bucketName the bucket name. 2067 // ruleId the ID of the replication configuration. 2068 // error it's nil if no error, otherwise it's an error object. 2069 // 2070 func (client Client) DeleteBucketReplication(bucketName string, ruleId string, options ...Option) error { 2071 replicationxml := ReplicationXML{} 2072 replicationxml.ID = ruleId 2073 2074 bs, err := xml.Marshal(replicationxml) 2075 if err != nil { 2076 return err 2077 } 2078 2079 buffer := new(bytes.Buffer) 2080 buffer.Write(bs) 2081 2082 contentType := http.DetectContentType(buffer.Bytes()) 2083 headers := map[string]string{} 2084 headers[HTTPHeaderContentType] = contentType 2085 2086 params := map[string]interface{}{} 2087 params["replication"] = nil 2088 params["comp"] = "delete" 2089 resp, err := client.do("POST", bucketName, params, headers, buffer, options...) 2090 if err != nil { 2091 return err 2092 } 2093 defer resp.Body.Close() 2094 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 2095 } 2096 2097 // GetBucketReplicationLocation get the locations of the target bucket that can be copied to 2098 // bucketName the bucket name. 2099 // string the locations of the target bucket that can be copied to. 2100 // error it's nil if no error, otherwise it's an error object. 2101 // 2102 func (client Client) GetBucketReplicationLocation(bucketName string, options ...Option) (string, error) { 2103 params := map[string]interface{}{} 2104 params["replicationLocation"] = nil 2105 2106 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 2107 if err != nil { 2108 return "", err 2109 } 2110 defer resp.Body.Close() 2111 2112 data, err := ioutil.ReadAll(resp.Body) 2113 if err != nil { 2114 return "", err 2115 } 2116 return string(data), err 2117 } 2118 2119 // GetBucketReplicationProgress get the replication progress of bucket 2120 // bucketName the bucket name. 2121 // ruleId the ID of the replication configuration. 2122 // string the replication progress of bucket. 2123 // error it's nil if no error, otherwise it's an error object. 2124 // 2125 func (client Client) GetBucketReplicationProgress(bucketName string, ruleId string, options ...Option) (string, error) { 2126 params := map[string]interface{}{} 2127 params["replicationProgress"] = nil 2128 if ruleId != "" { 2129 params["rule-id"] = ruleId 2130 } 2131 2132 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 2133 if err != nil { 2134 return "", err 2135 } 2136 defer resp.Body.Close() 2137 2138 data, err := ioutil.ReadAll(resp.Body) 2139 if err != nil { 2140 return "", err 2141 } 2142 return string(data), err 2143 } 2144 2145 // GetBucketAccessMonitor get bucket's access monitor config 2146 // bucketName the bucket name. 2147 // GetBucketAccessMonitorResult the access monitor configuration result of bucket. 2148 // error it's nil if no error, otherwise it's an error object. 2149 func (client Client) GetBucketAccessMonitor(bucketName string, options ...Option) (GetBucketAccessMonitorResult, error) { 2150 var out GetBucketAccessMonitorResult 2151 body, err := client.GetBucketAccessMonitorXml(bucketName, options...) 2152 if err != nil { 2153 return out, err 2154 } 2155 err = xmlUnmarshal(strings.NewReader(body), &out) 2156 return out, err 2157 } 2158 2159 // GetBucketAccessMonitorXml get bucket's access monitor config 2160 // bucketName the bucket name. 2161 // string the access monitor configuration result of bucket xml foramt. 2162 // error it's nil if no error, otherwise it's an error object. 2163 func (client Client) GetBucketAccessMonitorXml(bucketName string, options ...Option) (string, error) { 2164 params := map[string]interface{}{} 2165 params["accessmonitor"] = nil 2166 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 2167 if err != nil { 2168 return "", err 2169 } 2170 defer resp.Body.Close() 2171 body, err := ioutil.ReadAll(resp.Body) 2172 out := string(body) 2173 return out, err 2174 } 2175 2176 // PutBucketAccessMonitor get bucket's access monitor config 2177 // bucketName the bucket name. 2178 // accessMonitor the access monitor configuration of bucket. 2179 // error it's nil if no error, otherwise it's an error object. 2180 func (client Client) PutBucketAccessMonitor(bucketName string, accessMonitor PutBucketAccessMonitor, options ...Option) error { 2181 bs, err := xml.Marshal(accessMonitor) 2182 if err != nil { 2183 return err 2184 } 2185 err = client.PutBucketAccessMonitorXml(bucketName, string(bs), options...) 2186 return err 2187 } 2188 2189 // PutBucketAccessMonitorXml get bucket's access monitor config 2190 // bucketName the bucket name. 2191 // xmlData the access monitor configuration in xml foramt 2192 // error it's nil if no error, otherwise it's an error object. 2193 func (client Client) PutBucketAccessMonitorXml(bucketName string, xmlData string, options ...Option) error { 2194 buffer := new(bytes.Buffer) 2195 buffer.Write([]byte(xmlData)) 2196 contentType := http.DetectContentType(buffer.Bytes()) 2197 headers := map[string]string{} 2198 headers[HTTPHeaderContentType] = contentType 2199 params := map[string]interface{}{} 2200 params["accessmonitor"] = nil 2201 resp, err := client.do("PUT", bucketName, params, nil, buffer, options...) 2202 if err != nil { 2203 return err 2204 } 2205 defer resp.Body.Close() 2206 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 2207 } 2208 2209 // ListBucketCname list bucket's binding cname 2210 // bucketName the bucket name. 2211 // string the xml configuration of bucket. 2212 // error it's nil if no error, otherwise it's an error object. 2213 func (client Client) ListBucketCname(bucketName string, options ...Option) (ListBucketCnameResult, error) { 2214 var out ListBucketCnameResult 2215 body, err := client.GetBucketCname(bucketName, options...) 2216 if err != nil { 2217 return out, err 2218 } 2219 err = xmlUnmarshal(strings.NewReader(body), &out) 2220 return out, err 2221 } 2222 2223 // GetBucketCname get bucket's binding cname 2224 // bucketName the bucket name. 2225 // string the xml configuration of bucket. 2226 // error it's nil if no error, otherwise it's an error object. 2227 func (client Client) GetBucketCname(bucketName string, options ...Option) (string, error) { 2228 params := map[string]interface{}{} 2229 params["cname"] = nil 2230 2231 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 2232 if err != nil { 2233 return "", err 2234 } 2235 defer resp.Body.Close() 2236 2237 data, err := ioutil.ReadAll(resp.Body) 2238 if err != nil { 2239 return "", err 2240 } 2241 return string(data), err 2242 } 2243 2244 // CreateBucketCnameToken create a token for the cname. 2245 // bucketName the bucket name. 2246 // cname a custom domain name. 2247 // error it's nil if no error, otherwise it's an error object. 2248 func (client Client) CreateBucketCnameToken(bucketName string, cname string, options ...Option) (CreateBucketCnameTokenResult, error) { 2249 var out CreateBucketCnameTokenResult 2250 params := map[string]interface{}{} 2251 params["cname"] = nil 2252 params["comp"] = "token" 2253 2254 rxml := CnameConfigurationXML{} 2255 rxml.Domain = cname 2256 2257 bs, err := xml.Marshal(rxml) 2258 if err != nil { 2259 return out, err 2260 } 2261 buffer := new(bytes.Buffer) 2262 buffer.Write(bs) 2263 2264 contentType := http.DetectContentType(buffer.Bytes()) 2265 headers := map[string]string{} 2266 headers[HTTPHeaderContentType] = contentType 2267 2268 resp, err := client.do("POST", bucketName, params, headers, buffer, options...) 2269 if err != nil { 2270 return out, err 2271 } 2272 defer resp.Body.Close() 2273 2274 err = xmlUnmarshal(resp.Body, &out) 2275 return out, err 2276 } 2277 2278 // GetBucketCnameToken get a token for the cname 2279 // bucketName the bucket name. 2280 // cname a custom domain name. 2281 // error it's nil if no error, otherwise it's an error object. 2282 func (client Client) GetBucketCnameToken(bucketName string, cname string, options ...Option) (GetBucketCnameTokenResult, error) { 2283 var out GetBucketCnameTokenResult 2284 params := map[string]interface{}{} 2285 params["cname"] = cname 2286 params["comp"] = "token" 2287 2288 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 2289 if err != nil { 2290 return out, err 2291 } 2292 defer resp.Body.Close() 2293 2294 err = xmlUnmarshal(resp.Body, &out) 2295 return out, err 2296 } 2297 2298 // PutBucketCnameXml map a custom domain name to a bucket 2299 // bucketName the bucket name. 2300 // xmlBody the cname configuration in xml foramt 2301 // error it's nil if no error, otherwise it's an error object. 2302 func (client Client) PutBucketCnameXml(bucketName string, xmlBody string, options ...Option) error { 2303 params := map[string]interface{}{} 2304 params["cname"] = nil 2305 params["comp"] = "add" 2306 2307 buffer := new(bytes.Buffer) 2308 buffer.Write([]byte(xmlBody)) 2309 contentType := http.DetectContentType(buffer.Bytes()) 2310 headers := map[string]string{} 2311 headers[HTTPHeaderContentType] = contentType 2312 2313 resp, err := client.do("POST", bucketName, params, headers, buffer, options...) 2314 if err != nil { 2315 return err 2316 } 2317 defer resp.Body.Close() 2318 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 2319 } 2320 2321 // PutBucketCname map a custom domain name to a bucket 2322 // bucketName the bucket name. 2323 // cname a custom domain name. 2324 // error it's nil if no error, otherwise it's an error object. 2325 func (client Client) PutBucketCname(bucketName string, cname string, options ...Option) error { 2326 rxml := CnameConfigurationXML{} 2327 rxml.Domain = cname 2328 bs, err := xml.Marshal(rxml) 2329 if err != nil { 2330 return err 2331 } 2332 return client.PutBucketCnameXml(bucketName, string(bs), options...) 2333 } 2334 2335 // PutBucketCnameWithCertificate map a custom domain name to a bucket 2336 // bucketName the bucket name. 2337 // PutBucketCname the bucket cname config in struct format. 2338 // error it's nil if no error, otherwise it's an error object. 2339 func (client Client) PutBucketCnameWithCertificate(bucketName string, putBucketCname PutBucketCname, options ...Option) error { 2340 bs, err := xml.Marshal(putBucketCname) 2341 if err != nil { 2342 return err 2343 } 2344 return client.PutBucketCnameXml(bucketName, string(bs), options...) 2345 } 2346 2347 // DeleteBucketCname remove the mapping of the custom domain name from a bucket. 2348 // bucketName the bucket name. 2349 // cname a custom domain name. 2350 // error it's nil if no error, otherwise it's an error object. 2351 func (client Client) DeleteBucketCname(bucketName string, cname string, options ...Option) error { 2352 params := map[string]interface{}{} 2353 params["cname"] = nil 2354 params["comp"] = "delete" 2355 2356 rxml := CnameConfigurationXML{} 2357 rxml.Domain = cname 2358 2359 bs, err := xml.Marshal(rxml) 2360 if err != nil { 2361 return err 2362 } 2363 buffer := new(bytes.Buffer) 2364 buffer.Write(bs) 2365 2366 contentType := http.DetectContentType(buffer.Bytes()) 2367 headers := map[string]string{} 2368 headers[HTTPHeaderContentType] = contentType 2369 2370 resp, err := client.do("POST", bucketName, params, headers, buffer, options...) 2371 if err != nil { 2372 return err 2373 } 2374 defer resp.Body.Close() 2375 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 2376 } 2377 2378 // PutBucketResourceGroup set bucket's resource group 2379 // bucketName the bucket name. 2380 // resourceGroup the resource group configuration of bucket. 2381 // error it's nil if no error, otherwise it's an error object. 2382 func (client Client) PutBucketResourceGroup(bucketName string, resourceGroup PutBucketResourceGroup, options ...Option) error { 2383 bs, err := xml.Marshal(resourceGroup) 2384 if err != nil { 2385 return err 2386 } 2387 err = client.PutBucketResourceGroupXml(bucketName, string(bs), options...) 2388 return err 2389 } 2390 2391 // PutBucketResourceGroupXml set bucket's resource group 2392 // bucketName the bucket name. 2393 // xmlData the resource group in xml format 2394 // error it's nil if no error, otherwise it's an error object. 2395 func (client Client) PutBucketResourceGroupXml(bucketName string, xmlData string, options ...Option) error { 2396 buffer := new(bytes.Buffer) 2397 buffer.Write([]byte(xmlData)) 2398 contentType := http.DetectContentType(buffer.Bytes()) 2399 headers := map[string]string{} 2400 headers[HTTPHeaderContentType] = contentType 2401 params := map[string]interface{}{} 2402 params["resourceGroup"] = nil 2403 resp, err := client.do("PUT", bucketName, params, nil, buffer, options...) 2404 if err != nil { 2405 return err 2406 } 2407 defer resp.Body.Close() 2408 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 2409 } 2410 2411 // GetBucketResourceGroup get bucket's resource group 2412 // bucketName the bucket name. 2413 // GetBucketResourceGroupResult the resource group configuration result of bucket. 2414 // error it's nil if no error, otherwise it's an error object. 2415 func (client Client) GetBucketResourceGroup(bucketName string, options ...Option) (GetBucketResourceGroupResult, error) { 2416 var out GetBucketResourceGroupResult 2417 body, err := client.GetBucketResourceGroupXml(bucketName, options...) 2418 if err != nil { 2419 return out, err 2420 } 2421 err = xmlUnmarshal(strings.NewReader(body), &out) 2422 return out, err 2423 } 2424 2425 // GetBucketResourceGroupXml get bucket's resource group 2426 // bucketName the bucket name. 2427 // string the resource group result of bucket xml format. 2428 // error it's nil if no error, otherwise it's an error object. 2429 func (client Client) GetBucketResourceGroupXml(bucketName string, options ...Option) (string, error) { 2430 params := map[string]interface{}{} 2431 params["resourceGroup"] = nil 2432 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 2433 if err != nil { 2434 return "", err 2435 } 2436 defer resp.Body.Close() 2437 body, err := ioutil.ReadAll(resp.Body) 2438 out := string(body) 2439 return out, err 2440 } 2441 2442 // PutBucketStyle set bucket's style 2443 // bucketName the bucket name. 2444 // styleContent the style content. 2445 // error it's nil if no error, otherwise it's an error object. 2446 func (client Client) PutBucketStyle(bucketName, styleName string, styleContent string, options ...Option) error { 2447 bs := fmt.Sprintf("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Style><Content>%s</Content></Style>", styleContent) 2448 err := client.PutBucketStyleXml(bucketName, styleName, bs, options...) 2449 return err 2450 } 2451 2452 // PutBucketStyleXml set bucket's style 2453 // bucketName the bucket name. 2454 // styleName the style name. 2455 // xmlData the style in xml format 2456 // error it's nil if no error, otherwise it's an error object. 2457 func (client Client) PutBucketStyleXml(bucketName, styleName, xmlData string, options ...Option) error { 2458 buffer := new(bytes.Buffer) 2459 buffer.Write([]byte(xmlData)) 2460 contentType := http.DetectContentType(buffer.Bytes()) 2461 headers := map[string]string{} 2462 headers[HTTPHeaderContentType] = contentType 2463 params := map[string]interface{}{} 2464 params["style"] = nil 2465 params["styleName"] = styleName 2466 resp, err := client.do("PUT", bucketName, params, nil, buffer, options...) 2467 if err != nil { 2468 return err 2469 } 2470 defer resp.Body.Close() 2471 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 2472 } 2473 2474 // GetBucketStyle get bucket's style 2475 // bucketName the bucket name. 2476 // styleName the bucket style name. 2477 // GetBucketStyleResult the style result of bucket. 2478 // error it's nil if no error, otherwise it's an error object. 2479 func (client Client) GetBucketStyle(bucketName, styleName string, options ...Option) (GetBucketStyleResult, error) { 2480 var out GetBucketStyleResult 2481 body, err := client.GetBucketStyleXml(bucketName, styleName, options...) 2482 if err != nil { 2483 return out, err 2484 } 2485 err = xmlUnmarshal(strings.NewReader(body), &out) 2486 return out, err 2487 } 2488 2489 // GetBucketStyleXml get bucket's style 2490 // bucketName the bucket name. 2491 // styleName the bucket style name. 2492 // string the style result of bucket in xml format. 2493 // error it's nil if no error, otherwise it's an error object. 2494 func (client Client) GetBucketStyleXml(bucketName, styleName string, options ...Option) (string, error) { 2495 params := map[string]interface{}{} 2496 params["style"] = nil 2497 params["styleName"] = styleName 2498 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 2499 if err != nil { 2500 return "", err 2501 } 2502 defer resp.Body.Close() 2503 body, err := ioutil.ReadAll(resp.Body) 2504 out := string(body) 2505 return out, err 2506 } 2507 2508 // ListBucketStyle get bucket's styles 2509 // bucketName the bucket name. 2510 // GetBucketListStyleResult the list style result of bucket. 2511 // error it's nil if no error, otherwise it's an error object. 2512 func (client Client) ListBucketStyle(bucketName string, options ...Option) (GetBucketListStyleResult, error) { 2513 var out GetBucketListStyleResult 2514 body, err := client.ListBucketStyleXml(bucketName, options...) 2515 if err != nil { 2516 return out, err 2517 } 2518 err = xmlUnmarshal(strings.NewReader(body), &out) 2519 return out, err 2520 } 2521 2522 // ListBucketStyleXml get bucket's list style 2523 // bucketName the bucket name. 2524 // string the style result of bucket in xml format. 2525 // error it's nil if no error, otherwise it's an error object. 2526 func (client Client) ListBucketStyleXml(bucketName string, options ...Option) (string, error) { 2527 params := map[string]interface{}{} 2528 params["style"] = nil 2529 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 2530 if err != nil { 2531 return "", err 2532 } 2533 defer resp.Body.Close() 2534 body, err := ioutil.ReadAll(resp.Body) 2535 out := string(body) 2536 return out, err 2537 } 2538 2539 // DeleteBucketStyle delete bucket's style 2540 // bucketName the bucket name. 2541 // styleName the bucket style name. 2542 // string the style result of bucket in xml format. 2543 // error it's nil if no error, otherwise it's an error object. 2544 func (client Client) DeleteBucketStyle(bucketName, styleName string, options ...Option) error { 2545 params := map[string]interface{}{} 2546 params["style"] = bucketName 2547 params["styleName"] = styleName 2548 resp, err := client.do("DELETE", bucketName, params, nil, nil, options...) 2549 if err != nil { 2550 return err 2551 } 2552 defer resp.Body.Close() 2553 return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent}) 2554 } 2555 2556 // PutBucketResponseHeader set bucket response header 2557 // bucketName the bucket name. 2558 // xmlData the resource group in xml format 2559 // error it's nil if no error, otherwise it's an error object. 2560 func (client Client) PutBucketResponseHeader(bucketName string, responseHeader PutBucketResponseHeader, options ...Option) error { 2561 bs, err := xml.Marshal(responseHeader) 2562 if err != nil { 2563 return err 2564 } 2565 err = client.PutBucketResponseHeaderXml(bucketName, string(bs), options...) 2566 return err 2567 } 2568 2569 // PutBucketResponseHeaderXml set bucket response header 2570 // bucketName the bucket name. 2571 // xmlData the bucket response header in xml format 2572 // error it's nil if no error, otherwise it's an error object. 2573 func (client Client) PutBucketResponseHeaderXml(bucketName, xmlData string, options ...Option) error { 2574 buffer := new(bytes.Buffer) 2575 buffer.Write([]byte(xmlData)) 2576 contentType := http.DetectContentType(buffer.Bytes()) 2577 headers := map[string]string{} 2578 headers[HTTPHeaderContentType] = contentType 2579 params := map[string]interface{}{} 2580 params["responseHeader"] = nil 2581 resp, err := client.do("PUT", bucketName, params, nil, buffer, options...) 2582 if err != nil { 2583 return err 2584 } 2585 defer resp.Body.Close() 2586 return CheckRespCode(resp.StatusCode, []int{http.StatusOK}) 2587 } 2588 2589 // GetBucketResponseHeader get bucket's response header. 2590 // bucketName the bucket name. 2591 // GetBucketResponseHeaderResult the response header result of bucket. 2592 // error it's nil if no error, otherwise it's an error object. 2593 func (client Client) GetBucketResponseHeader(bucketName string, options ...Option) (GetBucketResponseHeaderResult, error) { 2594 var out GetBucketResponseHeaderResult 2595 body, err := client.GetBucketResponseHeaderXml(bucketName, options...) 2596 if err != nil { 2597 return out, err 2598 } 2599 err = xmlUnmarshal(strings.NewReader(body), &out) 2600 return out, err 2601 } 2602 2603 // GetBucketResponseHeaderXml get bucket's resource group 2604 // bucketName the bucket name. 2605 // string the response header result of bucket xml format. 2606 // error it's nil if no error, otherwise it's an error object. 2607 func (client Client) GetBucketResponseHeaderXml(bucketName string, options ...Option) (string, error) { 2608 params := map[string]interface{}{} 2609 params["responseHeader"] = nil 2610 resp, err := client.do("GET", bucketName, params, nil, nil, options...) 2611 if err != nil { 2612 return "", err 2613 } 2614 defer resp.Body.Close() 2615 body, err := ioutil.ReadAll(resp.Body) 2616 out := string(body) 2617 return out, err 2618 } 2619 2620 // DeleteBucketResponseHeader delete response header from a bucket. 2621 // bucketName the bucket name. 2622 // error it's nil if no error, otherwise it's an error object. 2623 func (client Client) DeleteBucketResponseHeader(bucketName string, options ...Option) error { 2624 params := map[string]interface{}{} 2625 params["responseHeader"] = nil 2626 resp, err := client.do("DELETE", bucketName, params, nil, nil, options...) 2627 2628 if err != nil { 2629 return err 2630 } 2631 defer resp.Body.Close() 2632 return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent}) 2633 } 2634 2635 // DescribeRegions get describe regions 2636 // GetDescribeRegionsResult the result of bucket in xml format. 2637 // error it's nil if no error, otherwise it's an error object. 2638 func (client Client) DescribeRegions(options ...Option) (DescribeRegionsResult, error) { 2639 var out DescribeRegionsResult 2640 body, err := client.DescribeRegionsXml(options...) 2641 if err != nil { 2642 return out, err 2643 } 2644 err = xmlUnmarshal(strings.NewReader(body), &out) 2645 return out, err 2646 } 2647 2648 // DescribeRegionsXml get describe regions 2649 // string the style result of bucket in xml format. 2650 // error it's nil if no error, otherwise it's an error object. 2651 func (client Client) DescribeRegionsXml(options ...Option) (string, error) { 2652 params, err := GetRawParams(options) 2653 if err != nil { 2654 return "", err 2655 } 2656 if params["regions"] == nil { 2657 params["regions"] = nil 2658 } 2659 resp, err := client.do("GET", "", params, nil, nil, options...) 2660 if err != nil { 2661 return "", err 2662 } 2663 defer resp.Body.Close() 2664 body, err := ioutil.ReadAll(resp.Body) 2665 out := string(body) 2666 return out, err 2667 } 2668 2669 // LimitUploadSpeed set upload bandwidth limit speed,default is 0,unlimited 2670 // upSpeed KB/s, 0 is unlimited,default is 0 2671 // error it's nil if success, otherwise failure 2672 func (client Client) LimitUploadSpeed(upSpeed int) error { 2673 if client.Config == nil { 2674 return fmt.Errorf("client config is nil") 2675 } 2676 return client.Config.LimitUploadSpeed(upSpeed) 2677 } 2678 2679 // LimitDownloadSpeed set download bandwidth limit speed,default is 0,unlimited 2680 // downSpeed KB/s, 0 is unlimited,default is 0 2681 // error it's nil if success, otherwise failure 2682 func (client Client) LimitDownloadSpeed(downSpeed int) error { 2683 if client.Config == nil { 2684 return fmt.Errorf("client config is nil") 2685 } 2686 return client.Config.LimitDownloadSpeed(downSpeed) 2687 } 2688 2689 // UseCname sets the flag of using CName. By default it's false. 2690 // 2691 // isUseCname true: the endpoint has the CName, false: the endpoint does not have cname. Default is false. 2692 // 2693 func UseCname(isUseCname bool) ClientOption { 2694 return func(client *Client) { 2695 client.Config.IsCname = isUseCname 2696 } 2697 } 2698 2699 // ForcePathStyle sets the flag of using Path Style. By default it's false. 2700 // 2701 // isPathStyle true: the endpoint has the Path Style, false: the endpoint does not have Path Style. Default is false. 2702 // 2703 func ForcePathStyle(isPathStyle bool) ClientOption { 2704 return func(client *Client) { 2705 client.Config.IsPathStyle = isPathStyle 2706 } 2707 } 2708 2709 // Timeout sets the HTTP timeout in seconds. 2710 // 2711 // connectTimeoutSec HTTP timeout in seconds. Default is 10 seconds. 0 means infinite (not recommended) 2712 // readWriteTimeout HTTP read or write's timeout in seconds. Default is 20 seconds. 0 means infinite. 2713 // 2714 func Timeout(connectTimeoutSec, readWriteTimeout int64) ClientOption { 2715 return func(client *Client) { 2716 client.Config.HTTPTimeout.ConnectTimeout = 2717 time.Second * time.Duration(connectTimeoutSec) 2718 client.Config.HTTPTimeout.ReadWriteTimeout = 2719 time.Second * time.Duration(readWriteTimeout) 2720 client.Config.HTTPTimeout.HeaderTimeout = 2721 time.Second * time.Duration(readWriteTimeout) 2722 client.Config.HTTPTimeout.IdleConnTimeout = 2723 time.Second * time.Duration(readWriteTimeout) 2724 client.Config.HTTPTimeout.LongTimeout = 2725 time.Second * time.Duration(readWriteTimeout*10) 2726 } 2727 } 2728 2729 // MaxConns sets the HTTP max connections for a client. 2730 // 2731 // maxIdleConns controls the maximum number of idle (keep-alive) connections across all hosts. Default is 100. 2732 // maxIdleConnsPerHost controls the maximum idle (keep-alive) connections to keep per-host. Default is 100. 2733 // maxConnsPerHost limits the total number of connections per host. Default is no limit. 2734 // 2735 func MaxConns(maxIdleConns, maxIdleConnsPerHost, maxConnsPerHost int) ClientOption { 2736 return func(client *Client) { 2737 client.Config.HTTPMaxConns.MaxIdleConns = maxIdleConns 2738 client.Config.HTTPMaxConns.MaxIdleConnsPerHost = maxIdleConnsPerHost 2739 client.Config.HTTPMaxConns.MaxConnsPerHost = maxConnsPerHost 2740 } 2741 } 2742 2743 // SecurityToken sets the temporary user's SecurityToken. 2744 // 2745 // token STS token 2746 // 2747 func SecurityToken(token string) ClientOption { 2748 return func(client *Client) { 2749 client.Config.SecurityToken = strings.TrimSpace(token) 2750 } 2751 } 2752 2753 // EnableMD5 enables MD5 validation. 2754 // 2755 // isEnableMD5 true: enable MD5 validation; false: disable MD5 validation. 2756 // 2757 func EnableMD5(isEnableMD5 bool) ClientOption { 2758 return func(client *Client) { 2759 client.Config.IsEnableMD5 = isEnableMD5 2760 } 2761 } 2762 2763 // MD5ThresholdCalcInMemory sets the memory usage threshold for computing the MD5, default is 16MB. 2764 // 2765 // threshold the memory threshold in bytes. When the uploaded content is more than 16MB, the temp file is used for computing the MD5. 2766 // 2767 func MD5ThresholdCalcInMemory(threshold int64) ClientOption { 2768 return func(client *Client) { 2769 client.Config.MD5Threshold = threshold 2770 } 2771 } 2772 2773 // EnableCRC enables the CRC checksum. Default is true. 2774 // 2775 // isEnableCRC true: enable CRC checksum; false: disable the CRC checksum. 2776 // 2777 func EnableCRC(isEnableCRC bool) ClientOption { 2778 return func(client *Client) { 2779 client.Config.IsEnableCRC = isEnableCRC 2780 } 2781 } 2782 2783 // UserAgent specifies UserAgent. The default is aliyun-sdk-go/1.2.0 (windows/-/amd64;go1.5.2). 2784 // 2785 // userAgent the user agent string. 2786 // 2787 func UserAgent(userAgent string) ClientOption { 2788 return func(client *Client) { 2789 client.Config.UserAgent = userAgent 2790 client.Config.UserSetUa = true 2791 } 2792 } 2793 2794 // Proxy sets the proxy (optional). The default is not using proxy. 2795 // 2796 // proxyHost the proxy host in the format "host:port". For example, proxy.com:80 . 2797 // 2798 func Proxy(proxyHost string) ClientOption { 2799 return func(client *Client) { 2800 client.Config.IsUseProxy = true 2801 client.Config.ProxyHost = proxyHost 2802 } 2803 } 2804 2805 // AuthProxy sets the proxy information with user name and password. 2806 // 2807 // proxyHost the proxy host in the format "host:port". For example, proxy.com:80 . 2808 // proxyUser the proxy user name. 2809 // proxyPassword the proxy password. 2810 // 2811 func AuthProxy(proxyHost, proxyUser, proxyPassword string) ClientOption { 2812 return func(client *Client) { 2813 client.Config.IsUseProxy = true 2814 client.Config.ProxyHost = proxyHost 2815 client.Config.IsAuthProxy = true 2816 client.Config.ProxyUser = proxyUser 2817 client.Config.ProxyPassword = proxyPassword 2818 } 2819 } 2820 2821 // 2822 // HTTPClient sets the http.Client in use to the one passed in 2823 // 2824 func HTTPClient(HTTPClient *http.Client) ClientOption { 2825 return func(client *Client) { 2826 client.HTTPClient = HTTPClient 2827 } 2828 } 2829 2830 // 2831 // SetLogLevel sets the oss sdk log level 2832 // 2833 func SetLogLevel(LogLevel int) ClientOption { 2834 return func(client *Client) { 2835 client.Config.LogLevel = LogLevel 2836 } 2837 } 2838 2839 // 2840 // SetLogger sets the oss sdk logger 2841 // 2842 func SetLogger(Logger *log.Logger) ClientOption { 2843 return func(client *Client) { 2844 client.Config.Logger = Logger 2845 } 2846 } 2847 2848 // SetCredentialsProvider sets function for get the user's ak 2849 func SetCredentialsProvider(provider CredentialsProvider) ClientOption { 2850 return func(client *Client) { 2851 client.Config.CredentialsProvider = provider 2852 } 2853 } 2854 2855 // SetLocalAddr sets function for local addr 2856 func SetLocalAddr(localAddr net.Addr) ClientOption { 2857 return func(client *Client) { 2858 client.Config.LocalAddr = localAddr 2859 } 2860 } 2861 2862 // AuthVersion sets auth version: v1 or v2 signature which oss_server needed 2863 func AuthVersion(authVersion AuthVersionType) ClientOption { 2864 return func(client *Client) { 2865 client.Config.AuthVersion = authVersion 2866 } 2867 } 2868 2869 // AdditionalHeaders sets special http headers needed to be signed 2870 func AdditionalHeaders(headers []string) ClientOption { 2871 return func(client *Client) { 2872 client.Config.AdditionalHeaders = headers 2873 } 2874 } 2875 2876 // RedirectEnabled only effective from go1.7 onward,RedirectEnabled set http redirect enabled or not 2877 func RedirectEnabled(enabled bool) ClientOption { 2878 return func(client *Client) { 2879 client.Config.RedirectEnabled = enabled 2880 } 2881 } 2882 2883 // InsecureSkipVerify skip verifying tls certificate file 2884 func InsecureSkipVerify(enabled bool) ClientOption { 2885 return func(client *Client) { 2886 client.Config.InsecureSkipVerify = enabled 2887 } 2888 } 2889 2890 // Region set region 2891 func Region(region string) ClientOption { 2892 return func(client *Client) { 2893 client.Config.Region = region 2894 } 2895 } 2896 2897 // CloudBoxId set cloudBox id 2898 func CloudBoxId(cloudBoxId string) ClientOption { 2899 return func(client *Client) { 2900 client.Config.CloudBoxId = cloudBoxId 2901 } 2902 } 2903 2904 // Product set product type 2905 func Product(product string) ClientOption { 2906 return func(client *Client) { 2907 client.Config.Product = product 2908 } 2909 } 2910 2911 // VerifyObjectStrict sets the flag of verifying object name strictly. 2912 func VerifyObjectStrict(enable bool) ClientOption { 2913 return func(client *Client) { 2914 client.Config.VerifyObjectStrict = enable 2915 } 2916 } 2917 2918 // Private 2919 func (client Client) do(method, bucketName string, params map[string]interface{}, 2920 headers map[string]string, data io.Reader, options ...Option) (*Response, error) { 2921 err := CheckBucketName(bucketName) 2922 if len(bucketName) > 0 && err != nil { 2923 return nil, err 2924 } 2925 2926 // option headers 2927 addHeaders := make(map[string]string) 2928 err = handleOptions(addHeaders, options) 2929 if err != nil { 2930 return nil, err 2931 } 2932 2933 // merge header 2934 if headers == nil { 2935 headers = make(map[string]string) 2936 } 2937 2938 for k, v := range addHeaders { 2939 if _, ok := headers[k]; !ok { 2940 headers[k] = v 2941 } 2942 } 2943 2944 resp, err := client.Conn.Do(method, bucketName, "", params, headers, data, 0, nil) 2945 2946 // get response header 2947 respHeader, _ := FindOption(options, responseHeader, nil) 2948 if respHeader != nil { 2949 pRespHeader := respHeader.(*http.Header) 2950 if resp != nil { 2951 *pRespHeader = resp.Headers 2952 } 2953 } 2954 2955 return resp, err 2956 }