云服务器内容精选

  • 返回结果说明 表5 返回结果 参数名称 参数类型 描述 output *CreateSignedUrlOutput 参数解释: 接口返回信息,详情参见CreateSignedUrlOutput。 err error 参数解释: 接口返回错误信息。 表6 CreateSignedUrlOutput 参数名称 参数类型 描述 SignedUrl string 参数解释: 带授权信息的URL。 默认取值: 无 ActualSignedRequestHeaders http.Header 参数解释: 通过带授权信息的URL发起请求时实际应携带的头域。 默认取值: 无
  • 请求参数说明 表1 请求参数列表 参数名称 参数类型 是否必选 描述 input *CreateSignedUrlInput 必选 参数解释: 生成带授权信息的URL请求参数,详情参见CreateSignedUrlInput。 表2 CreateSignedUrlInput 参数名称 参数类型 是否必选 描述 Method HttpMethodType 必选 参数解释: HTTP方法类型,详情参见HttpMethodType。 Bucket string 可选 参数解释: 桶名。 约束限制: 桶的名字需全局唯一,不能与已有的任何桶名称重复,包括其他用户创建的桶。 桶命名规则如下: 3~63个字符,数字或字母开头,支持小写字母、数字、“-”、“.”。 禁止使用IP地址。 禁止以“-”或“.”开头及结尾。 禁止两个“.”相邻(如:“my..bucket”)。 禁止“.”和“-”相邻(如:“my-.bucket”和“my.-bucket”)。 同一用户在同一个区域多次创建同名桶不会报错,创建的桶属性以第一次请求为准。 默认取值: 无 Key string 可选 参数解释: 对象名。对象名是对象在存储桶中的唯一标识。对象名是对象在桶中的完整路径,路径中不包含桶名。 例如,您对象的访问地址为examplebucket.obs.cn-north-4. myhuaweicloud.com/folder/test.txt 中,对象名为folder/test.txt。 取值范围: 长度大于0且不超过1024的字符串。 默认取值: 无 SubResource SubResourceType 可选 参数解释: 要访问的子资源,详情参见SubResourceType。 Expires int 可选 参数解释: 带授权信息的URL的过期时间。 取值范围: 0~(231-1),单位:秒。 默认取值: 300 Headers map[string]string 可选 参数解释: 请求中携带的头域。 默认取值: 无 QueryParams map[string]string 可选 参数解释: 请求中携带的查询参数。 默认取值: 无 表3 HttpMethodType 常量名 原始值 说明 HttpMethodGet GET HTTP GET请求。 HttpMethodPut POST HTTP POST请求。 HttpMethodPost PUT HTTP PUT请求。 HttpMethodDelete DELETE HTTP DELETE请求。 HttpMethodHead HEAD HTTP HEAD请求。 HttpMethodOptions OPTIONS HTTP OPTIONS请求。 表4 SubResourceType 常量名 原始值 适用接口 SubResourceStoragePolicy storagePolicy 设置/获取桶存储类型。 SubResourceQuota quota 设置/获取桶配额。 SubResourceStorageInfo storageinfo 获取桶存量信息。 SubResourceLocation location 获取桶区域位置。 SubResourceAcl acl 设置/获取桶ACL、设置/获取对象ACL。 SubResourcePolicy policy 设置/获取/删除桶策略。 SubResourceCors cors 设置/获取/删除桶CORS配置。 SubResourceVersioning versioning 设置/获取桶多版本状态。 SubResourceWebsite website 设置/获取/删除桶Website配置。 SubResourceLogging logging 设置/获取桶日志管理配置。 SubResourceLifecycle lifecycle 设置/获取/删除桶生命周期配置。 SubResourceNotification notification 设置/获取桶时间通知配置。 SubResourceTagging tagging 设置/获取/删除桶标签。 SubResourceDelete delete 批量删除对象。 SubResourceVersions versions 列举桶内多版本对象。 SubResourceUploads uploads 列举桶内分段上传任务、初始化分段上传任务。 SubResourceRestore restore 恢复归档或深度归档存储对象。
  • 接口约束 OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点。 如果遇到跨域报错、签名不匹配问题,请参考以下步骤排查问题: 未配置跨域,需要在控制台配置CORS规则,请参考配置桶允许跨域请求。 签名计算问题,请参考URL中携带签名排查签名参数是否正确;比如上传对象功能,后端将Content-Type参与计算签名生成授权URL,但是前端使用授权URL时没有设置Content-Type字段或者传入错误的值,此时会出现跨域错误。解决方案为:Content-Type字段前后端保持一致。
  • 请求参数说明 表1 请求参数列表 参数名称 参数类型 是否必选 描述 input *CreateBrowserBasedSignatureInput 必选 参数解释: 生成带授权信息的表单上传参数接口的请求参数,详情参见CreateBrowserBasedSignatureInput。 表2 CreateBrowserBasedSignatureInput 参数名称 参数类型 是否可选 描述 Bucket string 可选 参数解释: 桶名。 约束限制: 桶的名字需全局唯一,不能与已有的任何桶名称重复,包括其他用户创建的桶。 桶命名规则如下: 3~63个字符,数字或字母开头,支持小写字母、数字、“-”、“.”。 禁止使用IP地址。 禁止以“-”或“.”开头及结尾。 禁止两个“.”相邻(如:“my..bucket”)。 禁止“.”和“-”相邻(如:“my-.bucket”和“my.-bucket”)。 同一用户在同一个区域多次创建同名桶不会报错,创建的桶属性以第一次请求为准。 默认取值: 无 Key string 可选 参数解释: 对象名。对象名是对象在存储桶中的唯一标识。对象名是对象在桶中的完整路径,路径中不包含桶名。 例如,您对象的访问地址为examplebucket.obs.cn-north-4. emyhuaweicloud.com/folder/test.txt 中,对象名为folder/test.txt。 取值范围: 长度大于0且不超过1024的字符串。 默认取值: 无 Expires int 可选 参数解释: 表单上传鉴权的过期时间。 取值范围: 0~(263-1),单位:秒。 默认取值: 300 FormParams map[string]string 可选 参数解释: 除key,policy,signature外,表单上传时的其他参数。 取值范围: acl、cache-control、content-type、content-disposition、content-encoding、expires 默认取值: 无
  • 代码样例 本示例用于生成POST上传对象的带授权信息的URL。 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 66 67 68 69 70 71 72 package main import ( "fmt" "os" 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)*/) if err != nil { fmt.Printf("Create obsClient error, errMsg: %s", err.Error()) } // 生成上传对象的带授权信息的URL input := &obs.CreateBrowserBasedSignatureInput{} input.Bucket = "examplebucket" input.Key = "example/objectname" input.FormParams = map[string]string{ "content-type": "text/plain", "success_action_redirect": "https://www.example.com", } output, err := obsClient.CreateBrowserBasedSignature(input) if err == nil { fmt.Printf("Policy:%s\n", output.Policy) fmt.Printf("Signature:%s\n", output.Signature) } else { fmt.Println(err) return } requestBody := &bytes.Buffer{} writer := multipart.NewWriter(requestBody) writer.WriteField("key", input.Key) writer.WriteField("AccessKeyId", ak) writer.WriteField("policy", output.Policy) writer.WriteField("signature", output.Signature) writer.WriteField("success_action_redirect", "https://www.example.com") // writer.WriteField("token", obs.WithSecurityToken(securityToken)) writer.WriteField("Content-Type", "text/plain") formFile, _ := writer.CreateFormFile("file", "filename") io.Copy(formFile, strings.NewReader("hello OBS!")) writer.Close() url := "https://" + input.Bucket + "." + strings.Replace(endPoint, "https://", "", 1) request, err := http.NewRequest("POST", url, requestBody) if err != nil { fmt.Println(err) return } request.Header.Set("Content-Type", writer.FormDataContentType()) client := &http.Client{} response, err := client.Do(request) if err != nil { fmt.Println(err) return } defer response.Body.Close() if err == nil { fmt.Printf("Use signed-url successful!\n") fmt.Printf("Status:%s,Etag:%s\n", response.Status, response.Header.Get("Etag")) return } fmt.Printf("Use signed-url successful!\n") fmt.Println("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network.") fmt.Println(err) }
  • 返回结果说明 表3 返回结果 参数名称 参数类型 描述 output *CreateBrowserBasedSignatureOutput 参数解释: 接口返回信息,详情参见CreateBrowserBasedSignatureOutput。 err error 参数解释: 接口返回错误信息。 表4 CreateBrowserBasedSignatureOutput 参数名称 参数类型 描述 OriginPolicy string 参数解释: Policy未经过base64之前的值,仅用于校验。示例如下: {"expiration":"2023-09-12T12:52:59Z","conditions":[{"content-type":"text/plain"},{"bucket":"examplebucket"},{"key":"example/objectname"},]}" 默认取值: 无 Policy string 参数解释: 表单中的policy,已经base64之后的值。示例如下: eyJleHBpcmF0aW9uIjoiMjAyMy0wOS0xMlQxMjo1Mjo1OVoiLCJjb25kaXRpb25zIjpbeyJjb250ZW50LXR5cGUiOiJ0ZXh0L3BsYWluIn0seyJidWNrZXQiOiJleGFtcGxlYnVja2V0In0seyJrZXkiOiJleGFtcGxlL29iamVjdG5hbWUifSxdfQ== 默认取值: 无 Signature string 参数解释: 表单中的signature。示例如下: g0jQr4v9VWd1Q2FOFDG6LGfV9Cw= 默认取值: 无