Downloading Objects - Streaming

API Description

You can use this API to download a specified file in streaming mode.

Method Definition

func (obsClient ObsClient) GetObject(input *GetObjectInput) (output *GetObjectOutput, err error)

Method Definition If a Signed URL Is Used

func (obsClient ObsClient) GetObjectWithSignedUrl(signedUrl string, actualSignedRequestHeaders http.Header) (output *GetObjectOutput, err error)

Request Parameters

Field

Type

Optional or Mandatory

input

*GetObjectInput

Mandatory

Returned Results

Field

Type

output

*GetObjectOutput

err

error

Sample Code

func main() {
       input := &obs.GetObjectInput{}
       input.Bucket = "bucketname"
       input.Key = "objectname"
       output, err := obsClient.GetObject(input)
       if err == nil {
              defer output.Body.Close()
              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)
              var readErr error
              var readCount int
              // Read the object content.
              for {
                     readCount, readErr = output.Body.Read(p)
                     if readCount > 0 {
                           fmt.Printf("%s", p[:readCount])
                     }
                     if readErr != nil {
                           break
                     }
              }
       } else if obsError, ok := err.(obs.ObsError); ok {
              fmt.Printf("Code:%s\n", obsError.Code)
              fmt.Printf("Message:%s\n", obsError.Message)
       }
}

Object input streams obtained by GetObjectOutput.Body must be closed explicitly. Otherwise, resource leakage occurs.