packagemainimport("fmt""os"obs"github.com/huaweicloud/huaweicloud-sdk-go-obs/obs")funcmain(){//推荐通过环境变量获取AKSK,这里也可以使用其他外部引入方式传入,如果使用硬编码可能会存在泄露风险。//您可以登录访问管理控制台获取访问密钥AK/SKak:=os.Getenv("AccessKeyID")sk:=os.Getenv("SecretAccessKey")// endpoint填写Bucket对应的Endpoint, 这里以华北-北京四为例,其他地区请按实际情况填写。endPoint:="https://obs.cn-north-4.myhuaweicloud.com"// 创建obsClient实例obsClient,err:=obs.New(ak,sk,endPoint)iferr!=nil{fmt.Printf("Create obsClient error, errMsg: %s",err.Error())}input:=&obs.CreateBucketInput{}// 指定存储桶名称input.Bucket="examplebucket"// 指定存储桶所在区域,此处以“cn-north-4”为例,必须跟传入的Endpoint中Region保持一致。input.Location="cn-north-4"// 指定存储桶的权限控制策略,此处以obs.AclPrivate为例。input.ACL=obs.AclPrivate// 指定存储桶的存储类型,此处以obs.StorageClassWarm为例。如果未指定该参数,则创建的桶为标准存储类型。input.StorageClass=obs.StorageClassWarm// 指定存储桶的AZ类型,此处以“3AZ”为例。不携带时默认为单AZ,如果对应region不支持多AZ存储,则该桶的存储类型仍为单AZ。input.AvailableZone="3az"// 创建桶output,err:=obsClient.CreateBucket(input)iferr==nil{fmt.Printf("Create bucket:%s successful!\n",input.Bucket)fmt.Printf("RequestId:%s\n",output.RequestId)return}fmt.Printf("Create bucket:%s fail!\n",input.Bucket)ifobsError,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)}}
packagemainimport("fmt""os"obs"github.com/huaweicloud/huaweicloud-sdk-go-obs/obs")funcmain(){//推荐通过环境变量获取AKSK,这里也可以使用其他外部引入方式传入,如果使用硬编码可能会存在泄露风险。//您可以登录访问管理控制台获取访问密钥AK/SKak:=os.Getenv("AccessKeyID")sk:=os.Getenv("SecretAccessKey")// endpoint填写Bucket对应的Endpoint, 这里以华北-北京四为例,其他地区请按实际情况填写。endPoint:="https://obs.cn-north-4.myhuaweicloud.com"// 创建obsClient实例obsClient,err:=obs.New(ak,sk,endPoint)iferr!=nil{fmt.Printf("Create obsClient error, errMsg: %s",err.Error())}input:=&obs.CreateBucketInput{}// 指定存储桶名称input.Bucket="examplebucket"// 指定存储桶所在区域,此处以“cn-north-4”为例,必须跟传入的Endpoint中Region保持一致。input.Location="cn-north-4"// 指定存储桶的权限控制策略,此处以obs.AclPrivate为例。input.ACL=obs.AclPrivate// 指定存储桶的存储类型,此处以obs.StorageClassWarm为例。如果未指定该参数,则创建的桶为标准存储类型。input.StorageClass=obs.StorageClassWarm// 指定存储桶的AZ类型,此处以“3AZ”为例。不携带时默认为单AZ,如果对应region不支持多AZ存储,则该桶的存储类型仍为单AZ。input.AvailableZone="3az"// 创建桶output,err:=obsClient.CreateBucket(input)iferr==nil{fmt.Printf("Create bucket:%s successful!\n",input.Bucket)fmt.Printf("RequestId:%s\n",output.RequestId)return}fmt.Printf("Create bucket:%s fail!\n",input.Bucket)ifobsError,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)}}
packagemainimport("fmt""os"obs"github.com/huaweicloud/huaweicloud-sdk-go-obs/obs")funcmain(){//推荐通过环境变量获取AKSK,这里也可以使用其他外部引入方式传入,如果使用硬编码可能会存在泄露风险。//您可以登录访问管理控制台获取访问密钥AK/SKak:=os.Getenv("AccessKeyID")sk:=os.Getenv("SecretAccessKey")// endpoint填写Bucket对应的Endpoint, 这里以华北-北京四为例,其他地区请按实际情况填写。endPoint:="https://obs.cn-north-4.myhuaweicloud.com"// 创建obsClient实例obsClient,err:=obs.New(ak,sk,endPoint)iferr!=nil{fmt.Printf("Create obsClient error, errMsg: %s",err.Error())}input:=&obs.PutFileInput{}// 指定存储桶名称input.Bucket="examplebucket"// 指定上传对象,此处以 objectname 为例。input.Key="objectname"// 指定本地文件,此处以localfile为例input.SourceFile="localfile"// 文件上传output,err:=obsClient.PutFile(input)iferr==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)ifobsError,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)}}
packagemainimport("fmt""os"obs"github.com/huaweicloud/huaweicloud-sdk-go-obs/obs")funcmain(){//推荐通过环境变量获取AKSK,这里也可以使用其他外部引入方式传入,如果使用硬编码可能会存在泄露风险。//您可以登录访问管理控制台获取访问密钥AK/SKak:=os.Getenv("AccessKeyID")sk:=os.Getenv("SecretAccessKey")// endpoint填写Bucket对应的Endpoint, 这里以华北-北京四为例,其他地区请按实际情况填写。endPoint:="https://obs.cn-north-4.myhuaweicloud.com"// 创建obsClient实例obsClient,err:=obs.New(ak,sk,endPoint)iferr!=nil{fmt.Printf("Create obsClient error, errMsg: %s",err.Error())}input:=&obs.PutFileInput{}// 指定存储桶名称input.Bucket="examplebucket"// 指定上传对象,此处以 objectname 为例。input.Key="objectname"// 指定本地文件,此处以localfile为例input.SourceFile="localfile"// 文件上传output,err:=obsClient.PutFile(input)iferr==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)ifobsError,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)}}
packagemainimport("fmt""os"obs"github.com/huaweicloud/huaweicloud-sdk-go-obs/obs")funcmain(){//推荐通过环境变量获取AKSK,这里也可以使用其他外部引入方式传入,如果使用硬编码可能会存在泄露风险。//您可以登录访问管理控制台获取访问密钥AK/SKak:=os.Getenv("AccessKeyID")sk:=os.Getenv("SecretAccessKey")// endpoint填写Bucket对应的Endpoint, 这里以华北-北京四为例,其他地区请按实际情况填写。endPoint:="https://obs.cn-north-4.myhuaweicloud.com"// 创建obsClient实例obsClient,err:=obs.New(ak,sk,endPoint)iferr!=nil{fmt.Printf("Create obsClient error, errMsg: %s",err.Error())}input:=&obs.GetObjectInput{}// 指定存储桶名称input.Bucket="examplebucket"// 指定下载对象,此处以 objectname 为例。input.Key="objectname"// 流式下载对象output,err:=obsClient.GetObject(input)iferr==nil{// output.Body 在使用完毕后必须关闭,否则会造成连接泄漏。deferoutput.Body.Close()fmt.Printf("Get object(%s) under the bucket(%s) successful!\n",input.Key,input.Bucket)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)varreadErrerrorvarreadCountintfor{readCount,readErr=output.Body.Read(p)ifreadCount>0{fmt.Printf("%s",p[:readCount])}ifreadErr!=nil{break}}return}fmt.Printf("List objects under the bucket(%s) fail!\n",input.Bucket)ifobsError,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)}}
packagemainimport("fmt""os"obs"github.com/huaweicloud/huaweicloud-sdk-go-obs/obs")funcmain(){//推荐通过环境变量获取AKSK,这里也可以使用其他外部引入方式传入,如果使用硬编码可能会存在泄露风险。//您可以登录访问管理控制台获取访问密钥AK/SKak:=os.Getenv("AccessKeyID")sk:=os.Getenv("SecretAccessKey")// endpoint填写Bucket对应的Endpoint, 这里以华北-北京四为例,其他地区请按实际情况填写。endPoint:="https://obs.cn-north-4.myhuaweicloud.com"// 创建obsClient实例obsClient,err:=obs.New(ak,sk,endPoint)iferr!=nil{fmt.Printf("Create obsClient error, errMsg: %s",err.Error())}input:=&obs.GetObjectInput{}// 指定存储桶名称input.Bucket="examplebucket"// 指定下载对象,此处以 objectname 为例。input.Key="objectname"// 流式下载对象output,err:=obsClient.GetObject(input)iferr==nil{// output.Body 在使用完毕后必须关闭,否则会造成连接泄漏。deferoutput.Body.Close()fmt.Printf("Get object(%s) under the bucket(%s) successful!\n",input.Key,input.Bucket)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)varreadErrerrorvarreadCountintfor{readCount,readErr=output.Body.Read(p)ifreadCount>0{fmt.Printf("%s",p[:readCount])}ifreadErr!=nil{break}}return}fmt.Printf("List objects under the bucket(%s) fail!\n",input.Bucket)ifobsError,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)}}
packagemainimport("fmt""os"obs"github.com/huaweicloud/huaweicloud-sdk-go-obs/obs")funcmain(){//推荐通过环境变量获取AKSK,这里也可以使用其他外部引入方式传入,如果使用硬编码可能会存在泄露风险。//您可以登录访问管理控制台获取访问密钥AK/SKak:=os.Getenv("AccessKeyID")sk:=os.Getenv("SecretAccessKey")// endpoint填写Bucket对应的Endpoint, 这里以华北-北京四为例,其他地区请按实际情况填写。endPoint:="https://obs.cn-north-4.myhuaweicloud.com"// 创建obsClient实例obsClient,err:=obs.New(ak,sk,endPoint)iferr!=nil{fmt.Printf("Create obsClient error, errMsg: %s",err.Error())}input:=&obs.ListObjectsInput{}// 指定存储桶名称input.Bucket="examplebucket"// 列举桶内对象output,err:=obsClient.ListObjects(input)iferr==nil{fmt.Printf("List objects under the bucket(%s) successful!\n",input.Bucket)fmt.Printf("RequestId:%s\n",output.RequestId)forindex,val:=rangeoutput.Contents{fmt.Printf("Content[%d]-OwnerId:%s, ETag:%s, Key:%s, LastModified:%s, Size:%d\n",index,val.Owner.ID,val.ETag,val.Key,val.LastModified,val.Size)}return}fmt.Printf("List objects under the bucket(%s) fail!\n",input.Bucket)ifobsError,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)}}
packagemainimport("fmt""os"obs"github.com/huaweicloud/huaweicloud-sdk-go-obs/obs")funcmain(){//推荐通过环境变量获取AKSK,这里也可以使用其他外部引入方式传入,如果使用硬编码可能会存在泄露风险。//您可以登录访问管理控制台获取访问密钥AK/SKak:=os.Getenv("AccessKeyID")sk:=os.Getenv("SecretAccessKey")// endpoint填写Bucket对应的Endpoint, 这里以华北-北京四为例,其他地区请按实际情况填写。endPoint:="https://obs.cn-north-4.myhuaweicloud.com"// 创建obsClient实例obsClient,err:=obs.New(ak,sk,endPoint)iferr!=nil{fmt.Printf("Create obsClient error, errMsg: %s",err.Error())}input:=&obs.ListObjectsInput{}// 指定存储桶名称input.Bucket="examplebucket"// 列举桶内对象output,err:=obsClient.ListObjects(input)iferr==nil{fmt.Printf("List objects under the bucket(%s) successful!\n",input.Bucket)fmt.Printf("RequestId:%s\n",output.RequestId)forindex,val:=rangeoutput.Contents{fmt.Printf("Content[%d]-OwnerId:%s, ETag:%s, Key:%s, LastModified:%s, Size:%d\n",index,val.Owner.ID,val.ETag,val.Key,val.LastModified,val.Size)}return}fmt.Printf("List objects under the bucket(%s) fail!\n",input.Bucket)ifobsError,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)}}