If you need only part of the data in an object, you can use range download to download data within a specified range.

Specify a valid range

The following code provides an example on how to specify a valid range to download an object:

package main

import (
  "fmt"
  "os"
  "io/ioutil"
  "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
  // Create an OSSClient instance.
  client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
  if err ! = nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }

  // Obtain the bucket.
  bucket, err := client.Bucket("<yourBucketName>")
  if err ! = nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }

  // Query the data that is within the range from byte 15 to byte 35, which includes a total of 21 bytes.
  // If the specified value range is invalid, for example, the specified range includes a negative number, or the specified value is larger than the object size, the entire object is downloaded.
  body, err := bucket.GetObject("<yourObjectName>", oss.Range(15, 35))
  if err ! = nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
  // You must close the stream obtained by using ossClient.getObject after the object is read. Otherwise, connection leaks may occur. Consequently, no connections are available and an exception occurs.
  defer body.Close()

  data, err := ioutil.ReadAll(body)
  if err ! = nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
  fmt.Println("data:", string(data))
}

Specify an invalid range

For an object whose size is 1,000 bytes, the valid range is from byte 0 to byte 999. If the specified range is not within the range from byte 0 to byte 999, the range does not take effect. Then, OSS returns the HTTP status code 200 and the data of the entire object. The following list provides examples of invalid requests and the returned results:

  • If you specify Range: bytes to 500-2000, the value at the end of the range is invalid. Then, OSS returns the HTTP status code 200 and the data of the entire object.
  • If you specify Range: bytes to 1000-2000, the value at the start of the range is invalid. Then, OSS returns the HTTP status code 200 and the data of the entire object.

Compatible download

If you add x-oss-range-behavior:standard to the request header, the download behavior is modified when the specified range is not within the valid range. Example: Use range download to download an object whose size is 1,000 bytes.

  • If you specify Range: bytes to 500-2000, the value at the end of the range is invalid. Then, OSS returns the HTTP status code 206 and the data that is within the range from byte 500 to byte 999.
  • If you specify Range: bytes to 1000-2000, the value at the start of the range is invalid. Then, OSS returns the HTTP status code 416 and the error code InvalidRange.

The following code provides an example on how to perform compatible download:

package main

import (
    "fmt"
    "io/ioutil"
    "os"
    "strings"

    "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
    // Create an OSSClient instance.
    client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Obtain the bucket.
    bucket, err := client.Bucket("<yourBucketName>")
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Upload data of 1,000 bytes.
    strContent := ""
    for i := 0; i < 100; i++ {
        strContent += "abcdefghij"
    }

    fmt.Printf("content len:%d\n", len(strContent))

    // Upload a string.
    err = bucket.PutObject("<yourObjectName>", strings.NewReader(strContent))
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // If you specify Range: bytes to 500-2000, the value at the end of the range is invalid. Then, OSS returns the HTTP status code 206 and the data that is within the range from byte 500 to byte 999.
    // If you specify Range: bytes to 1000-2000, the value at the start of the range is invalid. Then, OSS returns the HTTP status code 416 and the error code InvalidRange.
    body, err := bucket.GetObject("<yourObjectName>", oss.Range(500, 2000), oss.RangeBehavior("standard"))
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // You must close the stream obtained by using ossClient.getObject after the object is read. Otherwise, connection leaks may occur. Consequently, no connections are available and an exception occurs.
    defer body.Close()
    data, err := ioutil.ReadAll(body)
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    if len(data) ! = 500 {
        fmt.Println("read data error, len:%d", len(data))
        os.Exit(-1)
    }
    fmt.Println("data:", string(data))
}