云服务器内容精选

  • 代码示例 本示例用于examplebucket桶中的对象名称为example/objectname图片处理。关于图片处理的代码样例,也可参见图片处理特性指南。 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 package main import ( "fmt" "os" obs "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs" ) func main() { // 您可以通过环境变量获取访问密钥AK/SK,也可以使用其他外部引入方式传入。如果使用硬编码可能会存在泄露风险。 // 您可以登录访问管理控制台获取访问密钥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()) } input := &obs.GetObjectInput{} // 指定存储桶名称 input.Bucket = "examplebucket" // 指定下载对象,此处以 example/objectname 为例。 input.Key = "example/objectname" // 对图片依次进行缩放、旋转 input.ImageProcess = "image/resize,m_fixed,w_100,h_100/rotate,90" // 图片处理 output, err := obsClient.GetObject(input) if err == nil { // output.Body 必须关闭,否则会造成连接泄漏。 defer output.Body.Close() fmt.Printf("Get object(%s) under the bucket(%s) successful!\n", input.Key, input.Bucket) fmt.Printf("ContentType:%s\n", output.ContentType) fmt.Printf("StorageClass:%s, ETag:%s, ContentType:%s, ContentLength:%d, LastModified:%s\n", output.StorageClass, output.ETag, output.ContentType, output.ContentLength, output.LastModified) return } fmt.Printf("List objects under the bucket(%s) fail!\n", input.Bucket) if obsError, ok := err.(obs.ObsError); ok { fmt.Println("An ObsError was found, which means your request sent to OBS was rejected with an error response.") fmt.Println(obsError.Error()) } else { 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) } }
  • 请求参数说明 表1 请求参数列表 参数名称 参数类型 是否必选 描述 input *GetObjectInput 必选 参数解释: 图片处理请求参数。详情参见GetObjectInput。 extensions extensionOptions 可选 参数解释: 拓展配置项。通过调用拓展配置项为对应请求配置额外的拓展请求头,详情参见extensionOptions。 表2 GetObjectInput 参数名称 参数类型 是否必选 描述 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的字符串。 默认取值: 无 ResponseCacheControl string 可选 参数解释: 指定OBS返回请求中Cache-Control头的值。指定Object被下载时的网页的缓存行为。 默认取值: 无 ResponseContentDisposition string 可选 参数解释: 指定OBS返回请求中Content-Disposition头的值。指定Object被下载时的名称。 默认取值: 无 ResponseContentEncoding string 可选 参数解释: 指定OBS返回请求中Content-Encoding头的值。指定Object被下载时的内容编码格式。 默认取值: 无 ResponseContentLanguage string 可选 参数解释: 指定OBS返回请求中Content-Language头的值。指定Object被下载时的内容语言格式。 默认取值: 无 ResponseContentType string 可选 参数解释: 指定OBS返回请求中Content-Type头的值。指定Object文件类型。 默认取值: 无 ResponseExpires string 可选 参数解释: 指定OBS返回请求中Expires头的值。指定Object被下载时的网页的缓存过期时间。 默认取值: 无 VersionId string 可选 参数解释: 对象的版本号。例如:G001117FCE89978B0000401205D5DC9。 取值范围: 长度为32的字符串。 默认取值: 无,如果不设置则默认指定最新版本的对象。 ImageProcess string 可选 参数解释: 图片处理参数,描述针对对象的图片处理命令或处理样式。例如如果想表示对图片依次进行缩放、旋转,取值:image/resize,m_fixed,w_100,h_100/rotate,90。 取值范围: 命令方式:image/commands 样式方式:style/stylename 详细参数说明参见处理图片。 默认取值: 如果不输入处理命令,将返回原图。 RangeStart int64 可选 参数解释: 指定下载对象的起始位置。 取值范围: 0~对象长度,单位:字节。 默认取值: 0,即从对象的第一个字节开始下载。 RangeEnd int64 可选 参数解释: 指定下载对象的结束位置。 取值范围: 取值必须大于RangeStart。 如果该值大于对象长度-1,实际仍取对象长度-1,单位为字节。 默认取值: 无 IfMatch string 可选 参数解释: 指定一个预设的Etag值,如果下载对象的ETag值与该参数值相同,则返回对象内容,否则返回错误。 取值范围: 长度为32的字符串。 默认取值: 无 IfNoneMatch string 可选 参数解释: 指定一个预设的Etag值,如果下载对象的ETag值与该参数值不相同,则返回对象内容,否则返回错误。 取值范围: 长度为32的字符串。 默认取值: 无 IfModifiedSince time.Time 可选 参数解释: 如果对象在指定的时间后有修改,则返回对象内容,否则返回错误。 约束限制: 日期格式必须为ISO8601的格式。 例如:2018-01-01T00:00:00.000Z,表示如果最后修改时间晚于2018-01-01T00:00:00.000Z则返回对象内容,否则返回错误。 代码示例:time.Now().Add(time.Duration(24) * time.Hour)。 默认取值: 无 IfUnmodifiedSince time.Time 可选 参数解释: 如果对象在指定的时间后没有修改,则返回对象内容,否则返回错误。 约束限制: 日期格式必须为ISO8601的格式。 例如:2018-01-01T00:00:00.000Z,表示如果最后修改时间早于2018-01-01T00:00:00.000Z则返回对象内容,否则返回错误。 代码示例:time.Now().Add(time.Duration(24) * time.Hour)。 默认取值: 无 SseHeader SseCHeader 可选 参数解释: 服务端解密头信息,详见SseCHeader。 约束限制: 如果客户端的对象上传时,使用了客户提供的加密密钥进行服务端加密,当下载对象时,同样也必须在消息中提供密钥。 表3 SseCHeader 参数名称 参数类型 是否必选 描述 Encryption string 作为请求参数时必选 参数解释: 表示服务端加密是SSE-C方式。对象使用SSE-C加密方式。 取值范围: 可选值:AES256,即选择SSE-C方式加密对象。 默认取值: 无 Key string 作为请求参数时必选 参数解释: SSE-C方式下加密使用的密钥,该密钥用于加密对象。 约束限制: 该头域由256bit的密钥经过Base64编码得到,示例:K7QkYpBkM5+hca27fsNkUnNVaobncnLht/rCB2o/9Cw= 默认取值: 无 KeyMD5 string 作为请求参数时可选 参数解释: SSE-C方式下加密使用密钥的MD5值,该值用于验证密钥传输过程中是否出错。 约束限制: 由密钥值经过MD5加密再经过Base64编码后得到,示例:4XvB3tbNTN+tIEVa0/fGaQ== 默认取值: 无
  • 上传对象简介(Go SDK) 在OBS中,用户操作的基本数据单元是对象。OBS Go SDK提供了丰富的对象上传接口,可以通过以下方式上传对象: 流式上传 文件上传 分段上传 断点续传上传 SDK支持上传0KB~5GB的对象。流式上传和文件上传的内容大小不能超过5GB;当上传较大文件时,请使用分段上传,分段上传每段内容大小不能超过5GB。 如果上传的对象权限设置为匿名用户读取权限,对象上传成功后,匿名用户可通过链接地址访问该对象数据。对象链接地址格式为:https://桶名.域名/文件夹目录层级/对象名。如果该对象存在于桶的根目录下,则链接地址将不需要有文件夹目录层级。 上传对象-流式上传(Go SDK) 上传对象-文件上传(Go SDK) 上传对象-追加上传(Go SDK) 上传对象-断点续传上传(Go SDK) 父主题: 对象相关接口(Go SDK)
  • 代码示例 本示例用于删除examplebucket桶中的example/objectname对象 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 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()) } input := &obs.DeleteObjectInput{} // 指定存储桶名称 input.Bucket = "examplebucket" // 指定删除对象,此处以 example/objectname 为例。 input.Key = "example/objectname" // 删除对象 output, err := obsClient.DeleteObject(input) if err == nil { fmt.Printf("Delete object(%s) under the bucket(%s) successful!\n", input.Key, input.Bucket) fmt.Printf("RequestId:%s\n", output.RequestId) return } fmt.Printf("Delete object(%s) under the bucket(%s) fail!\n", input.Key, input.Bucket) if obsError, ok := err.(obs.ObsError); ok { fmt.Println("An ObsError was found, which means your request sent to OBS was rejected with an error response.") fmt.Println(obsError.Error()) } else { 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) } }