SDK接口拓展配置(Go SDK)
功能说明
增加拓展配置extensionOptions(obs包中的私有类型),当前可以通过调用如下拓展配置项为对应请求配置额外的拓展请求头。
可用的拓展请求头
创建方式 |
说明 |
---|---|
WithTrafficLimitHeader(trafficLimit int64) |
配置在请求中携带单链接限速头域。目前仅支持 GetObject 方法。 取值范围:819200 ~ 838860800,单位:bit/s。 |
WithCallbackHeader(callback string) |
配置在请求中携带回调头域。目前仅支持流式上传、文件上传和断点续传接口。 |
常量名 |
原始值 |
说明 |
---|---|---|
BucketOwnerPayer |
BucketOwner |
表示桶拥有者支付所有费用。 |
RequesterPayer |
Requester |
表示请求者支付流量和请求次数费用,桶拥有者支付存储费用。 |
代码示例
携带单链接限速头域调用下载对象接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
// 引入依赖包 import ( "fmt" obs "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs" ) func main() { //推荐通过环境变量获取AKSK,这里也可以使用其他外部引入方式传入,如果使用硬编码可能会存在泄露风险。 //您可以登录访问管理控制台获取访问密钥AK/SK,获取方式请参见https://support.huaweicloud.com/usermanual-ca/ca_01_0003.html。 ak := os.Getenv("AccessKeyID") sk := os.Getenv("SecretAccessKey") // 【可选】如果使用临时AK/SK和SecurityToken访问OBS,同样建议您尽量避免使用硬编码,以降低信息泄露风险。您可以通过环境变量获取访问密钥AK/SK,也可以使用其他外部引入方式传入。 // securityToken := os.Getenv("SecurityToken") // endpoint填写Bucket对应的Endpoint, 这里以华北-北京四为例,其他地区请按实际情况填写。 endPoint := "https://obs.cn-north-4.myhuaweicloud.com" // 创建obsClient实例 // 如果使用临时AKSK和SecurityToken访问OBS,需要在创建实例时通过obs.WithSecurityToken方法指定securityToken值。 obsClient, err := obs.New(ak, sk, endPoint/*, obs.WithSecurityToken(securityToken)*/) input := &obs.GetObjectInput{} input.Bucket = "bucketname" input.Key = "objectname" var traffic int64 = 41943040 output, err := obsClient.GetObject(input, obs.WithTrafficLimitHeader(traffic)) if err == nil { defer output.Body.Close() fmt.Printf("StorageClass:%s, ETag:%s, ContentType:%s, ContentLength:%d, LastModified:%s\n", output.StorageClass, output.ETag, output.ContentType, output.ContentLength, output.LastModified) p := make([]byte, 1024) var readErr error var readCount int // 读取对象内容 for { readCount, readErr = output.Body.Read(p) if readCount > 0 { fmt.Printf("%s", p[:readCount]) } if readErr != nil { break } } } else if obsError, ok := err.(obs.ObsError); ok { fmt.Printf("Code:%s\n", obsError.Code) fmt.Printf("Message:%s\n", obsError.Message) } } |
携带callback回调头域调用上传对象接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
// 引入依赖包 import ( "fmt" "bytes" "encoding/base64" "encoding/json" "strings" obs "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs" ) func main() { //推荐通过环境变量获取AKSK,这里也可以使用其他外部引入方式传入,如果使用硬编码可能会存在泄露风险。 //您可以登录访问管理控制台获取访问密钥AK/SK,获取方式请参见https://support.huaweicloud.com/usermanual-ca/ca_01_0003.html。 ak := os.Getenv("AccessKeyID") sk := os.Getenv("SecretAccessKey") // 【可选】如果使用临时AK/SK和SecurityToken访问OBS,同样建议您尽量避免使用硬编码,以降低信息泄露风险。您可以通过环境变量获取访问密钥AK/SK,也可以使用其他外部引入方式传入。 // securityToken := os.Getenv("SecurityToken") // endpoint填写Bucket对应的Endpoint, 这里以华北-北京四为例,其他地区请按实际情况填写。 endPoint := "https://obs.cn-north-4.myhuaweicloud.com" // 创建obsClient实例 // 如果使用临时AKSK和SecurityToken访问OBS,需要在创建实例时通过obs.WithSecurityToken方法指定securityToken值。 obsClient, err := obs.New(ak, sk, endPoint/*, obs.WithSecurityToken(securityToken)*/) input := &obs.PutObjectInput{} input.Bucket = "bucketname" input.Key = "objectname" input.Body = strings.NewReader("Hello OBS") callbackMap := map[string]string{} callbackMap["callbackUrl"] = "http://example.com:80" // callbackMap["callbackHost"] = "example.com" callbackMap["callbackBody"] = "filename=${object}&size=${size}&mimeType=${mimeType}" // callbackMap["callbackBodyType"] = "application/x-www-form-urlencoded" callbackBuffer := bytes.NewBuffer([]byte{}) callbackEncoder := json.NewEncoder(callbackBuffer) callbackEncoder.SetEscapeHTML(false) err := callbackEncoder.Encode(callbackMap) if err != nil { fmt.Print(err) return } callbackVal := base64.StdEncoding.EncodeToString(callbackBuffer.Bytes()) output, err := obsClient.PutObject(input, obs.WithCallbackHeader(callbackVal)) if err == nil { // 必须调用该方法 defer output.CloseCallbackBody() fmt.Printf("RequestId:%s\n", output.RequestId) fmt.Printf("ETag:%s\n", output.ETag) p := make([]byte, 1024) var readErr error var readCount int // 读取回调内容 for { readCount, readErr = output.ReadCallbackBody(p) if readCount > 0 { fmt.Printf("%s", p[:readCount]) } if readErr != nil { break } } } else if obsError, ok := err.(obs.ObsError); ok { fmt.Printf("Code:%s\n", obsError.Code) fmt.Printf("Message:%s\n", obsError.Message) } } |