Downloading Objects - Partially

API Description

You can use this API to download data falling within a specific range when only partial data of an object is required. If the specified range is 0 to 1000, data at the 0th to the 1000th bytes, 1001 bytes in total, will be returned. If the specified range is invalid, data of the whole object will be returned.

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"
       // Set the start point and end point.
       input.RangeStart = 0
       input.RangeEnd = 1000
       output, err := obsClient.GetObject(input)
       if err == nil {
              defer output.Body.Close()
              p := make([]byte, 1024)
              var readErr error
              var readCount int
              // Obtain 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)
       }
}
  • If the specified range is invalid (because the start or end position is set to a negative integer or the range is longer than the object length), data of the whole object will be returned.
  • This download method also can be used to concurrently download parts of a large object. For details about the sample code, see concurrent_download_object_sample.