This topic describes how to perform a range download, which lets you efficiently retrieve specific parts of a file.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Configure OSSClient instances.
To perform range download, you must have the
oss:GetObjectpermission. For more information, see Attach a custom policy to a RAM user.
Sample code
The following code shows how to download data from a specified range in a file.
package main
import (
"io"
"log"
"strings"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
log.Fatalf("Error: %v", err)
}
// Create an OSSClient instance.
// Set yourEndpoint to the Endpoint of the bucket. For example, for a bucket in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual Endpoint.
// Set yourRegion to the region where the bucket is located. For example, for a bucket in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
log.Fatalf("Error: %v", err)
}
// Specify the bucket name.
bucketName := "yourBucketName"
bucket, err := client.Bucket(bucketName)
if err != nil {
log.Fatalf("Error: %v", err)
}
// Upload 1,000 bytes of content.
strContent := ""
for i := 0; i < 100; i++ {
strContent += "abcdefghij"
}
log.Printf("content len: %d\n", len(strContent))
// Upload the string.
objectName := "yourObjectName"
err = bucket.PutObject(objectName, strings.NewReader(strContent))
if err != nil {
log.Fatalf("Error: %v", err)
}
// Get data within the range of 15 to 35 bytes. This includes bytes 15 and 35, for a total of 21 bytes.
// If the specified range is invalid (for example, the start or end position is a negative number or is larger than the file size), the entire file is downloaded.
body, err := bucket.GetObject(objectName, oss.Range(15, 35))
if err != nil {
log.Fatalf("Error: %v", err)
}
defer body.Close()
// Read the data into memory.
data, err := io.ReadAll(body)
if err != nil {
log.Fatalf("Error: %v", err)
}
log.Printf("data: %s", string(data))
}
FAQ
What happens if I specify an invalid download range?
What is a standard behavior range download?
References
For the complete sample code for range downloads, see GitHub sample.
For more information about the API operation for range downloads, see GetObject.