云服务器内容精选

  • 语法格式 1 2 3 4 5 6 7 8 9 10 11 CREATE TABLE sink_table ( name string, num INT, p_day string, p_hour string ) partitioned by (p_day, p_hour) WITH ( 'connector' = 'filesystem', 'path' = 'obs://*** ', 'format' = 'parquet', 'source.monitor-interval'='' );
  • 示例 从obs表作为数据源读取数据,输出到print connector。 CREATE TABLE obs_source( name string, num INT, `file.path` STRING NOT NULL METADATA ) WITH ( 'connector' = 'filesystem', 'path' = 'obs://demo/sink_parquent_obs', 'format' = 'parquet', 'source.monitor-interval'='1 h' ); CREATE TABLE print ( name string, num INT, path STRING ) WITH ( 'connector' = 'print' ); insert into print select * from obs_source;
  • 接口约束 您必须是桶拥有者或拥有上传对象的权限,才能上传对象。建议使用 IAM 或桶策略进行授权,如果使用IAM则需授予obs:object:PutObject权限,如果使用桶策略则需授予PutObject权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略、配置对象策略。 OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点。 单次上传对象大小范围是[0, 5GB]。 如果需要上传超过5GB的大文件,需要通过多段操作来分段上传。
  • 代码示例 本示例用于流式上传到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 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.PutObjectInput{} // 指定存储桶名称 input.Bucket = "examplebucket" // 指定上传对象,此处以 example/objectname 为例。 input.Key = "example/objectname" fd, _ := os.Open("localfile") input.Body = fd // 流式上传本地文件 output, err := obsClient.PutObject(input) if err == nil { fmt.Printf("Put object(%s) under the bucket(%s) successful!\n", input.Key, input.Bucket) fmt.Printf("StorageClass:%s, ETag:%s\n", output.StorageClass, output.ETag) return } fmt.Printf("Put 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) } }
  • 代码示例 本示例用于将本地localfile文件上传到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 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.PutFileInput{} // 指定存储桶名称 input.Bucket = "examplebucket" // 指定上传对象,此处以 example/objectname 为例。 input.Key = "example/objectname" // 指定本地文件,此处以localfile为例 input.SourceFile = "localfile" // 文件上传 output, err := obsClient.PutFile(input) if err == nil { fmt.Printf("Put file(%s) under the bucket(%s) successful!\n", input.Key, input.Bucket) fmt.Printf("StorageClass:%s, ETag:%s\n", output.StorageClass, output.ETag) return } fmt.Printf("Put file(%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) } }
  • 接口约束 您必须是桶拥有者或拥有上传对象的权限,才能上传对象。建议使用IAM或桶策略进行授权,如果使用IAM则需授予obs:object:PutObject权限,如果使用桶策略则需授予PutObject权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略、配置对象策略。 OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点。 单次上传对象大小范围是[0, 5GB]。 如果需要上传超过5GB的大文件,需要通过多段操作来分段上传。
  • 方式一:手动下载开发包使用GoLand新建工程并编译 以安装OBS Go SDK最新版本为例,步骤如下: 下载OBS Go SDK开发包。 解压该开发包,可以看到其中包含obs文件夹(SDK源码)、main文件夹和examples文件夹(示例代码)和README.MD(SDK版本特性描述文件)。 使用GoLand新建Go工程,将obs、examples、main文件夹拷贝至新建工程中的src文件夹下。 右键单击建好的Go工程,运行弹出菜单中的“Build Project”命令编译工程并等待编译完成。 编译成功后,您的目录结构应该像下面这样: ├── bin ├── pkg ├── src -----├── examples -----├── main -----└── obs └── README.MD
  • OBS服务端错误码 在向OBS服务端发出请求后,如果遇到错误,会在响应中包含响应的错误码描述错误信息。详细的错误码及其对应的描述和HTTP状态码见下表: HTTP状态码 错误码 错误信息 处理措施 301 Moved Permanently PermanentRedirect 尝试访问的桶必须使用指定的地址,请将以后的请求发送到这个地址。 按照返回的重定向地址发送请求。 301 Moved Permanently WebsiteRedirect Website请求缺少bucketName。 携带桶名后重试。 307 Moved Temporarily TemporaryRedirect 临时重定向,当DNS更新时,请求将被重定向到桶。 会自动重定向,也可以将请求发送到重定向地址。 400 Bad Request BadDigest 客户端指定的对象内容的MD5值与系统接收到的内容MD5值不一致。 检查头域中携带的MD5与消息体计算出来的MD5是否一致。 400 Bad Request BadDomainName 域名 不合法。 使用合法的域名。 400 Bad Request BadRequest 请求参数不合法。 根据返回的错误消息体提示进行修改。 400 Bad Request CustomDomainAreadyExist 配置了已存在的域。 已经配置过了,不需要再配置。 400 Bad Request CustomDomainNotExist 删除不存在的域。 未配置或已经删除,无需删除。 400 Bad Request EntityTooLarge 用户POST上传的对象大小超过了条件允许的最大大小。 修改POST上传的policy中的条件或者减少对象大小。 400 Bad Request EntityTooSmall 用户POST上传的对象大小小于条件允许的最小大小。 修改POST上传的policy中的条件或者增加对象大小。 400 Bad Request IllegalLocationConstraintException 用户未带Location在非默认Region创桶。 请求发往默认Region创桶或带非默认Region的Location创桶。 400 Bad Request IncompleteBody 由于网络原因或其他问题导致请求体未接受完整。 重新上传对象。 400 Bad Request IncorrectNumberOfFilesInPost Request 每个POST请求都需要带一个上传的文件。 带上一个上传文件。 400 Bad Request InvalidArgument 无效的参数。 根据返回的错误消息体提示进行修改。 400 Bad Request InvalidBucket 请求访问的桶已不存在。 更换桶名。 400 Bad Request InvalidBucketName 请求中指定的桶名无效,超长或带不允许的特殊字符。 更换桶名。 400 Bad Request InvalidEncryptionAlgorithmError 错误的加密算法。下载SSE-C加密的对象,携带的加密头域错误,导致不能解密。 携带正确的加密头域下载对象。 400 Bad Request InvalidLocationConstraint 创建桶时,指定的Location不合法或不存在。 指定正确的Location创桶。 400 Bad Request InvalidPart 一个或多个指定的段无法找到。这些段可能没有上传,或者指定的entity tag与段的entity tag不一致。 按照正确的段和entity tag合并段。 400 Bad Request InvalidPartOrder 段列表的顺序不是升序,段列表必须按段号升序排列。 按段号升序排列后重新合并。 400 Bad Request InvalidPolicyDocument 表单中的内容与策略文档中指定的条件不一致。 根据返回的错误消息体提示修改构造表单的policy重试。 400 Bad Request InvalidRedirectLocation 无效的重定向地址。 指定正确的地址。 400 Bad Request InvalidRequest 无效请求。 根据返回的错误消息体提示进行修改。 400 Bad Request InvalidRequestBody 请求体无效,需要消息体的请求没有上传消息体。 按照正确的格式上传消息体。 400 Bad Request InvalidTargetBucketForLogging delivery group对目标桶无ACL权限。 对目标桶配置ACL权限后重试。 400 Bad Request KeyTooLongError 提供的Key过长。 使用较短的Key。 400 Bad Request KMS.DisabledException SSE-KMS加密方式下,主密钥被禁用。 更换密钥后重试,或联系技术支持。 400 Bad Request KMS.NotFoundException SSE-KMS加密方式下,主密钥不存在。 携带正确的主密钥重试。 400 Bad Request MalformedACLError 提供的XML格式错误,或者不符合要求的格式。 使用正确的XML格式重试。 400 Bad Request MalformedError 请求中携带的XML格式不正确。 使用正确的XML格式重试。 400 Bad Request MalformedLoggingStatus Logging的XML格式不正确。 使用正确的XML格式重试。 400 Bad Request MalformedPolicy Bucket policy检查不通过。 根据返回的错误消息体提示结合桶policy的要求进行修改。 400 Bad Request MalformedQuotaError Quota的XML格式不正确。 使用正确的XML格式重试。 400 Bad Request MalformedXML 当用户发送了一个配置项的错误格式的XML会出现这样的错误。 使用正确的XML格式重试。 400 Bad Request MaxMessageLengthExceeded 拷贝对象,带请求消息体。 拷贝对象不带消息体重试。 400 Bad Request MetadataTooLarge 元数据消息头超过了允许的最大元数据大小。 减少元数据消息头。 400 Bad Request MissingRegion 请求中缺少Region信息,且系统无默认Region。 请求中携带Region信息。 400 Bad Request MissingRequestBodyError 当用户发送一个空的XML文档作为请求时会发生。 提供正确的XML文档。 400 Bad Request MissingRequiredHeader 请求中缺少必要的头域。 提供必要的头域。 400 Bad Request MissingSecurityHeader 请求缺少一个必须的头。 提供必要的头域。 400 Bad Request TooManyBuckets 用户拥有的桶的数量达到了系统的上限,并且请求试图创建一个新桶。 删除部分桶后重试。 400 Bad Request TooManyCustomDomains 配置了过多的用户域 删除部分用户域后重试。 400 Bad Request TooManyWrongSignature 因高频错误请求被拒绝服务。 更换正确的Access Key后重试。 400 Bad Request UnexpectedContent 该请求需要消息体而客户端没带,或该请求不需要消息体而客户端带了。 根据说明重试。 400 Bad Request UserKeyMustBeSpecified 该操作只有特殊用户可使用。 请联系技术支持。 403 Forbidden AccessDenied 拒绝访问,请求没有携带日期头域或者头域格式错误。 请求携带正确的日期头域。 403 Forbidden AccessForbidden 权限不足,桶未配置CORS或者CORS规则不匹配。 修改桶的CORS配置,或者根据桶的CORS配置发送匹配的OPTIONS请求。 403 Forbidden AllAccessDisabled 用户无权限执行某操作。桶名为禁用关键字。 更换桶名。 403 Forbidden DeregisterUserId 用户已经注销。 充值或重新开户。 403 Forbidden InArrearOrInsufficientBalance 用户欠费或余额不足而没有权限进行某种操作。 充值。 403 Forbidden InsufficientStorageSpace 存储空间不足。 超过配额限制,增加配额或删除部分对象。 403 Forbidden InvalidAccessKeyId 系统记录中不存在客户提供的Access Key Id。 携带正确的Access Key Id。 403 Forbidden NotSignedUp 你的账户还没有在系统中注册,必须先在系统中注册了才能使用该账户。 先注册OBS服务。 403 Forbidden RequestTimeTooSkewed 请求的时间与服务器的时间相差太大。 检查客户端时间是否与当前时间相差太大。 403 Forbidden SignatureDoesNotMatch 请求中带的签名与系统计算得到的签名不一致。 检查你的Secret Access Key和签名计算方法。 403 Forbidden Unauthorized 用户未实名认证。 请实名认证后重试。 404 Not Found NoSuchBucket 指定的桶不存在。 先创桶再操作。 404 Not Found NoSuchBucketPolicy 桶policy不存在。 先配置桶policy。 404 Not Found NoSuchCORSConfiguration CORS配置不存在。 先配置CORS。 404 Not Found NoSuchCustomDomain 请求的用户域不存在。 先设置用户域。 404 Not Found NoSuchKey 指定的Key不存在。 先上传对象。 404 Not Found NoSuchLifecycleConfiguration 请求的LifeCycle不存在。 先配置LifeCycle。 404 Not Found NoSuchUpload 指定的多段上传不存在。Upload ID不存在,或者多段上传已经终止或完成。 使用存在的段或重新初始化段。 404 Not Found NoSuchVersion 请求中指定的version ID与现存的所有版本都不匹配。 使用正确的version ID。 404 Not Found NoSuchWebsiteConfiguration 请求的Website不存在。 先配置Website。 405 Method Not Allowed MethodNotAllowed 指定的方法不允许操作在请求的资源上。 对应返回的Message为:Specified method is not supported. 方法不允许。 408 Request Timeout RequestTimeout 用户与Server之间的socket连接在超时时间内没有进行读写操作。 检查网络后重试,或联系技术支持。 409 Conflict BucketAlreadyExists 请求的桶名已经存在。桶的命名空间是系统中所有用户共用的,选择一个不同的桶名再重试一次。 更换桶名。 409 Conflict BucketAlreadyOwnedByYou 发起该请求的用户已经创建过了这个名字的桶,并拥有这个桶。 不需要再创桶了。 409 Conflict BucketNotEmpty 用户尝试删除的桶不为空。 先删除桶中对象,然后再删桶。 409 Conflict InvalidBucketState 无效的桶状态,配置跨Region复制后不允许关闭桶多版本。 不关闭桶的多版本或取消跨Region复制。 409 Conflict OperationAborted 另外一个冲突的操作当前正作用在这个资源上,请重试。 等待一段时间后重试。 409 Conflict ServiceNotSupported 请求的方法服务端不支持。 服务端不支持,请联系技术支持。 411 Length Required MissingContentLength 必须要提供HTTP消息头中的Content-Length字段。 提供Content-Length消息头。 412 Precondition Failed PreconditionFailed 用户指定的先决条件中至少有一项没有包含。 根据返回消息体中的Condition提示进行修改。 416 Client Requested Range Not Satisfiable InvalidRange 请求的range不可获得。 携带正确的range重试。 500 Internal Server Error InternalError 系统遇到内部错误,请重试。 请联系技术支持。 501 Not Implemented ServiceNotImplemented 请求的方法服务端没有实现。 当前不支持,请联系技术支持。 503 Service Unavailable ServiceUnavailable 服务器过载或者内部错误异常。 等待一段时间后重试,或联系技术支持。 503 Service Unavailable SlowDown 请降低请求频率。 请降低请求频率。 父主题: 异常处理(Go SDK)
  • 接口约束 您必须是桶拥有者或拥有设置对象元数据的权限,才能设置对象元数据。建议使用IAM或桶策略进行授权,如果使用IAM则需授予obs:object:ModifyObjectMetaData权限,如果使用桶策略则需授予ModifyObjectMetaData权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略、配置对象策略。 OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点。 当桶开启多版本控制时,最新版本的对象支持设置元数据,历史版本的对象不支持设置元数据。 对于存储类别为归档存储或深度归档存储的对象,不能对其设置对象的元数据。
  • 代码示例 本示例用于设置examplebucket桶中的example/objectname对象元数据,并且将对象MIME类型设置成image/jpeg,对象存储类型指定为归档存储,并设置自定义元数据。 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() { //推荐通过环境变量获取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.SetObjectMetadataInput{} // 指定存储桶名称 input.Bucket = "examplebucket" // 指定对象,此处以 example/objectname 为例。 input.Key = "example/objectname" // 指定对象MIME类型,这里以image/jpeg为例 input.ContentType = "image/jpeg" // 指定对象存储类型,这里以obs.StorageClassCold为例 input.StorageClass = obs.StorageClassCold // 指定自定义元数据 input.Metadata = map[string]string{"property1": "property-value1", "property2": "property-value2"} // 设置对象元数据 output, err := obsClient.SetObjectMetadata(input) if err == nil { fmt.Printf("Set Object(%s)'s metadata successful with bucket(%s)!\n", input.Key, input.Bucket) fmt.Printf("RequestId:%s\n", output.RequestId) return } fmt.Printf("Set Object(%s)'s metadata fail with bucket(%s)!\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) } }
  • 功能说明 用户可以使用普通方式上传、下载对象,也可以使用服务端加密方式进行上传、下载对象。 OBS支持服务端加密功能,使加密的行为在服务端进行。 用户可以根据自身的需求,使用不同的密钥管理方式来使用服务端加密功能。当前支持两种服务端加密方式: KMS托管密钥的服务端加密(SSE-KMS)和客户提供加密密钥的服务端加密(SSE-C)。上述两种方式都采用行业标准的AES256加密算法。 SSE-KMS方式,OBS使用KMS(Key Management Service)服务提供的密钥进行服务端加密。 SSE-C方式,OBS使用用户提供的密钥和密钥的MD5值进行服务端加密。 使用服务端加密,返回的ETag值不是对象的MD5值。无论是否使用服务端加密上传对象,请求消息头中加入Content-MD5参数时,OBS均会对对象进行MD5校验。
  • 支持接口 OBS Go SDK支持服务端加密的接口见下表: OBS Go SDK接口方法 描述 支持加密类型 ObsClient.PutObject 上传对象时设置加密算法、密钥,对对象启用服务端加密。 SSE-KMS SSE-C ObsClient.PutFile 上传文件时设置加密算法、密钥,对对象启用服务端加密。 SSE-KMS SSE-C ObsClient.GetObject 下载对象时设置解密算法、密钥,用于解密对象。 SSE-C ObsClient.CopyObject 复制对象时设置源对象的解密算法、密钥,用于解密源对象。 复制对象时设置目标对象的加密算法、密钥,对目标对象启用加密算法。 SSE-KMS SSE-C ObsClient.GetObjectMetadata 获取对象元数据时设置解密算法、密钥,用于解密对象。 SSE-C ObsClient.InitiateMultipartUpload 初始化分段上传任务时设置加密算法、密钥,对分段上传任务最终生成的对象启用服务端加密。 SSE-KMS SSE-C ObsClient.UploadPart 上传段时设置加密算法、密钥,对分段数据启用服务端加密。 SSE-C ObsClient.CopyPart 复制段时设置源对象的解密算法、密钥,用于解密源对象。 复制段时设置目标段的加密算法、密钥,对目标段启用加密算法。 SSE-C
  • 代码样例 本示例用于生成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) }
  • 请求参数说明 表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 默认取值: 无
  • 返回结果说明 表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= 默认取值: 无