流式上传
更新时间: 2019-03-14 10:05
流式上传使用io.Reader作为对象的数据源。您可以通过ObsClient.PutObject上传您的数据流到OBS。以下代码展示了如何进行流式上传:
上传字符串
- // 引入依赖包
- import (
- "fmt"
- "obs"
- "strings"
- )
- 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.PutObjectInput{}
- input.Bucket = "bucketname"
- input.Key = "objectkey"
- input.Body = strings.NewReader("Hello OBS")
- output, err := obsClient.PutObject(input)
- if err == nil {
- fmt.Printf("RequestId:%s\n", output.RequestId)
- 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)
- }
- }
上传文件流
- // 引入依赖包
- import (
- "fmt"
- "obs"
- "os"
- )
- 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.PutObjectInput{}
- input.Bucket = "bucketname"
- input.Key = "objectkey"
- fd, _ := os.Open("localfile")
- input.Body = fd
- output, err := obsClient.PutObject(input)
- if err == nil {
- fmt.Printf("RequestId:%s\n", output.RequestId)
- 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)
- }
- }
- 大文件上传建议使用分段上传。
- 上传内容大小不能超过5GB。
父主题:上传对象