Uploading Objects - Streaming

API Description

Streaming upload uses io.Reader as the data source of an object. You can call ObsClient.PutObject to upload a data stream to OBS.

Method Definition

func (obsClient ObsClient) PutObject(input *PutObjectInput) (output *PutObjectOutput, err error)

Method Definition If a Signed URL Is Used

func (obsClient ObsClient) PutObjectWithSignedUrl(signedUrl string, actualSignedRequestHeaders http.Header, data io.Reader) (output *PutObjectOutput, err error)

Request Parameters

Field

Type

Optional or Mandatory

input

*PutObjectInput

Mandatory

Returned Results

Field

Type

output

*PutObjectOutput

err

error

Sample Code

Uploading a Character String

func main() {
       input := &obs.PutObjectInput{}
       input.Bucket = "bucketname"
       input.Key = "objectname"
       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)
       }
}

Uploading a File Stream

func main() {
       input := &obs.PutObjectInput{}
       input.Bucket = "bucketname"
       input.Key = "objectname"
       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)
       }
}