| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499 |
- // Package oss implements functions for access oss service.
- // It has two main struct Client and Bucket.
- package oss
- import (
- "bytes"
- "encoding/xml"
- "fmt"
- "io"
- "io/ioutil"
- "log"
- "net"
- "net/http"
- "strings"
- "time"
- )
- // 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).
- // Object related operations are done by Bucket class.
- // Users use oss.New to create Client instance.
- //
- type (
- // Client OSS client
- Client struct {
- Config *Config // OSS client configuration
- Conn *Conn // Send HTTP request
- HTTPClient *http.Client //http.Client to use - if nil will make its own
- }
- // ClientOption client option such as UseCname, Timeout, SecurityToken.
- ClientOption func(*Client)
- )
- // New creates a new client.
- //
- // endpoint the OSS datacenter endpoint such as http://oss-cn-hangzhou.aliyuncs.com .
- // accessKeyId access key Id.
- // accessKeySecret access key secret.
- //
- // Client creates the new client instance, the returned value is valid when error is nil.
- // error it's nil if no error, otherwise it's an error object.
- //
- func New(endpoint, accessKeyID, accessKeySecret string, options ...ClientOption) (*Client, error) {
- // Configuration
- config := getDefaultOssConfig()
- config.Endpoint = endpoint
- config.AccessKeyID = accessKeyID
- config.AccessKeySecret = accessKeySecret
- // URL parse
- url := &urlMaker{}
- err := url.Init(config.Endpoint, config.IsCname, config.IsUseProxy)
- if err != nil {
- return nil, err
- }
- // HTTP connect
- conn := &Conn{config: config, url: url}
- // OSS client
- client := &Client{
- Config: config,
- Conn: conn,
- }
- // Client options parse
- for _, option := range options {
- option(client)
- }
- if config.AuthVersion != AuthV1 && config.AuthVersion != AuthV2 {
- return nil, fmt.Errorf("Init client Error, invalid Auth version: %v", config.AuthVersion)
- }
- // Create HTTP connection
- err = conn.init(config, url, client.HTTPClient)
- return client, err
- }
- // Bucket gets the bucket instance.
- //
- // bucketName the bucket name.
- // Bucket the bucket object, when error is nil.
- //
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) Bucket(bucketName string) (*Bucket, error) {
- err := CheckBucketName(bucketName)
- if err != nil {
- return nil, err
- }
- return &Bucket{
- client,
- bucketName,
- }, nil
- }
- // CreateBucket creates a bucket.
- //
- // bucketName the bucket name, it's globably unique and immutable. The bucket name can only consist of lowercase letters, numbers and dash ('-').
- // It must start with lowercase letter or number and the length can only be between 3 and 255.
- // options options for creating the bucket, with optional ACL. The ACL could be ACLPrivate, ACLPublicRead, and ACLPublicReadWrite. By default it's ACLPrivate.
- // It could also be specified with StorageClass option, which supports StorageStandard, StorageIA(infrequent access), StorageArchive.
- //
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) CreateBucket(bucketName string, options ...Option) error {
- headers := make(map[string]string)
- handleOptions(headers, options)
- buffer := new(bytes.Buffer)
- var cbConfig createBucketConfiguration
- cbConfig.StorageClass = StorageStandard
- isStorageSet, valStroage, _ := IsOptionSet(options, storageClass)
- isRedundancySet, valRedundancy, _ := IsOptionSet(options, redundancyType)
- if isStorageSet {
- cbConfig.StorageClass = valStroage.(StorageClassType)
- }
- if isRedundancySet {
- cbConfig.DataRedundancyType = valRedundancy.(DataRedundancyType)
- }
- bs, err := xml.Marshal(cbConfig)
- if err != nil {
- return err
- }
- buffer.Write(bs)
- contentType := http.DetectContentType(buffer.Bytes())
- headers[HTTPHeaderContentType] = contentType
- params := map[string]interface{}{}
- resp, err := client.do("PUT", bucketName, params, headers, buffer, options...)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
- }
- // ListBuckets lists buckets of the current account under the given endpoint, with optional filters.
- //
- // options specifies the filters such as Prefix, Marker and MaxKeys. Prefix is the bucket name's prefix filter.
- // And marker makes sure the returned buckets' name are greater than it in lexicographic order.
- // Maxkeys limits the max keys to return, and by default it's 100 and up to 1000.
- // For the common usage scenario, please check out list_bucket.go in the sample.
- // ListBucketsResponse the response object if error is nil.
- //
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) ListBuckets(options ...Option) (ListBucketsResult, error) {
- var out ListBucketsResult
- params, err := GetRawParams(options)
- if err != nil {
- return out, err
- }
- resp, err := client.do("GET", "", params, nil, nil, options...)
- if err != nil {
- return out, err
- }
- defer resp.Body.Close()
- err = xmlUnmarshal(resp.Body, &out)
- return out, err
- }
- // IsBucketExist checks if the bucket exists
- //
- // bucketName the bucket name.
- //
- // bool true if it exists, and it's only valid when error is nil.
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) IsBucketExist(bucketName string) (bool, error) {
- listRes, err := client.ListBuckets(Prefix(bucketName), MaxKeys(1))
- if err != nil {
- return false, err
- }
- if len(listRes.Buckets) == 1 && listRes.Buckets[0].Name == bucketName {
- return true, nil
- }
- return false, nil
- }
- // DeleteBucket deletes the bucket. Only empty bucket can be deleted (no object and parts).
- //
- // bucketName the bucket name.
- //
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) DeleteBucket(bucketName string, options ...Option) error {
- params := map[string]interface{}{}
- resp, err := client.do("DELETE", bucketName, params, nil, nil, options...)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent})
- }
- // GetBucketLocation gets the bucket location.
- //
- // Checks out the following link for more information :
- // https://help.aliyun.com/document_detail/oss/user_guide/oss_concept/endpoint.html
- //
- // bucketName the bucket name
- //
- // string bucket's datacenter location
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) GetBucketLocation(bucketName string) (string, error) {
- params := map[string]interface{}{}
- params["location"] = nil
- resp, err := client.do("GET", bucketName, params, nil, nil)
- if err != nil {
- return "", err
- }
- defer resp.Body.Close()
- var LocationConstraint string
- err = xmlUnmarshal(resp.Body, &LocationConstraint)
- return LocationConstraint, err
- }
- // SetBucketACL sets bucket's ACL.
- //
- // bucketName the bucket name
- // bucketAcl the bucket ACL: ACLPrivate, ACLPublicRead and ACLPublicReadWrite.
- //
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) SetBucketACL(bucketName string, bucketACL ACLType) error {
- headers := map[string]string{HTTPHeaderOssACL: string(bucketACL)}
- params := map[string]interface{}{}
- params["acl"] = nil
- resp, err := client.do("PUT", bucketName, params, headers, nil)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
- }
- // GetBucketACL gets the bucket ACL.
- //
- // bucketName the bucket name.
- //
- // GetBucketAclResponse the result object, and it's only valid when error is nil.
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) GetBucketACL(bucketName string) (GetBucketACLResult, error) {
- var out GetBucketACLResult
- params := map[string]interface{}{}
- params["acl"] = nil
- resp, err := client.do("GET", bucketName, params, nil, nil)
- if err != nil {
- return out, err
- }
- defer resp.Body.Close()
- err = xmlUnmarshal(resp.Body, &out)
- return out, err
- }
- // SetBucketLifecycle sets the bucket's lifecycle.
- //
- // For more information, checks out following link:
- // https://help.aliyun.com/document_detail/oss/user_guide/manage_object/object_lifecycle.html
- //
- // bucketName the bucket name.
- // rules the lifecycle rules. There're two kind of rules: absolute time expiration and relative time expiration in days and day/month/year respectively.
- // Check out sample/bucket_lifecycle.go for more details.
- //
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) SetBucketLifecycle(bucketName string, rules []LifecycleRule) error {
- err := verifyLifecycleRules(rules)
- if err != nil {
- return err
- }
- lifecycleCfg := LifecycleConfiguration{Rules: rules}
- bs, err := xml.Marshal(lifecycleCfg)
- if err != nil {
- return err
- }
- buffer := new(bytes.Buffer)
- buffer.Write(bs)
- contentType := http.DetectContentType(buffer.Bytes())
- headers := map[string]string{}
- headers[HTTPHeaderContentType] = contentType
- params := map[string]interface{}{}
- params["lifecycle"] = nil
- resp, err := client.do("PUT", bucketName, params, headers, buffer)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
- }
- // DeleteBucketLifecycle deletes the bucket's lifecycle.
- //
- //
- // bucketName the bucket name.
- //
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) DeleteBucketLifecycle(bucketName string) error {
- params := map[string]interface{}{}
- params["lifecycle"] = nil
- resp, err := client.do("DELETE", bucketName, params, nil, nil)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent})
- }
- // GetBucketLifecycle gets the bucket's lifecycle settings.
- //
- // bucketName the bucket name.
- //
- // GetBucketLifecycleResponse the result object upon successful request. It's only valid when error is nil.
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) GetBucketLifecycle(bucketName string) (GetBucketLifecycleResult, error) {
- var out GetBucketLifecycleResult
- params := map[string]interface{}{}
- params["lifecycle"] = nil
- resp, err := client.do("GET", bucketName, params, nil, nil)
- if err != nil {
- return out, err
- }
- defer resp.Body.Close()
- err = xmlUnmarshal(resp.Body, &out)
- return out, err
- }
- // SetBucketReferer sets the bucket's referer whitelist and the flag if allowing empty referrer.
- //
- // 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
- // the allowing empty referrer flag. Note that this applies to requests from webbrowser only.
- // For example, for a bucket os-example and its referrer http://www.aliyun.com, all requests from this URL could access the bucket.
- // For more information, please check out this link :
- // https://help.aliyun.com/document_detail/oss/user_guide/security_management/referer.html
- //
- // bucketName the bucket name.
- // referers the referrer white list. A bucket could have a referrer list and each referrer supports one '*' and multiple '?' as wildcards.
- // The sample could be found in sample/bucket_referer.go
- // allowEmptyReferer the flag of allowing empty referrer. By default it's true.
- //
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) SetBucketReferer(bucketName string, referers []string, allowEmptyReferer bool) error {
- rxml := RefererXML{}
- rxml.AllowEmptyReferer = allowEmptyReferer
- if referers == nil {
- rxml.RefererList = append(rxml.RefererList, "")
- } else {
- for _, referer := range referers {
- rxml.RefererList = append(rxml.RefererList, referer)
- }
- }
- bs, err := xml.Marshal(rxml)
- if err != nil {
- return err
- }
- buffer := new(bytes.Buffer)
- buffer.Write(bs)
- contentType := http.DetectContentType(buffer.Bytes())
- headers := map[string]string{}
- headers[HTTPHeaderContentType] = contentType
- params := map[string]interface{}{}
- params["referer"] = nil
- resp, err := client.do("PUT", bucketName, params, headers, buffer)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
- }
- // GetBucketReferer gets the bucket's referrer white list.
- //
- // bucketName the bucket name.
- //
- // GetBucketRefererResponse the result object upon successful request. It's only valid when error is nil.
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) GetBucketReferer(bucketName string) (GetBucketRefererResult, error) {
- var out GetBucketRefererResult
- params := map[string]interface{}{}
- params["referer"] = nil
- resp, err := client.do("GET", bucketName, params, nil, nil)
- if err != nil {
- return out, err
- }
- defer resp.Body.Close()
- err = xmlUnmarshal(resp.Body, &out)
- return out, err
- }
- // SetBucketLogging sets the bucket logging settings.
- //
- // OSS could automatically store the access log. Only the bucket owner could enable the logging.
- // Once enabled, OSS would save all the access log into hourly log files in a specified bucket.
- // For more information, please check out https://help.aliyun.com/document_detail/oss/user_guide/security_management/logging.html
- //
- // bucketName bucket name to enable the log.
- // targetBucket the target bucket name to store the log files.
- // targetPrefix the log files' prefix.
- //
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) SetBucketLogging(bucketName, targetBucket, targetPrefix string,
- isEnable bool) error {
- var err error
- var bs []byte
- if isEnable {
- lxml := LoggingXML{}
- lxml.LoggingEnabled.TargetBucket = targetBucket
- lxml.LoggingEnabled.TargetPrefix = targetPrefix
- bs, err = xml.Marshal(lxml)
- } else {
- lxml := loggingXMLEmpty{}
- bs, err = xml.Marshal(lxml)
- }
- if err != nil {
- return err
- }
- buffer := new(bytes.Buffer)
- buffer.Write(bs)
- contentType := http.DetectContentType(buffer.Bytes())
- headers := map[string]string{}
- headers[HTTPHeaderContentType] = contentType
- params := map[string]interface{}{}
- params["logging"] = nil
- resp, err := client.do("PUT", bucketName, params, headers, buffer)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
- }
- // DeleteBucketLogging deletes the logging configuration to disable the logging on the bucket.
- //
- // bucketName the bucket name to disable the logging.
- //
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) DeleteBucketLogging(bucketName string) error {
- params := map[string]interface{}{}
- params["logging"] = nil
- resp, err := client.do("DELETE", bucketName, params, nil, nil)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent})
- }
- // GetBucketLogging gets the bucket's logging settings
- //
- // bucketName the bucket name
- // GetBucketLoggingResponse the result object upon successful request. It's only valid when error is nil.
- //
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) GetBucketLogging(bucketName string) (GetBucketLoggingResult, error) {
- var out GetBucketLoggingResult
- params := map[string]interface{}{}
- params["logging"] = nil
- resp, err := client.do("GET", bucketName, params, nil, nil)
- if err != nil {
- return out, err
- }
- defer resp.Body.Close()
- err = xmlUnmarshal(resp.Body, &out)
- return out, err
- }
- // SetBucketWebsite sets the bucket's static website's index and error page.
- //
- // 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.
- // For more information, please check out: https://help.aliyun.com/document_detail/oss/user_guide/static_host_website.html
- //
- // bucketName the bucket name to enable static web site.
- // indexDocument index page.
- // errorDocument error page.
- //
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) SetBucketWebsite(bucketName, indexDocument, errorDocument string) error {
- wxml := WebsiteXML{}
- wxml.IndexDocument.Suffix = indexDocument
- wxml.ErrorDocument.Key = errorDocument
- bs, err := xml.Marshal(wxml)
- if err != nil {
- return err
- }
- buffer := new(bytes.Buffer)
- buffer.Write(bs)
- contentType := http.DetectContentType(buffer.Bytes())
- headers := make(map[string]string)
- headers[HTTPHeaderContentType] = contentType
- params := map[string]interface{}{}
- params["website"] = nil
- resp, err := client.do("PUT", bucketName, params, headers, buffer)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
- }
- // SetBucketWebsiteDetail sets the bucket's static website's detail
- //
- // 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.
- // For more information, please check out: https://help.aliyun.com/document_detail/oss/user_guide/static_host_website.html
- //
- // bucketName the bucket name to enable static web site.
- //
- // wxml the website's detail
- //
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) SetBucketWebsiteDetail(bucketName string, wxml WebsiteXML, options ...Option) error {
- bs, err := xml.Marshal(wxml)
- if err != nil {
- return err
- }
- buffer := new(bytes.Buffer)
- buffer.Write(bs)
- contentType := http.DetectContentType(buffer.Bytes())
- headers := make(map[string]string)
- headers[HTTPHeaderContentType] = contentType
- params := map[string]interface{}{}
- params["website"] = nil
- resp, err := client.do("PUT", bucketName, params, headers, buffer, options...)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
- }
- // SetBucketWebsiteXml sets the bucket's static website's rule
- //
- // 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.
- // For more information, please check out: https://help.aliyun.com/document_detail/oss/user_guide/static_host_website.html
- //
- // bucketName the bucket name to enable static web site.
- //
- // wxml the website's detail
- //
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) SetBucketWebsiteXml(bucketName string, webXml string, options ...Option) error {
- buffer := new(bytes.Buffer)
- buffer.Write([]byte(webXml))
- contentType := http.DetectContentType(buffer.Bytes())
- headers := make(map[string]string)
- headers[HTTPHeaderContentType] = contentType
- params := map[string]interface{}{}
- params["website"] = nil
- resp, err := client.do("PUT", bucketName, params, headers, buffer, options...)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
- }
- // DeleteBucketWebsite deletes the bucket's static web site settings.
- //
- // bucketName the bucket name.
- //
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) DeleteBucketWebsite(bucketName string) error {
- params := map[string]interface{}{}
- params["website"] = nil
- resp, err := client.do("DELETE", bucketName, params, nil, nil)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent})
- }
- // GetBucketWebsite gets the bucket's default page (index page) and the error page.
- //
- // bucketName the bucket name
- //
- // GetBucketWebsiteResponse the result object upon successful request. It's only valid when error is nil.
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) GetBucketWebsite(bucketName string) (GetBucketWebsiteResult, error) {
- var out GetBucketWebsiteResult
- params := map[string]interface{}{}
- params["website"] = nil
- resp, err := client.do("GET", bucketName, params, nil, nil)
- if err != nil {
- return out, err
- }
- defer resp.Body.Close()
- err = xmlUnmarshal(resp.Body, &out)
- return out, err
- }
- // SetBucketCORS sets the bucket's CORS rules
- //
- // For more information, please check out https://help.aliyun.com/document_detail/oss/user_guide/security_management/cors.html
- //
- // bucketName the bucket name
- // corsRules the CORS rules to set. The related sample code is in sample/bucket_cors.go.
- //
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) SetBucketCORS(bucketName string, corsRules []CORSRule) error {
- corsxml := CORSXML{}
- for _, v := range corsRules {
- cr := CORSRule{}
- cr.AllowedMethod = v.AllowedMethod
- cr.AllowedOrigin = v.AllowedOrigin
- cr.AllowedHeader = v.AllowedHeader
- cr.ExposeHeader = v.ExposeHeader
- cr.MaxAgeSeconds = v.MaxAgeSeconds
- corsxml.CORSRules = append(corsxml.CORSRules, cr)
- }
- bs, err := xml.Marshal(corsxml)
- if err != nil {
- return err
- }
- buffer := new(bytes.Buffer)
- buffer.Write(bs)
- contentType := http.DetectContentType(buffer.Bytes())
- headers := map[string]string{}
- headers[HTTPHeaderContentType] = contentType
- params := map[string]interface{}{}
- params["cors"] = nil
- resp, err := client.do("PUT", bucketName, params, headers, buffer)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
- }
- // DeleteBucketCORS deletes the bucket's static website settings.
- //
- // bucketName the bucket name.
- //
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) DeleteBucketCORS(bucketName string) error {
- params := map[string]interface{}{}
- params["cors"] = nil
- resp, err := client.do("DELETE", bucketName, params, nil, nil)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent})
- }
- // GetBucketCORS gets the bucket's CORS settings.
- //
- // bucketName the bucket name.
- // GetBucketCORSResult the result object upon successful request. It's only valid when error is nil.
- //
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) GetBucketCORS(bucketName string) (GetBucketCORSResult, error) {
- var out GetBucketCORSResult
- params := map[string]interface{}{}
- params["cors"] = nil
- resp, err := client.do("GET", bucketName, params, nil, nil)
- if err != nil {
- return out, err
- }
- defer resp.Body.Close()
- err = xmlUnmarshal(resp.Body, &out)
- return out, err
- }
- // GetBucketInfo gets the bucket information.
- //
- // bucketName the bucket name.
- // GetBucketInfoResult the result object upon successful request. It's only valid when error is nil.
- //
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) GetBucketInfo(bucketName string, options ...Option) (GetBucketInfoResult, error) {
- var out GetBucketInfoResult
- params := map[string]interface{}{}
- params["bucketInfo"] = nil
- resp, err := client.do("GET", bucketName, params, nil, nil, options...)
- if err != nil {
- return out, err
- }
- defer resp.Body.Close()
- err = xmlUnmarshal(resp.Body, &out)
- // convert None to ""
- if err == nil {
- if out.BucketInfo.SseRule.KMSMasterKeyID == "None" {
- out.BucketInfo.SseRule.KMSMasterKeyID = ""
- }
- if out.BucketInfo.SseRule.SSEAlgorithm == "None" {
- out.BucketInfo.SseRule.SSEAlgorithm = ""
- }
- }
- return out, err
- }
- // SetBucketVersioning set bucket versioning:Enabled、Suspended
- // bucketName the bucket name.
- // error it's nil if no error, otherwise it's an error object.
- func (client Client) SetBucketVersioning(bucketName string, versioningConfig VersioningConfig, options ...Option) error {
- var err error
- var bs []byte
- bs, err = xml.Marshal(versioningConfig)
- if err != nil {
- return err
- }
- buffer := new(bytes.Buffer)
- buffer.Write(bs)
- contentType := http.DetectContentType(buffer.Bytes())
- headers := map[string]string{}
- headers[HTTPHeaderContentType] = contentType
- params := map[string]interface{}{}
- params["versioning"] = nil
- resp, err := client.do("PUT", bucketName, params, headers, buffer, options...)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
- }
- // GetBucketVersioning get bucket versioning status:Enabled、Suspended
- // bucketName the bucket name.
- // error it's nil if no error, otherwise it's an error object.
- func (client Client) GetBucketVersioning(bucketName string, options ...Option) (GetBucketVersioningResult, error) {
- var out GetBucketVersioningResult
- params := map[string]interface{}{}
- params["versioning"] = nil
- resp, err := client.do("GET", bucketName, params, nil, nil, options...)
- if err != nil {
- return out, err
- }
- defer resp.Body.Close()
- err = xmlUnmarshal(resp.Body, &out)
- return out, err
- }
- // SetBucketEncryption set bucket encryption config
- // bucketName the bucket name.
- // error it's nil if no error, otherwise it's an error object.
- func (client Client) SetBucketEncryption(bucketName string, encryptionRule ServerEncryptionRule, options ...Option) error {
- var err error
- var bs []byte
- bs, err = xml.Marshal(encryptionRule)
- if err != nil {
- return err
- }
- buffer := new(bytes.Buffer)
- buffer.Write(bs)
- contentType := http.DetectContentType(buffer.Bytes())
- headers := map[string]string{}
- headers[HTTPHeaderContentType] = contentType
- params := map[string]interface{}{}
- params["encryption"] = nil
- resp, err := client.do("PUT", bucketName, params, headers, buffer, options...)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
- }
- // GetBucketEncryption get bucket encryption
- // bucketName the bucket name.
- // error it's nil if no error, otherwise it's an error object.
- func (client Client) GetBucketEncryption(bucketName string, options ...Option) (GetBucketEncryptionResult, error) {
- var out GetBucketEncryptionResult
- params := map[string]interface{}{}
- params["encryption"] = nil
- resp, err := client.do("GET", bucketName, params, nil, nil, options...)
- if err != nil {
- return out, err
- }
- defer resp.Body.Close()
- err = xmlUnmarshal(resp.Body, &out)
- return out, err
- }
- // DeleteBucketEncryption delete bucket encryption config
- // bucketName the bucket name.
- // error it's nil if no error, otherwise it's an error bucket
- func (client Client) DeleteBucketEncryption(bucketName string, options ...Option) error {
- params := map[string]interface{}{}
- params["encryption"] = nil
- resp, err := client.do("DELETE", bucketName, params, nil, nil, options...)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent})
- }
- //
- // SetBucketTagging add tagging to bucket
- // bucketName name of bucket
- // tagging tagging to be added
- // error nil if success, otherwise error
- func (client Client) SetBucketTagging(bucketName string, tagging Tagging, options ...Option) error {
- var err error
- var bs []byte
- bs, err = xml.Marshal(tagging)
- if err != nil {
- return err
- }
- buffer := new(bytes.Buffer)
- buffer.Write(bs)
- contentType := http.DetectContentType(buffer.Bytes())
- headers := map[string]string{}
- headers[HTTPHeaderContentType] = contentType
- params := map[string]interface{}{}
- params["tagging"] = nil
- resp, err := client.do("PUT", bucketName, params, headers, buffer, options...)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
- }
- // GetBucketTagging get tagging of the bucket
- // bucketName name of bucket
- // error nil if success, otherwise error
- func (client Client) GetBucketTagging(bucketName string, options ...Option) (GetBucketTaggingResult, error) {
- var out GetBucketTaggingResult
- params := map[string]interface{}{}
- params["tagging"] = nil
- resp, err := client.do("GET", bucketName, params, nil, nil, options...)
- if err != nil {
- return out, err
- }
- defer resp.Body.Close()
- err = xmlUnmarshal(resp.Body, &out)
- return out, err
- }
- //
- // DeleteBucketTagging delete bucket tagging
- // bucketName name of bucket
- // error nil if success, otherwise error
- //
- func (client Client) DeleteBucketTagging(bucketName string, options ...Option) error {
- params := map[string]interface{}{}
- params["tagging"] = nil
- resp, err := client.do("DELETE", bucketName, params, nil, nil, options...)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent})
- }
- // GetBucketStat get bucket stat
- // bucketName the bucket name.
- // error it's nil if no error, otherwise it's an error object.
- func (client Client) GetBucketStat(bucketName string) (GetBucketStatResult, error) {
- var out GetBucketStatResult
- params := map[string]interface{}{}
- params["stat"] = nil
- resp, err := client.do("GET", bucketName, params, nil, nil)
- if err != nil {
- return out, err
- }
- defer resp.Body.Close()
- err = xmlUnmarshal(resp.Body, &out)
- return out, err
- }
- // GetBucketPolicy API operation for Object Storage Service.
- //
- // Get the policy from the bucket.
- //
- // bucketName the bucket name.
- //
- // string return the bucket's policy, and it's only valid when error is nil.
- //
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) GetBucketPolicy(bucketName string, options ...Option) (string, error) {
- params := map[string]interface{}{}
- params["policy"] = nil
- resp, err := client.do("GET", bucketName, params, nil, nil, options...)
- if err != nil {
- return "", err
- }
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body)
- out := string(body)
- return out, err
- }
- // SetBucketPolicy API operation for Object Storage Service.
- //
- // Set the policy from the bucket.
- //
- // bucketName the bucket name.
- //
- // policy the bucket policy.
- //
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) SetBucketPolicy(bucketName string, policy string, options ...Option) error {
- params := map[string]interface{}{}
- params["policy"] = nil
- buffer := strings.NewReader(policy)
- resp, err := client.do("PUT", bucketName, params, nil, buffer, options...)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
- }
- // DeleteBucketPolicy API operation for Object Storage Service.
- //
- // Deletes the policy from the bucket.
- //
- // bucketName the bucket name.
- //
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) DeleteBucketPolicy(bucketName string, options ...Option) error {
- params := map[string]interface{}{}
- params["policy"] = nil
- resp, err := client.do("DELETE", bucketName, params, nil, nil, options...)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent})
- }
- // SetBucketRequestPayment API operation for Object Storage Service.
- //
- // Set the requestPayment of bucket
- //
- // bucketName the bucket name.
- //
- // paymentConfig the payment configuration
- //
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) SetBucketRequestPayment(bucketName string, paymentConfig RequestPaymentConfiguration, options ...Option) error {
- params := map[string]interface{}{}
- params["requestPayment"] = nil
- var bs []byte
- bs, err := xml.Marshal(paymentConfig)
- if err != nil {
- return err
- }
- buffer := new(bytes.Buffer)
- buffer.Write(bs)
- contentType := http.DetectContentType(buffer.Bytes())
- headers := map[string]string{}
- headers[HTTPHeaderContentType] = contentType
- resp, err := client.do("PUT", bucketName, params, headers, buffer, options...)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
- }
- // GetBucketRequestPayment API operation for Object Storage Service.
- //
- // Get bucket requestPayment
- //
- // bucketName the bucket name.
- //
- // RequestPaymentConfiguration the payment configuration
- //
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) GetBucketRequestPayment(bucketName string, options ...Option) (RequestPaymentConfiguration, error) {
- var out RequestPaymentConfiguration
- params := map[string]interface{}{}
- params["requestPayment"] = nil
- resp, err := client.do("GET", bucketName, params, nil, nil, options...)
- if err != nil {
- return out, err
- }
- defer resp.Body.Close()
- err = xmlUnmarshal(resp.Body, &out)
- return out, err
- }
- // GetUserQoSInfo API operation for Object Storage Service.
- //
- // Get user qos.
- //
- // UserQoSConfiguration the User Qos and range Information.
- //
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) GetUserQoSInfo(options ...Option) (UserQoSConfiguration, error) {
- var out UserQoSConfiguration
- params := map[string]interface{}{}
- params["qosInfo"] = nil
- resp, err := client.do("GET", "", params, nil, nil, options...)
- if err != nil {
- return out, err
- }
- defer resp.Body.Close()
- err = xmlUnmarshal(resp.Body, &out)
- return out, err
- }
- // SetBucketQoSInfo API operation for Object Storage Service.
- //
- // Set Bucket Qos information.
- //
- // bucketName tht bucket name.
- //
- // qosConf the qos configuration.
- //
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) SetBucketQoSInfo(bucketName string, qosConf BucketQoSConfiguration, options ...Option) error {
- params := map[string]interface{}{}
- params["qosInfo"] = nil
- var bs []byte
- bs, err := xml.Marshal(qosConf)
- if err != nil {
- return err
- }
- buffer := new(bytes.Buffer)
- buffer.Write(bs)
- contentTpye := http.DetectContentType(buffer.Bytes())
- headers := map[string]string{}
- headers[HTTPHeaderContentType] = contentTpye
- resp, err := client.do("PUT", bucketName, params, headers, buffer, options...)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
- }
- // GetBucketQosInfo API operation for Object Storage Service.
- //
- // Get Bucket Qos information.
- //
- // bucketName tht bucket name.
- //
- // BucketQoSConfiguration the return qos configuration.
- //
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) GetBucketQosInfo(bucketName string, options ...Option) (BucketQoSConfiguration, error) {
- var out BucketQoSConfiguration
- params := map[string]interface{}{}
- params["qosInfo"] = nil
- resp, err := client.do("GET", bucketName, params, nil, nil, options...)
- if err != nil {
- return out, err
- }
- defer resp.Body.Close()
- err = xmlUnmarshal(resp.Body, &out)
- return out, err
- }
- // DeleteBucketQosInfo API operation for Object Storage Service.
- //
- // Delete Bucket QoS information.
- //
- // bucketName tht bucket name.
- //
- // error it's nil if no error, otherwise it's an error object.
- //
- func (client Client) DeleteBucketQosInfo(bucketName string, options ...Option) error {
- params := map[string]interface{}{}
- params["qosInfo"] = nil
- resp, err := client.do("DELETE", bucketName, params, nil, nil, options...)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent})
- }
- // LimitUploadSpeed set upload bandwidth limit speed,default is 0,unlimited
- // upSpeed KB/s, 0 is unlimited,default is 0
- // error it's nil if success, otherwise failure
- func (client Client) LimitUploadSpeed(upSpeed int) error {
- if client.Config == nil {
- return fmt.Errorf("client config is nil")
- }
- return client.Config.LimitUploadSpeed(upSpeed)
- }
- // UseCname sets the flag of using CName. By default it's false.
- //
- // isUseCname true: the endpoint has the CName, false: the endpoint does not have cname. Default is false.
- //
- func UseCname(isUseCname bool) ClientOption {
- return func(client *Client) {
- client.Config.IsCname = isUseCname
- client.Conn.url.Init(client.Config.Endpoint, client.Config.IsCname, client.Config.IsUseProxy)
- }
- }
- // Timeout sets the HTTP timeout in seconds.
- //
- // connectTimeoutSec HTTP timeout in seconds. Default is 10 seconds. 0 means infinite (not recommended)
- // readWriteTimeout HTTP read or write's timeout in seconds. Default is 20 seconds. 0 means infinite.
- //
- func Timeout(connectTimeoutSec, readWriteTimeout int64) ClientOption {
- return func(client *Client) {
- client.Config.HTTPTimeout.ConnectTimeout =
- time.Second * time.Duration(connectTimeoutSec)
- client.Config.HTTPTimeout.ReadWriteTimeout =
- time.Second * time.Duration(readWriteTimeout)
- client.Config.HTTPTimeout.HeaderTimeout =
- time.Second * time.Duration(readWriteTimeout)
- client.Config.HTTPTimeout.IdleConnTimeout =
- time.Second * time.Duration(readWriteTimeout)
- client.Config.HTTPTimeout.LongTimeout =
- time.Second * time.Duration(readWriteTimeout*10)
- }
- }
- // SetBucketInventory API operation for Object Storage Service
- //
- // Set the Bucket inventory.
- //
- // bucketName tht bucket name.
- //
- // inventoryConfig the inventory configuration.
- //
- // error it's nil if no error, otherwise it's an error.
- //
- func (client Client) SetBucketInventory(bucketName string, inventoryConfig InventoryConfiguration, options ...Option) error {
- params := map[string]interface{}{}
- params["inventoryId"] = inventoryConfig.Id
- params["inventory"] = nil
- var bs []byte
- bs, err := xml.Marshal(inventoryConfig)
- if err != nil {
- return err
- }
- buffer := new(bytes.Buffer)
- buffer.Write(bs)
- contentType := http.DetectContentType(buffer.Bytes())
- headers := make(map[string]string)
- headers[HTTPHeaderContentType] = contentType
- resp, err := client.do("PUT", bucketName, params, headers, buffer, options...)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
- }
- // GetBucketInventory API operation for Object Storage Service
- //
- // Get the Bucket inventory.
- //
- // bucketName tht bucket name.
- //
- // strInventoryId the inventory id.
- //
- // InventoryConfiguration the inventory configuration.
- //
- // error it's nil if no error, otherwise it's an error.
- //
- func (client Client) GetBucketInventory(bucketName string, strInventoryId string, options ...Option) (InventoryConfiguration, error) {
- var out InventoryConfiguration
- params := map[string]interface{}{}
- params["inventory"] = nil
- params["inventoryId"] = strInventoryId
- resp, err := client.do("GET", bucketName, params, nil, nil, options...)
- if err != nil {
- return out, err
- }
- defer resp.Body.Close()
- err = xmlUnmarshal(resp.Body, &out)
- return out, err
- }
- // ListBucketInventory API operation for Object Storage Service
- //
- // List the Bucket inventory.
- //
- // bucketName tht bucket name.
- //
- // continuationToken the users token.
- //
- // ListInventoryConfigurationsResult list all inventory configuration by .
- //
- // error it's nil if no error, otherwise it's an error.
- //
- func (client Client) ListBucketInventory(bucketName, continuationToken string, options ...Option) (ListInventoryConfigurationsResult, error) {
- var out ListInventoryConfigurationsResult
- params := map[string]interface{}{}
- params["inventory"] = nil
- if continuationToken == "" {
- params["continuation-token"] = nil
- } else {
- params["continuation-token"] = continuationToken
- }
- resp, err := client.do("GET", bucketName, params, nil, nil, options...)
- if err != nil {
- return out, err
- }
- defer resp.Body.Close()
- err = xmlUnmarshal(resp.Body, &out)
- return out, err
- }
- // DeleteBucketInventory API operation for Object Storage Service.
- //
- // Delete Bucket inventory information.
- //
- // bucketName tht bucket name.
- //
- // strInventoryId the inventory id.
- //
- // error it's nil if no error, otherwise it's an error.
- //
- func (client Client) DeleteBucketInventory(bucketName, strInventoryId string, options ...Option) error {
- params := map[string]interface{}{}
- params["inventory"] = nil
- params["inventoryId"] = strInventoryId
- resp, err := client.do("DELETE", bucketName, params, nil, nil, options...)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent})
- }
- // SecurityToken sets the temporary user's SecurityToken.
- //
- // token STS token
- //
- func SecurityToken(token string) ClientOption {
- return func(client *Client) {
- client.Config.SecurityToken = strings.TrimSpace(token)
- }
- }
- // EnableMD5 enables MD5 validation.
- //
- // isEnableMD5 true: enable MD5 validation; false: disable MD5 validation.
- //
- func EnableMD5(isEnableMD5 bool) ClientOption {
- return func(client *Client) {
- client.Config.IsEnableMD5 = isEnableMD5
- }
- }
- // MD5ThresholdCalcInMemory sets the memory usage threshold for computing the MD5, default is 16MB.
- //
- // threshold the memory threshold in bytes. When the uploaded content is more than 16MB, the temp file is used for computing the MD5.
- //
- func MD5ThresholdCalcInMemory(threshold int64) ClientOption {
- return func(client *Client) {
- client.Config.MD5Threshold = threshold
- }
- }
- // EnableCRC enables the CRC checksum. Default is true.
- //
- // isEnableCRC true: enable CRC checksum; false: disable the CRC checksum.
- //
- func EnableCRC(isEnableCRC bool) ClientOption {
- return func(client *Client) {
- client.Config.IsEnableCRC = isEnableCRC
- }
- }
- // UserAgent specifies UserAgent. The default is aliyun-sdk-go/1.2.0 (windows/-/amd64;go1.5.2).
- //
- // userAgent the user agent string.
- //
- func UserAgent(userAgent string) ClientOption {
- return func(client *Client) {
- client.Config.UserAgent = userAgent
- client.Config.UserSetUa = true
- }
- }
- // Proxy sets the proxy (optional). The default is not using proxy.
- //
- // proxyHost the proxy host in the format "host:port". For example, proxy.com:80 .
- //
- func Proxy(proxyHost string) ClientOption {
- return func(client *Client) {
- client.Config.IsUseProxy = true
- client.Config.ProxyHost = proxyHost
- client.Conn.url.Init(client.Config.Endpoint, client.Config.IsCname, client.Config.IsUseProxy)
- }
- }
- // AuthProxy sets the proxy information with user name and password.
- //
- // proxyHost the proxy host in the format "host:port". For example, proxy.com:80 .
- // proxyUser the proxy user name.
- // proxyPassword the proxy password.
- //
- func AuthProxy(proxyHost, proxyUser, proxyPassword string) ClientOption {
- return func(client *Client) {
- client.Config.IsUseProxy = true
- client.Config.ProxyHost = proxyHost
- client.Config.IsAuthProxy = true
- client.Config.ProxyUser = proxyUser
- client.Config.ProxyPassword = proxyPassword
- client.Conn.url.Init(client.Config.Endpoint, client.Config.IsCname, client.Config.IsUseProxy)
- }
- }
- //
- // HTTPClient sets the http.Client in use to the one passed in
- //
- func HTTPClient(HTTPClient *http.Client) ClientOption {
- return func(client *Client) {
- client.HTTPClient = HTTPClient
- }
- }
- //
- // SetLogLevel sets the oss sdk log level
- //
- func SetLogLevel(LogLevel int) ClientOption {
- return func(client *Client) {
- client.Config.LogLevel = LogLevel
- }
- }
- //
- // SetLogger sets the oss sdk logger
- //
- func SetLogger(Logger *log.Logger) ClientOption {
- return func(client *Client) {
- client.Config.Logger = Logger
- }
- }
- // SetCredentialsProvider sets funciton for get the user's ak
- func SetCredentialsProvider(provider CredentialsProvider) ClientOption {
- return func(client *Client) {
- client.Config.CredentialsProvider = provider
- }
- }
- // SetLocalAddr sets funciton for local addr
- func SetLocalAddr(localAddr net.Addr) ClientOption {
- return func(client *Client) {
- client.Config.LocalAddr = localAddr
- }
- }
- // AuthVersion sets auth version: v1 or v2 signature which oss_server needed
- func AuthVersion(authVersion AuthVersionType) ClientOption {
- return func(client *Client) {
- client.Config.AuthVersion = authVersion
- }
- }
- // AdditionalHeaders sets special http headers needed to be signed
- func AdditionalHeaders(headers []string) ClientOption {
- return func(client *Client) {
- client.Config.AdditionalHeaders = headers
- }
- }
- // Private
- func (client Client) do(method, bucketName string, params map[string]interface{},
- headers map[string]string, data io.Reader, options ...Option) (*Response, error) {
- err := CheckBucketName(bucketName)
- if len(bucketName) > 0 && err != nil {
- return nil, err
- }
- resp, err := client.Conn.Do(method, bucketName, "", params, headers, data, 0, nil)
- // get response header
- respHeader, _ := FindOption(options, responseHeader, nil)
- if respHeader != nil {
- pRespHeader := respHeader.(*http.Header)
- *pRespHeader = resp.Headers
- }
- return resp, err
- }
|