加密示例
更新时间: 2019-03-14 10:05
上传对象加密
以下代码展示了在上传对象时使用服务端加密功能:
- // 引入依赖包
- import (
- "fmt"
- "obs"
- )
- var ak = "*** Provide your Access Key ***"
- var sk = "*** Provide your Secret Key ***"
- var endpoint = "https://your-endpoint"
- // 创建ObsClient结构体
- var obsClient, _ = obs.New(ak, sk, endpoint)
- func main() {
- input := &obs.PutFileInput{}
- input.Bucket = "bucketname"
- input.Key = "objectkey"
- input.SourceFile = "localfile"
- // 设置SSE-C算法加密对象
- input.SseHeader = &obs.SseCHeader{Encryption: "AES256", Key: "your base64-encoded sse-c key generated by AES-256 algorithm"}
- output, err := obsClient.PutFile(input)
- if err == nil {
- fmt.Printf("RequestId:%s\n", output.RequestId)
- } else if obsError, ok := err.(obs.ObsError); ok {
- fmt.Printf("Code:%s\n", obsError.Code)
- fmt.Printf("Message:%s\n", obsError.Message)
- }
- input.Bucket = "bucketname"
- input.Key = "objectkey2"
- input.SourceFile = "localfile2"
- // 设置SSE-KMS算法加密对象
- input.SseHeader = &obs.SseKmsHeader{Encryption: "kms"}
- output, err = obsClient.PutFile(input)
- if err == nil {
- fmt.Printf("RequestId:%s\n", output.RequestId)
- } else if obsError, ok := err.(obs.ObsError); ok {
- fmt.Printf("Code:%s\n", obsError.Code)
- fmt.Printf("Message:%s\n", obsError.Message)
- }
- }
下载对象解密
以下代码展示了在下载对象时使用服务端解密功能:
- // 引入依赖包
- import (
- "fmt"
- "obs"
- )
- var ak = "*** Provide your Access Key ***"
- var sk = "*** Provide your Secret Key ***"
- var endpoint = "https://your-endpoint"
- // 创建ObsClient结构体
- var obsClient, _ = obs.New(ak, sk, endpoint)
- func main() {
- input := &obs.GetObjectInput{}
- input.Bucket = "bucketname"
- input.Key = "objectkey"
- //设置SSE-C算法解密对象,此处的密钥必须和上传对象加密时使用的密钥一致
- input.SseHeader = &obs.SseCHeader{Encryption: "AES256", Key: "your base64-encoded sse-c key generated by AES-256 algorithm"}
- output, err := obsClient.GetObject(input)
- if err == nil {
- defer output.Body.Close()
- fmt.Printf("ETag:%s\n", output.ETag)
- } else if obsError, ok := err.(obs.ObsError); ok {
- fmt.Printf("Code:%s\n", obsError.Code)
- fmt.Printf("Message:%s\n", obsError.Message)
- }
- }
父主题:服务端加密