Updated on 2022-02-10 GMT+08:00

GET Object

API Description

You can use this API to download an object in a specified bucket.

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 Parameter

Field

Type

Optional or Mandatory

input

*GetObjectInput

Mandatory

Returned Result

Field

Type

output

*GetObjectOutput

err

error

Sample Code

  • Downloading an object:
    func main() {
           input := &obs.GetObjectInput{}
           input.Bucket = "bucketname"
           input.Key = "objectkey"
           output, err := obsClient.GetObject(input)
           if err == nil {
                  defer output.Body.Close()
                  fmt.Printf("RequestId:%s\n", output.RequestId)
                  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
                  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.Println(obsError.Code)
                         fmt.Println(obsError.Message)
                  } else {
                         fmt.Println(err)
                  }
           }
    }
  • Download an object and save it to the local PC:
    func main() {
           input := &obs.GetObjectInput{}
           input.Bucket = "bucketname"
           input.Key = "objectkey"
           output, err := obsClient.GetObject(input)
           if err == nil {
                  defer output.Body.Close()
                  fmt.Printf("RequestId:%s\n", output.RequestId)
                  fmt.Printf("StorageClass:%s, ETag:%s, ContentType:%s, ContentLength:%d, LastModified:%s\n",
                         output.StorageClass, output.ETag, output.ContentType, output.ContentLength, output.LastModified)
                  p, err := ioutil.ReadAll(output.Body)
                  if err == nil {   
                      ioutil.WriteFile("***your file path***", p, os.ModeAppend);
                  }
    
           } else {
                  if obsError, ok := err.(obs.ObsError); ok {
                         fmt.Println(obsError.Code)
                         fmt.Println(obsError.Message)
                  } else {
                         fmt.Println(err)
                  }
           }
    }